C#读写txt文件的方法
1.添加命名空间
System.IO;
System.Text;
2.文件的读取
#region 读取TXT文本文件
/// <summary>
/// FileStream读取文本文件
/// </summary>
public void FileStreamRead()
{
//文件路径
string filePath = AppDomain.CurrentDomain.BaseDirectory; // Server.MapPath("~/UploadFiles/");
//文件夹不存在则创建
if (!System.IO.Directory.Exists(filePath))
{
System.IO.Directory.CreateDirectory(filePath);
}
filePath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Today.ToString("yyyMMdd") + ".txt";
if (System.IO.File.Exists(filePath))
{
//1.直接读取出字符串
string strText = System.IO.File.ReadAllText(filePath);
//2.按行读取为字符串数组
string[] arrFileText = System.IO.File.ReadAllLines(filePath);
//3.FileStream读取写入给定的缓存区
System.IO.FileStream fs = new FileStream(filePath, FileMode.Open);
fs.Seek(, SeekOrigin.Begin);
byte[] byData = new byte[];
fs.Read(byData, , );//byData传进来的字节数组,用以接受FileStream对象中的数据
System.Text.Decoder d = System.Text.Encoding.Default.GetDecoder();
char[] charData = new char[];
d.GetChars(byData, , byData.Length, charData, );
fs.Close();
}
}
/// <summary>
/// StreamReader读取文本文件
/// </summary>
public void StreamReaderRead()
{
//文件路径
string filePath = AppDomain.CurrentDomain.BaseDirectory; // Server.MapPath("~/UploadFiles/");
//文件夹不存在则创建
if (!System.IO.Directory.Exists(filePath))
{
System.IO.Directory.CreateDirectory(filePath);
}
filePath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Today.ToString("yyyMMdd") + ".txt";
if (System.IO.File.Exists(filePath))
{
//1.从头到尾以流的方式读出文本文件,该方法会读出一行文本
System.IO.StreamReader sr = new StreamReader(filePath);
string strStreamReader = sr.ReadToEnd();
sr.Close();
}
}
#endregion
文件读取
FileStream fs = new FileStream(@"c:\temp\ascii.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
//StreamReader sr3 = new StreamReader(fs);
//string str3 = sr3.ReadToEnd();
//sr3.Close();
StreamReader sr4 = new StreamReader(fs, System.Text.Encoding.Default);
string strCH = sr4.ReadToEnd();
sr4.Close();
string str2 = System.IO.File.ReadAllText(@"c:\temp\ascii.txt", System.Text.Encoding.ASCII);
3.文件的写入
#region 写入TXT文本文件
/// <summary>
/// StreamWriter写入文本文件
/// </summary>
public void StreamWriterWrite()
{
//文件路径
string filePath = AppDomain.CurrentDomain.BaseDirectory; // Server.MapPath("~/UploadFiles/");
//文件不存在则创建
if (!System.IO.Directory.Exists(filePath))
{
System.IO.Directory.CreateDirectory(filePath);
}
filePath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Today.ToString("yyyMMdd") + ".txt";
#region 另一种方式
////FileMode.Append,FileAccess.Write追加文件
//FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate);
//StreamWriter sw = new StreamWriter(fs);
#endregion
StreamWriter sw = new StreamWriter(filePath, true);
//Write直接追加文件末尾,不换行;WriteLine直接追加文件末尾,换行
sw.WriteLine("测试StreamWriter写入TXT文件" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
//清空缓冲区、关闭流
sw.Flush();
sw.Close();
//直接追加到文件
//using (System.IO.StreamWriter sw = System.IO.File.AppendText(filePath))
//{
// sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
//}
}
/// <summary>
/// FileStream写入文本文件
/// </summary>
public void FileStreamWrite()
{
//文件路径
string filePath = AppDomain.CurrentDomain.BaseDirectory; // Server.MapPath("~/UploadFiles/");
//文件不存在则创建
if (!System.IO.Directory.Exists(filePath))
{
System.IO.Directory.CreateDirectory(filePath);
}
filePath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Today.ToString("yyyMMdd") + ".txt";
//1.该方法写入字符数组换行显示
string[] arrFile = { "first line", "second line", "third line", "第四行" };
System.IO.File.AppendAllLines(filePath, arrFile, System.Text.Encoding.Default);
//2.字符串写入文本
string strTest = "该例子测试一个字符串写入文本文件。";
System.IO.File.AppendAllText(filePath, strTest, System.Text.Encoding.Default);
//3.FileMode.Append,FileAccess.Write追加文件
FileStream fs = new FileStream(filePath, FileMode.Append,FileAccess.Write);
byte[] data = System.Text.Encoding.Default.GetBytes("测试FileStream写入TXT文件" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
fs.Write(data, , data.Length);
//清空缓冲区、关闭流
fs.Flush();
fs.Close();
}
#endregion
文件写入
#region 读取 保存
////读取
//string strFileTxt = string.Empty;
//using (FileStream fs = new FileStream(strFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
//{
// StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8); //选择编码方式
// strFileTxt = sr.ReadToEnd();
//}
////保存
//using (FileStream fs = new FileStream(strSavePath, FileMode.Create, FileAccess.Write))
//{
// byte[] fData = Encoding.UTF8.GetBytes(strFileTxt);
// fs.Write(fData, 0, fData.Length);
// fs.Flush();
//}
#endregion
读取 保存
http://www.cnblogs.com/jx270/archive/2013/04/14/3020456.html
C#读写txt文件的方法的更多相关文章
- [转载]C#读写txt文件的两种方法介绍
C#读写txt文件的两种方法介绍 by 大龙哥 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char ...
- C#读写txt文件的两种方法介绍
C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...
- C#读写txt文件的两种方法介绍[转]
C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...
- C#读写txt文件的两种方法介绍 v
C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...
- Javascript写入txt和读取txt文件的方法
文章主要介绍了Javascript写入txt和读取txt文件的方法,需要的朋友可以参考下1. 写入 FileSystemObject可以将文件翻译成文件流. 第一步: 例: 复制代码 代码如下: Va ...
- java指定编码的按行读写txt文件(几种读写方式的比较)
转: java指定编码的按行读写txt文件(几种读写方式的比较) 2018年10月16日 20:40:02 Handoking 阅读数:976 版权声明:本文为博主原创文章,未经博主允许不得转载. ...
- python使用xlrd模块读写Excel文件的方法
本文实例讲述了python使用xlrd模块读写Excel文件的方法.分享给大家供大家参考.具体如下: 一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi ...
- WPF 读写TxT文件
原文:WPF 读写TxT文件 文/嶽永鹏 WPF 中读取和写入TxT 是经常性的操作,本篇将从详细演示WPF如何读取和写入TxT文件. 首先,TxT文件希望逐行读取,并将每行读取到的数据作为一个数组的 ...
- python操作txt文件中数据教程[1]-使用python读写txt文件
python操作txt文件中数据教程[1]-使用python读写txt文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原始txt文件 程序实现后结果 程序实现 filename = '. ...
随机推荐
- linux文件链接
我的github,欢迎关注,分享知识与技术 链接:一种在共享文件和访问它的用户的若干目录项之间建立联系的一种方法. Linux中包括两种链接:硬链接(HardLink)和软链接(Soft Link), ...
- hdu2328(后缀数组 + 二分)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2328 题意: 求 n 个串的字典序最小的最长公共子串 思路: 本题中单个字符串长度不超过 200, ...
- MobaXterm替换cmder
Windows上命令行工具cmder确实很好用,其扩展功能呢,比系统自带强大几倍.后来在使用MobaXterm,官网https://mobaxterm.mobatek.net/免费版本功能足够强大,支 ...
- 20.Add Two Numbers(两个链表的和)
Level: Medium 题目描述: You are given two non-empty linked lists representing two non-negative integer ...
- java程序设计实验
建立文件调试jdk idea断点调试 项目素数的寻遍
- java8 optional操作
目标:测试option的过滤,链式操作: 代码: package test; import java.util.ArrayList; import java.util.Arrays; import j ...
- Python判断字符串编码以及编码的转换
转自:http://www.cnblogs.com/zhanhg/p/4392089.html Python判断字符串编码以及编码的转换 判断字符串编码: 使用 chardet 可以很方便的实现字符串 ...
- 2-28 if...else
- poj1860 兑换货币(bellman ford判断正环)
传送门:点击打开链接 题目大意:一个城市有n种货币,m个货币交换点,你有v的钱,每个交换点只能交换两种货币,(A换B或者B换A),每一次交换都有独特的汇率和手续费,问你存不存在一种换法使原来的钱更多. ...
- 在IntelliJ IDEA中配置Google Java Code Style及代码格式化快捷键
google-java-format plugin should intercept the “Reformat Code” action in IDEA (Ctrl+Alt+L) and apply ...