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. BOP 2016 复赛题目

    复赛采用团队赛的形式,基于Azure云计算平台在实际大数据的基础上展开前沿课题的较量. 在编程之美挑战赛复赛中,选手需要通过组队共同完成复赛题,考查选手编程能力的同时,也考验选手的团队协作能力.选手点 ...

  2. SpringMVC使用@PathVariable,@RequestBody,@ResponseBody,@RequestParam,@InitBinder

    @Pathvariable public ResponseEntity<String> ordersBack(           @PathVariable String reqKey, ...

  3. Gson解析json数据

    1.解析单个对象 JSON字符串如下 [ { "formatDate":"2015-10-28", "date":"2015\u5 ...

  4. [转] easyui 获取数据表格中选中行的数据 Get selected row data from...

    原文地址:http://my.oschina.net/meSpace/blog/41463 http://www.easyui.net/2010/06/easyui-tutorial-get-sele ...

  5. 【BZOJ 2693】jzptab

    莫名其妙地又卡在long long上了,我果然又在同一个地方犯逗. 在“在路上,同梦行”群里闹了个大笑话QAQ #include<cstdio> #include<cstring&g ...

  6. nutch-介绍

    本文主要源于http://www.ibm.com/developerworks/cn/opensource/os-cn-nutchintro/ 基本信息 Nutch是一个开放源代码(open-sour ...

  7. Maven-setting.xml详解

    settings.xml对于maven来说相当于全局性的配置,用于所有的项目,当Maven运行过程中的各种配置,例如pom.xml,不想绑定到一个固定的project或者要分配给用户时,我们使用set ...

  8. python求职之路

    自我介绍 这是一道送分题,万年不变的第一个问题.不过有些小伙伴可能没有太在意,其实这个问题已经在面试官心中决定了你的去留意向.自我介绍的主要结构:个人基本信息 + 基本技术构成 + 项目经验(具体项目 ...

  9. java的执行与加载的过程

    第一.我们编写一个.java源文件: 第二.通过编译器javac.exe把.java源文件编译为.class字节码文件并装入类装载器里: 第三.java虚拟机java.exe把字节码文件解释为各个平台 ...

  10. 理解 Java 的三大特性之多态

    面向对象编程有三大特性:封装.继承.多态. 封装隐藏了类的内部实现机制,可以在不影响使用的情况下改变类的内部结构,同时也保护了数据.对外界而已它的内部细节是隐藏的,暴露给外界的只是它的访问方法. 继承 ...