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. PS技能大全

    1.设置固定的图片大小 参考:http://jingyan.baidu.com/album/642c9d3418bec4644a46f72a.html?picindex=1

  2. redis的主从复制,读写分离,主从切换

    当数据量变得庞大的时候,读写分离还是很有必要的.同时避免一个redis服务宕机,导致应用宕机的情况,我们启用sentinel(哨兵)服务,实现主从切换的功能. redis提供了一个master,多个s ...

  3. 《Spring 3.0就这么简单》 读书笔记

    第一章:快速入门 开发流程: 1.创建库表    依赖jar包配置 2.事务:事务是恢复和并发控制的基本单位. 原子性(Atomicity) 一致性(Consistency) 隔离性(Isolatio ...

  4. asp.net MVC添加HtmlHelper扩展示例和用法

    一.先创建一个HtmlHelper的扩展类,代码: using System; using System.Collections.Generic; using System.Linq; using S ...

  5. jquery里的on方法使用

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <script src ...

  6. MyEclipse下Maven的安装配置

    Maven常用命令: •mvn archetype:generate :创建 Maven 项目 •mvn compile :编译源代码 •mvn test-compile :编译测试代码 •mvn t ...

  7. springMVC之AOP

    AOP(Aspect-Oriented Programming,面向切面编程) 切面(Aepect):横切关注点(跨越应用程序多个模块的功能)被模块化的对象: 通知(Advice):切面必须要完成的工 ...

  8. 【POJ 3693】Maximum repetition substring 重复次数最多的连续重复子串

    后缀数组的论文里的例题,论文里的题解并没有看懂,,, 求一个重复次数最多的连续重复子串,又因为要找最靠前的,所以扫的时候记录最大的重复次数为$ans$,扫完后再后从头暴力扫到尾找重复次数为$ans$的 ...

  9. 初探psutil

    系统性能信息模块psutil 1,psutil简介 psutil是一个跨平台能够轻松获取系统的进程和系统利用率,主要应用在系统监控,分析和限制系统资源以及进程管理.它实现了很多系统管理的命令,如ps, ...

  10. awk打印出当前行的上一行

    #awk '/B/{print a;}{a=$0}' a.txt A # cat a.txt A BCDE