How to use C # To Read txt files

Source: Internet
Author: User

1. Use FileStream to read and write files File Header:Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. IO;Core code for reading files:Byte [] byData = new byte [100];
Char [] charData = new char [1000];
Try
{
FileStream sFile = new FileStream ("file path", FileMode. Open );
SFile. Seek (55, SeekOrigin. Begin );
SFile. read (byData, 0,100); // The first parameter is the byte array that is passed in to accept the data in the FileStream object. The second parameter is the position where data is written in the byte array, it is usually 0, indicating to write data to the array from the array's start file. The last parameter specifies the number of characters read from the file.
}
Catch (IOException e)
{
Console. WriteLine ("An IO exception has been thrown! ");
Console. WriteLine (e. ToString ());
Console. ReadLine ();
Return;
}
Decoder d = Encoding. UTF8.GetDecoder ();
D. GetChars (byData, 0, byData. Length, charData, 0 );
Console. WriteLine (charData );
Console. ReadLine ();Core code for writing files:FileStream fs = new FileStream (file path, FileMode. Create );
// Obtain the byte array byte [] data = new UTF8Encoding (). GetBytes (String );
// Start writing
Fs. Write (data, 0, data. Length); // clears the buffer and closes the stream.
Fs. Flush ();
Fs. Close ();2. Use StreamReader and StreamWriter File Header:Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. IO;StreamReader reads files:StreamReader objReader = new StreamReader (file path );
String sLine = "";
ArrayList LineList = new ArrayList ();
While (sLine! = Null)
{
SLine = objReader. ReadLine ();
If (sLine! = Null &&! SLine. Equals (""))
LineList. Add (sLine );
}
ObjReader. Close ();
Return LineList;StreamWriter writes files:FileStream fs = new FileStream (file path, FileMode. Create );
StreamWriter sw = new StreamWriter (fs );
// Start writing
Sw. Write (String );
// Clear the buffer
Sw. Flush ();
// Close the stream
Sw. Close ();
Fs. Close ();

Read. txt files with csung.
StreamReader sr = new StreamReader ("TestFile.txt") // StreamReader sr = new StreamReader ("TestFile.txt", Encoding. GetEncoding ("GB2312 "))
/// GBK
String line;
While (line = sr. ReadLine ())! = Null)
{
TextBox1. Text + = ii. ToString () + "-" + line. ToString () + "";

}
Add reference:System. IO
StreamReader objReader = new StreamReader ("c: \ test.txt ");
System. IOObjects in a namespace, especiallySystem. IO. StreamReaderClass.

It is generally used together to represent the Enter key on the keyboard. It can also be used only to represent the "TAB" key on the keyboard.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.