C#读写文本文件
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#读写文本文件的更多相关文章
- Java读写文本文件操作
package com.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; ...
- delphi读写文本文件
delphi读写文本文件 在工作中遇到了这样一个问题,使用PLSQL将一个表的数据转化成一些列的insert语句存储到一个.sql文本中,我本来想使用access数据库中的查询视图一次执行这些语句 ...
- 03_Android项目中读写文本文件的代码
编写一下Android界面的项目 使用默认的Android清单文件 <?xml version="1.0" encoding="utf-8"?> & ...
- 通过读写文本文件小结“关于python处理中文编码的问题”
一.引言 无论学习什么程序语言,字符串这种数据类型总是着有非常重要.然而最近在学习python这门语言,想要显示中文,总是出现各种乱码.于是在网上查了很多资料,各说纷纭,我也尝试了许多的方法,有时候可 ...
- Java入门:读写文本文件
文本文件的读写是学习java必须掌握的一项基本技术,因为在项目中时常会涉及到文本文件的读写. 一.使用FileWriter写文件 1.FileWriter类 [功能] FileWriter类专门用来写 ...
- 【289】◀▶ Python I/O & 读写文本文件
参考:Python 文件 I/O 参考:Python OS 文件/目录方法 目录: 01 open 函数 用于打开一个文件,创建一个 file 对象,相关的方法才可以调用它进行读写. 02 F ...
- python 读写文本文件
本人最近新学python ,用到文本文件的读取,经过一番研究,从网上查找资料,经过测试,总结了一下读取文本文件的方法. 1.在读取文本文件的时无非有两种方法: a.f=open('filename', ...
- StreamWrite-StreamRead 读写文本文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- C# 读写文本文件乱码解决方案
在使用C#对文本文件读取的时候,如果其中包含了中文,经常会出现乱码.一般解决是在StreamReader加一个编码,我使用的是Encoding.UTF8,一般情况下使用这个参数就可以.但是,在这次我使 ...
- 关于fstream、ifstream、ofstream读写文本文件、二进制文件详解
fstream.ifstream.ofstream是c++中关于文件操作的三个类 fstream类对文件进行读操作和写操作 打开文件 fstream fs("要打开的文件名",打开 ...
随机推荐
- pageEncoding与contentType属性
1图例分析 由图中可以看出,这个两个属性没有任何关系. 把这两个设置成不同的编码格式对中文显示不会产生任何影响 2.原因分析 pageEncoding规定了以什么编码方式存储和读取,使两者保持一致性, ...
- python基础-PyCharm设置作者信息模板_修改解释器_设置软件UTF-8编码
一.PyCharm 设置作者信息模板 1.File---Settings---在搜索框中搜索:File and Code Templates---Python scripts #!/usr/bin/e ...
- import random 模块导入
import random print(random.random()) #浮点数值 print(random.randint(1,2))#循环显示1,2 print(random.randrange ...
- Python:no encoding declared 错误
使用Python编译的时候出现如下错误: SyntaxError: Non-ASCII character ‘\xe5’ in file magentonotes.com.py on line 2, ...
- tomcat 的安全规范
下面来说一下,日常工作当中我们需要注意的一些tomcat的安全规范: 一:telnet管理端口的保护 类别 配置内容及其说明 标准配置 备注 Telnet 管理端口的保护 (强制) (1)配置文件中的 ...
- HttpModule与HttpHandler详解
ASP.NET对请求处理的过程:当请求一个*.aspx文件的时候,这个请求会被inetinfo.exe进程截获,它判断文件的后缀(aspx)之后,将这个请求转交给 ASPNET_ISAPI.dll,A ...
- iOS开发--二维码的生成
一.需要包含头文件 #import <CoreImage/CoreImage.h> 二.示例代码 -- 以下生成的二维码不够清晰 如图: - (void)touchesBegan:(NSS ...
- iPad开发--UIPopoverController简单使用iOS7之前和iOS7之后的使用方法
一.iOS7之前的Popover的使用 对Popover进行懒加载处理 内容控制器中设置Popover弹出后的尺寸 设置显示的位置,两种情况.1 -- 给BarButtonItem设置Popover的 ...
- Handler消息机制与Binder IPC机制完全解析
1.Handler消息机制 序列 文章 0 Android消息机制-Handler(framework篇) 1 Android消息机制-Handler(native篇) 2 Android消息机制-H ...
- 转发;Dota英文名
http://esports.17173.com/content/2011-02-17/20110217163225753.shtml 有的英雄的名字真的很漂亮,也给那些学校里英文各种悲剧的同鞋增加点 ...