C#读取文本文件和C# 写文本文件
2013-09-25 17:32
204 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; // 引用输入输出操作的命令空间
namespace ReadWriteFile
{
class Program
{
// 主函数
static void Main(string[] args)
{
Read(); // 读操作
Write(); // 写操作
}
// 读操作
public static void Read()
{
// 读取文件的源路径及其读取流
string strReadFilePath = @"../../data/ReadLog.txt";
StreamReader srReadFile = new StreamReader(strReadFilePath);
// 读取流直至文件末尾结束
while (!srReadFile.EndOfStream)
{
string strReadLine = srReadFile.ReadLine(); //读取每行数据
Console.WriteLine(strReadLine); //屏幕打印每行数据
}
// 关闭读取流文件
srReadFile.Close();
Console.ReadKey();
}
// 写操作
public static void Write()
{
// 统计写入(读取的行数)
int WriteRows = 0;
// 读取文件的源路径及其读取流
string strReadFilePath = @"../../data/ReadLog.txt";
StreamReader srReadFile = new StreamReader(strReadFilePath);
// 写入文件的源路径及其写入流
string strWriteFilePath = @"../../data/WriteLog.txt";
StreamWriter swWriteFile = File.CreateText(strWriteFilePath);
// 读取流直至文件末尾结束,并逐行写入另一文件内
while (!srReadFile.EndOfStream)
{
string strReadLine = srReadFile.ReadLine(); //读取每行数据
++WriteRows; //统计写入(读取)的数据行数
swWriteFile.WriteLine(strReadLine); //写入读取的每行数据
Console.WriteLine("正在写入... " + strReadLine);
}
// 关闭流文件
srReadFile.Close();
swWriteFile.Close();
Console.WriteLine("共计写入记录总数:" + WriteRows);
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; // 引用输入输出操作的命令空间
namespace ReadWriteFile
{
class Program
{
// 主函数
static void Main(string[] args)
{
Read(); // 读操作
Write(); // 写操作
}
// 读操作
public static void Read()
{
// 读取文件的源路径及其读取流
string strReadFilePath = @"../../data/ReadLog.txt";
StreamReader srReadFile = new StreamReader(strReadFilePath);
// 读取流直至文件末尾结束
while (!srReadFile.EndOfStream)
{
string strReadLine = srReadFile.ReadLine(); //读取每行数据
Console.WriteLine(strReadLine); //屏幕打印每行数据
}
// 关闭读取流文件
srReadFile.Close();
Console.ReadKey();
}
// 写操作
public static void Write()
{
// 统计写入(读取的行数)
int WriteRows = 0;
// 读取文件的源路径及其读取流
string strReadFilePath = @"../../data/ReadLog.txt";
StreamReader srReadFile = new StreamReader(strReadFilePath);
// 写入文件的源路径及其写入流
string strWriteFilePath = @"../../data/WriteLog.txt";
StreamWriter swWriteFile = File.CreateText(strWriteFilePath);
// 读取流直至文件末尾结束,并逐行写入另一文件内
while (!srReadFile.EndOfStream)
{
string strReadLine = srReadFile.ReadLine(); //读取每行数据
++WriteRows; //统计写入(读取)的数据行数
swWriteFile.WriteLine(strReadLine); //写入读取的每行数据
Console.WriteLine("正在写入... " + strReadLine);
}
// 关闭流文件
srReadFile.Close();
swWriteFile.Close();
Console.WriteLine("共计写入记录总数:" + WriteRows);
Console.ReadKey();
}
}
}
相关文章推荐
- C#读取文本文件出现乱码的问题及解决
- c#读取写入文本文件
- C#读取文本文件和C# 写文本文件
- C#逐行读取文本文件
- C#读取文本文件
- C#中读取文本文件导入SQL数据库解决方法
- C#文本文件读取和写入
- C#快速随机按行读取大型文本文件 - 磊的博客 - sanshi_leilei - 和讯博客
- C#读取文本文件和C# 写文本文件
- C# 读取文本文件
- 通过程序 VB.Net 或 C# 读取文本文件行数
- C#读取文本文件和C# 写文本文件
- C#创建创建文本文件写入读取,可以用来做系统日志或程序操作日志或者错误记录
- c# 以二进制读取文本文件
- C#读取文本文件
- C# 使用List泛型读取和保存文本文件
- C#读取超大文本文件的部分内容
- 通过程序 VB.Net 或 C# 读取文本文件行数
- C#读取文本文件和C# 写文本文件