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. Git与GitHub到底有什么联系?

    Git与GitHub区别 git 是一个软件 版本控制器 分享合并代码, 团队开发 时间机器, 可以获取到任意阶段时间节点开发的代码, 代码找回 git, cvs, bitkeeper, svn 典型 ...

  2. SimpleDateFormate的使用

    import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public cl ...

  3. centos 7 下进入单用户模式修改root密码

    centos7进入单用户模式修改root用户密码 在工作中可能会遇到root密码忘记,那么这里就要进入单用户模式下了. 在重启电脑之后 时间:2015-05-13 10:42来源:blog.51cto ...

  4. bootstarp风格的toggle效果分享

    最近在写项目的时候想要一个这样的效果: 我知道这个效果在 flat-ui中有, 但是我又不想引用一整个flat-ui; 这个效果依赖html5的transition, 所以浏览器兼容成问题: 从fla ...

  5. Java+FlexPaper+swfTools仿百度文库文档在线预览系统设计与实现

    笔者最近在给客户开发文档管理系统时,客户要求上传到管理系统的文档(包括ppt,word,excel,txt)只能预览不允许下载.笔者想到了百度文库和豆丁网,百度文库和豆丁网的在线预览都是利用flash ...

  6. 浅谈 GPU图形固定渲染管线

    图形渲染管道被认为是实时图形渲染的核心,简称为管道.管道的主要功能是由给定的虚拟摄像机.三维物体.灯源.光照模型.纹理贴图或其他来产生或渲染一个二维图像.由此可见,渲染管线是实时渲染技术的底层工具.图 ...

  7. android开发之生命周期

    android开发之生命周期 一:Activity的生命周期: 这几天了了解了安卓Activity的生命周期,对于生命周期有了大概的理解: 一个Activity的生命周期也就是Activity从生成到 ...

  8. 精通Web Analytics 2.0 (2) 内容简介

    精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 这本书里有什么? 这本书是在我第一本书<网站分析:每天一小时>的基础上创作的.我并不打算绕弯子,第一章是网站分析2. ...

  9. C/C++中数组转换成指针的情况

    数组转换成指针:在大多数用到数组的表达式中,数组自动转换成指向数组首元素的指针.比如: int ia[10]; int *p = ia; //ia转换成指向数组首元素的指针 以下情况上述转换不会发生: ...

  10. IRC(Internet Relay Chat Protocol) Protocal Learning && IRC Bot

    catalogue . Abstract . INTRODUCTION . 通信协议Connection Registration Action . 通信协议Channel operations Ac ...