1. Add a Namespace
System.IO;
System.Text;
2. Read the file
(1). Use the FileStream class to read the file, convert it to a char array, and then output.
public void read () { byte[] byData = new byte[100]; char[] chardata = new char[1000]; try { FileStream file = new FileStream ("E:\\test.txt", filemode.open); file. Seek (0, seekorigin.begin); &Nbsp; file. Read (bydata, 0, 100); //bydata incoming byte array to accept data from the FileStream object, the 2nd parameter is the position in the byte array where the data is written, usually 0, Indicates that the data is written to the array from the beginning file of the array, and the last parameter specifies how many characters to read from the file . decoder d = encoding.default.getdecoder (); d.getchars (byData, 0, bydata.length, chardata, 0); console.writeline (Chardata); file. Close (); } catch (ioexception e) { &nbsP; console.writeline (E.ToString ()); } }
(2). Use StreamReader to read the file, and then one line of output.
public void Read (string path) {StreamReader sr = new StreamReader (Path,encoding.default); String Line; while (line = Sr. ReadLine ()) = null) {Console.WriteLine (line. ToString ()); } }
3. Writing the file
(1). Use the FileStream class to create the file, and then write the data to the file.
public void Write ()
{
FileStream fs = new FileStream ("E:\\ak.txt", FileMode.Create);
Get byte array
byte[] data = System.Text.Encoding.Default.GetBytes ("Hello world!");
Start writing
Fs. Write (data, 0, data. Length);
Empties the buffer, closes the stream
Fs. Flush ();
Fs. Close ();
}
(2). Use the FileStream class to create a file, use the StreamWriter class, and write the data to a file.
Public void write (String path) { filestream fs = new filestream (path, filemode.create); Streamwriter sw = new streamwriter (FS); //begins writing to             SW. Write ("Hello world!!!!"); //emptying buffer        SW. Flush (); //Close Flow         SW. Close (); fs. Close (); }
Overall code:
using system;using system.io;using system.text;public class program{ public static void main () { write ("E : \\program.xml "); write2 (); read ("E:\\program.xml"); read2 (); console.readline ();} static void read2 () { Try{ byte[] bydata = new byte[100]; char[] chardata = new char[1000]; Filestream file = new filestream ("E:\\program2.xml", filemodE.open); file. Seek (0, seekorigin.begin); file. Read (bydata, 0, 100); //bydata incoming byte array to accept data from the FileStream object, the 2nd parameter is the position in the byte array where the data is written, usually 0, Indicates that the data is written to the array from the beginning file of the array, and the last parameter specifies how many characters to read from the file . Decoder d = Encoding.Default.GetDecoder (); d. GetChars (bydata, 0, bydata.length, chardata, 0); console.writeline (Chardata); file. Close ();} catch (ioexception e) { &Nbsp; console.writeline (E.tostring ());}} static void read (String path) { streamreader sr = new streamreader (Path, encoding.default); string line; while (LINE = SR. ReadLine ()) != null) { Console.WriteLine (line. ToString ());}} static void write2 () { filestream fs = new filestream ("E:\\program2.xml", FileMode.Create) ; //get byte array byte[] data = System.Text.Encoding.Default.GetBytes ("hello world!"); //Start Writing fs. Write (Data, 0, data. Length); //empties the buffer, closes the stream fs. Flush (); fs. Close ();} static void write (String path) { filestream fs = new filestream (path, filemode.create); streamwriter sw = new streamwriter (FS); //begins writing to         SW. Write ("Hello world!!!!"); //empties the buffer sw. Flush (); //close the stream sw. Close (); fs. Close ();}}
Results:
Hello WORLD!!!!
Hello world!
Reference:
Http://www.cnblogs.com/akwwl/p/3240813.html
This article comes from the "Ricky's blog" blog, please be sure to keep this source http://57388.blog.51cto.com/47388/1655714
C #-Two ways to read TXT files