C#读写txt
StreamReader
和 StreamWriter
类用于文本文件的数据读写。这些类从抽象基类 Stream 继承,Stream
支持文件流的字节读写。
读文件StreamReader实例
using System;
using System. IO;
namespace FileApplication
{
class Program
{
static void Main ( string[ ] args)
{
try
{
using ( StreamReader sr = new StreamReader ( "c:/jamaica.txt" ) )
{
string line;
while ( ( line = sr. ReadLine ( ) ) != null)
{
Console. WriteLine ( line) ;
}
}
}
catch ( Exception e)
{
Console. WriteLine ( "The file could not be read:" ) ;
Console. WriteLine ( e. Message) ;
}
Console. ReadKey ( ) ;
}
}
}
读文件StreamWriter 实例
using System;
using System. IO;
namespace FileApplication
{
class Program
{
static void Main ( string[ ] args)
{
string[ ] names = new string[ ] { "Zara Ali" , "Nuha Ali" } ;
using ( StreamWriter sw = new StreamWriter ( "names.txt" ) )
{
foreach ( string s in names)
{
sw. WriteLine ( s) ;
}
}
string line = "" ;
using ( StreamReader sr = new StreamReader ( "names.txt" ) )
{
while ( ( line = sr. ReadLine ( ) ) != null)
{
Console. WriteLine ( line) ;
}
}
Console. ReadKey ( ) ;
}
}
}
txt的读
string[ ] txtLines = File. ReadAllLines ( @"./result/data.txt" ) ;
for ( int i = 0 ; i < txtLines. Length; ++ i)
{
string[ ] line = txtLines[ i] . Split ( ',' ) ;
IRow dataRow = confidence_sheet. CreateRow ( i) ;
for ( int j = 0 ; j < line. Length; j++ )
{
ICell cell = dataRow. CreateCell ( j + 1 ) ;
cell. SetCellValue ( line[ j] ) ;
cell. CellStyle = cellstyle1;
}
}
写入的几种方式
WriteAllText
string strTest = "该例子测试一个字符串写入文本文件。" ;
System. IO. File. WriteAllText ( @"C:\testDir\test1.txt" , strTest, Encoding. UTF8) ;
File.WriteAllLines
string[ ] lines = { "first line" , "second line" , "third line" , "第四行" } ;
System. IO. File. WriteAllLines ( @"C:\testDir\test.txt" , lines, Encoding. UTF8) ;
1. FileStream. Write
string filePath = Directory. GetCurrentDirectory ( ) + "\\" + Process. GetCurrentProcess ( ) . ProcessName + ".txt" ;
if ( File. Exists ( filePath) )
File. Delete ( filePath) ;
FileStream fs = new FileStream ( filePath, FileMode. Create) ;
string xyPointer = string. Format ( "X: {0}, Y: {1}" , this . Location. X. ToString ( ) , this . Location. Y. ToString ( ) ) ;
string highWidth = string. Format ( "\nW: {0}, H: {1}" , this . Width. ToString ( ) , this . Height. ToString ( ) ) ;
byte[ ] data = System. Text. Encoding. Default. GetBytes ( xyPointer + highWidth) ;
fs. Write ( data, 0 , data. Length) ;
fs. Flush ( ) ;
fs. Close ( ) ;
2. File. WriteAllLines
string[ ] lines = { "first line" , "second line" , "third line" , "第四行" } ;
System. IO. File. WriteAllLines ( @"C:\testDir\test.txt" , lines, Encoding. UTF8) ;
3. File. WriteAllText
string strTest = "该例子测试一个字符串写入文本文件。" ;
System. IO. File. WriteAllText ( @"C:\testDir\test1.txt" , strTest, Encoding. UTF8) ;
4. StreamWriter. Write
using ( System. IO. StreamWriter file = new System. IO. StreamWriter ( @"C:\testDir\test2.txt" , true ) )
{
foreach ( string line in lines)
{
if ( ! line. Contains ( "second" ) )
{
file. Write ( line) ;
file. WriteLine ( line) ;
}
}
}
读txt
File.ReadAllText(filepath);
string readText = File. ReadAllText ( path) ;
File.ReadAllLines(path);
using System;
using System. IO;
class Test
{
public static void Main ( )
{
string path = @"c:\temp\MyTest.txt" ;
if ( ! File. Exists ( path) )
{
string[ ] createText = { "Hello" , "And" , "Welcome" } ;
File. WriteAllLines ( path, createText) ;
}
string appendText = "This is extra text" + Environment. NewLine;
File. AppendAllText ( path, appendText) ;
string[ ] readText = File. ReadAllLines ( path) ;
foreach ( string s in readText)
{
Console. WriteLine ( s) ;
}
}
}
System.IO.File.ReadAllBytes