1.读取文件

2.写入文件

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml; namespace BigData.Common.File
{
/// <summary>
/// 文件操作类
/// </summary>
public class FileHelper
{
/// <summary>
/// 写入一行数据
/// </summary>
/// <param name="path"></param>
/// <param name="str"></param>
/// <param name="isAppend"></param>
/// <returns></returns>
public static bool WriteLine(string path, string str, bool isAppend = true)
{
try
{
StreamWriter sw = new StreamWriter(path, isAppend);
sw.WriteLine(str);
sw.Flush();
sw.Close();
}
catch (Exception)
{
return false;
} return true;
} /// <summary>
/// 读取文件所有内容
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string ReadAll(string path)
{
string result = string.Empty;
try
{
StreamReader sr = new StreamReader(path);
result = sr.ReadToEnd();
sr.Close();
}
catch (Exception)
{
return string.Empty;
} return result;
} /// <summary>
/// 写入文本
/// </summary>
/// <param name="path"></param>
/// <param name="str"></param>
/// <returns></returns>
public static bool WriteToTxt(string path, string str)
{
try
{
FileStream fs = new FileStream(path, FileMode.Append);
byte[] bs = Encoding.Default.GetBytes(str);
fs.Write(bs, , bs.Length);
fs.Flush();
fs.Close();
}
catch (Exception)
{
return false;
} return true;
} /// <summary>
/// 读取文本
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string ReadTxt(string path)
{
string result = string.Empty;
try
{
FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
byte[] bs = new byte[fs.Length];
fs.Read(bs, , bs.Length);
result = Encoding.Default.GetString(bs);
fs.Close();
}
catch (Exception)
{
return string.Empty;
} return result;
}
}
}

ASP.NET 文件操作类的更多相关文章

  1. asp.net文件操作类

    /** 文件操作类 **/ #region 引用命名空间 using System; using System.Collections.Generic; using System.Text; usin ...

  2. [C#] 常用工具类——文件操作类

    /// <para> FilesUpload:工具方法:ASP.NET上传文件的方法</para> /// <para> FileExists:返回文件是否存在&l ...

  3. 文件操作类CFile

    CFile file; CString str1= L"写入文件成功!"; wchar_t *str2; if (!file.Open(L"Hello.txt" ...

  4. android 文件操作类简易总结

    android 文件操作类(参考链接) http://www.cnblogs.com/menlsh/archive/2013/04/02/2997084.html package com.androi ...

  5. Ini文件操作类

    /// <summary> /// Ini文件操作类 /// </summary> public class Ini { // 声明INI文件的写操作函数 WritePriva ...

  6. java csv 文件 操作类

    一个CSV文件操作类,功能比较齐全: package tool; import java.io.BufferedReader; import java.io.BufferedWriter; impor ...

  7. Qt5:Qt文件操作类 QFile

    在QT中,操作文件一般不使用C++提供的文件操作类 , 因为操作文件的时候,要用到C++提供的 string 类,而在QT中使用的是Qt自己实现的一个string类 QString .在Qt中使用C+ ...

  8. C# 文件操作类大全

      C# 文件操作类大全 时间:2015-01-31 16:04:20      阅读:1724      评论:0      收藏:0      [点我收藏+] 标签: 1.创建文件夹 //usin ...

  9. Java文件操作类效率对比

    前言 众所周知,Java中有多种针对文件的操作类,以面向字节流和字符流可分为两大类,这里以写入为例: 面向字节流的:FileOutputStream 和 BufferedOutputStream 面向 ...

随机推荐

  1. JDK动态代理与CGLib动态代理相关问题

    导读: 1.JDK动态代理原理是什么?为什么不支持类的代理? 2.JDK动态代理实例 3.CGLib代理原理是什么? 4.CGLib代理实例 5.JDK动态代理与CGLib代理的区别是什么? 6.总结 ...

  2. 通过jQuery制作电子时钟表的代码

    源码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <titl ...

  3. 【读书笔记】iOS-iOS开发之iOS程序偏好设置(Settings Bundle)的使用

    在Android手机上, 在某个程序里,通过按Menu键,一般都会打开这个程序的设置,而在iOS里,系统提供了一个很好的保存程序设置的机制.就是使用Settings Bundle. 在按了HOME键的 ...

  4. IDEA项目搭建十二——站点用户登录会话实现

    一.简介 前两天写了一篇用户登录会话设计的脑图,这次就把这个引入到项目中实现,总体来说需要几步先罗列一下: 1.需要一个Cookie工具类用于读写cookie 2.需要一个Cache工具类用于在服务端 ...

  5. Python 基于python+mysql浅谈redis缓存设计与数据库关联数据处理

    基于python+mysql浅谈redis缓存设计与数据库关联数据处理 by:授客  QQ:1033553122 测试环境 redis-3.0.7 CentOS 6.5-x86_64 python 3 ...

  6. Loadrunner 脚本录制-通过代理录制脚本

    脚本录制-通过代理录制脚本 by:授客 QQ:1033553122 版本:Loadruner 11.0 A.PC端录制Web应用程序 步骤1:根据实际情况,选择对应的协议 本例中选择Web(HTTP/ ...

  7. ScrollView与ListView的事件冲突

    布局文件 当ListView嵌套在ScrollView中时,会发生冲突,导致ListView控件的拉动效果消失‘ 解决办法: 重写ListView的onTouchEvent(),并在返回前调用getP ...

  8. Luncene介绍

    1.Luncene介绍 案例:实现一个文件的搜索功能,通过关键字搜索文件,凡是文件名或文件内容包括关键字文件都需要找出来.还可以根据中文词语进行查询,并且需要支持多个条件查询.Lucene可以解决 数 ...

  9. Asp.Net WebApi Get请求整理(一)

    Asp.Net WebApi+JQuery Ajax的Get请求整理 一.总结 1.Asp.Net WebApi默认不支持Get请求,需要在Action方法上指定[HttpGet], 除非Action ...

  10. python TCP socket套接字编程以及注意事项

    TCPServer.py #coding:utf-8 import socket #s 等待链接 #c 实时通讯 s = socket.socket(socket.AF_INET,socket.SOC ...