static public string Read(string path)
{
StreamReader sr = new StreamReader(path,Encoding.Default);
String line;
StringBuilder sb=new StringBuilder();
while ((line = sr.ReadLine()) != null)
{
sb.AppendLine(line);

}
return sb.ToString();

}

public static string ReadFile(string FilePath)
{
if (System.IO.File.Exists(FilePath))
{
try
{
StreamReader reader = new StreamReader(FilePath, Encoding.GetEncoding("gb2312"));//System.IO.File.OpenText(FilePath);
StringBuilder builder = new StringBuilder();
string str = "";
string str2 = "";
while ((str = reader.ReadLine()) != null)
{
builder.AppendLine(str  );
}
str2 = builder.ToString();
reader.Close();
return str2;
}
catch
{
return "";
}
}
return "";
}

public static bool SaveFile(string FilePath, string type, string content)
{
string path = FilePath;
if (!File.Exists(path))
type = "NEW";
try
{
StreamWriter writer;
if (type == "NEW")
{
using (writer = new StreamWriter(FilePath, false, Encoding.GetEncoding("gb2312")))//System.IO.File.CreateText(path))
{
writer.Write(content);
writer.Flush();
writer.Close();
return true;
}
}
if (type == "ADD")
{
if (System.IO.File.Exists(path))
{
writer = new StreamWriter(path, true, Encoding.GetEncoding("gb2312"));
writer.Write(content);
writer.Flush();
writer.Close();
return true;
}
return false;
}
return false;
}
catch
{
return false;
}
}

 public class FileHelper
{
public static string ReadFile(string path)
{
StreamReader sr = new StreamReader(path, Encoding.Default);
String line;
StringBuilder sb = new StringBuilder();
while ((line = sr.ReadLine()) != null)
{
sb.AppendLine(line); }
return sb.ToString(); } public static IEnumerable<string> ReadFileToEnumerable(string path)
{
StreamReader sr = new StreamReader(path, Encoding.Default);
String line; while ((line = sr.ReadLine()) != null)
{
yield return line; } } public static IList<T> ReadFileToList<T>(string[] path, Func<string, T> func)
{
var list = new List<T>();
foreach (var item in path)
{
list = list.Union(ReadFileToEnumerable(item).Select(func)).ToList();
}
return list;
}
public static bool AppendFile(string filePath, string type, string content)
{ try
{ if (System.IO.File.Exists(filePath))
{
var writer = new StreamWriter(filePath, true, Encoding.GetEncoding("gb2312"));
writer.Write(content);
writer.Flush();
writer.Close();
return true;
}
return false; }
catch
{
return false;
}
} public static bool CreateFile(string filePath, string type, string content)
{ try
{
StreamWriter writer; using (writer = new StreamWriter(filePath, false, Encoding.GetEncoding("gb2312")))//System.IO.File.CreateText(path))
{
writer.Write(content);
writer.Flush();
writer.Close();
return true;
} }
catch
{
return false;
}
} }

  

C#读写文本文件的更多相关文章

  1. Java读写文本文件操作

    package com.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; ...

  2. delphi读写文本文件

    delphi读写文本文件   在工作中遇到了这样一个问题,使用PLSQL将一个表的数据转化成一些列的insert语句存储到一个.sql文本中,我本来想使用access数据库中的查询视图一次执行这些语句 ...

  3. 03_Android项目中读写文本文件的代码

    编写一下Android界面的项目 使用默认的Android清单文件 <?xml version="1.0" encoding="utf-8"?> & ...

  4. 通过读写文本文件小结“关于python处理中文编码的问题”

    一.引言 无论学习什么程序语言,字符串这种数据类型总是着有非常重要.然而最近在学习python这门语言,想要显示中文,总是出现各种乱码.于是在网上查了很多资料,各说纷纭,我也尝试了许多的方法,有时候可 ...

  5. Java入门:读写文本文件

    文本文件的读写是学习java必须掌握的一项基本技术,因为在项目中时常会涉及到文本文件的读写. 一.使用FileWriter写文件 1.FileWriter类 [功能] FileWriter类专门用来写 ...

  6. 【289】◀▶ Python I/O & 读写文本文件

    参考:Python 文件 I/O 参考:Python OS 文件/目录方法 目录: 01   open 函数 用于打开一个文件,创建一个 file 对象,相关的方法才可以调用它进行读写. 02   F ...

  7. python 读写文本文件

    本人最近新学python ,用到文本文件的读取,经过一番研究,从网上查找资料,经过测试,总结了一下读取文本文件的方法. 1.在读取文本文件的时无非有两种方法: a.f=open('filename', ...

  8. StreamWrite-StreamRead 读写文本文件

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. C# 读写文本文件乱码解决方案

    在使用C#对文本文件读取的时候,如果其中包含了中文,经常会出现乱码.一般解决是在StreamReader加一个编码,我使用的是Encoding.UTF8,一般情况下使用这个参数就可以.但是,在这次我使 ...

  10. 关于fstream、ifstream、ofstream读写文本文件、二进制文件详解

    fstream.ifstream.ofstream是c++中关于文件操作的三个类 fstream类对文件进行读操作和写操作 打开文件 fstream fs("要打开的文件名",打开 ...

随机推荐

  1. 1014mysqldumpslow.pl简单分析慢日志 WINDOW平台

    转自http://www.th7.cn/db/mysql/201507/113998.shtml 要想运行mysqldumpslow.pl(这是perl程序),下载perl编译器.下载地址:http: ...

  2. Linux IO Scheduler(Linux IO 调度器)

    每个块设备或者块设备的分区,都对应有自身的请求队列(request_queue),而每个请求队列都可以选择一个I/O调度器来协调所递交的request.I/O调度器的基本目的是将请求按照它们对应在块设 ...

  3. 对C语言中指针的一些新认识

    学C语言这么久了,才发现指针不是想象中那么简单,当初根本就没理解指针怎么用! 变量--是由操作系统自动分配存储空间的    指针--手动分配存储空间或指向已有变量的地址 指针中的内容需要手动释放,而变 ...

  4. 离线安装redis集群

    Step0:redis集群组件需求 Step1:离线安装ruby Step2:离线安装rubygems Step3:安装rubygems的 redis api Step4:离线安装tcl 8.6 St ...

  5. REST服务中的异常处理

    在REST服务中,服务端如果产生了异常信息,无论是业务异常或是系统异常,如果直接将异常抛出,在客户端浏览器中,是无法获取异常的详细,只能获取一个StateCode 500 Internal Serve ...

  6. Android Studio构建系统基础

    基础知识 项目创建成功后会自动下载Gradle,这个过程特别慢,建议FQ.下载的Gradle在Windows平台会默认在 C:\Documents and Settings\<用户名>.g ...

  7. 【hdu 5918】Sequence I(KMP)

    给定两个数字序列,求a序列中每隔p个构成的p+1个序列中共能匹配多少个b序列. 例如1 1 2 2 3 3 每隔1个的序列有两个1 2 3 kmp,匹配时每次主串往前p个,枚举1到p为起点. 题目 # ...

  8. eclipse下查看jdk源码

    打开eclipse,点 "window"-> "Preferences" -> "Java" -> "Insta ...

  9. BZOJ 4384: [POI2015]Trzy wieże

    4384: [POI2015]Trzy wieże Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 217  Solved: 61[Submit][St ...

  10. 关于使用struts2时子窗体页面跳转后在父窗体打开的问题以及Session过期后的页面跳转问题

    问题1:传统的系统界面,iframe了三个页面,上,左,右,用户点击注销的按钮在上面得top.jsp里面,方法:<a href="../adminAction/admin_logout ...