using System; using System.IO; using System.Text;class Test {
public static void Main() { string path = @"c:\temp\MyTest.txt"; // Delete the file if it exists. if (File.Exists(path)) { File.Delete(path); } //Create the file. using (FileStream fs = File.Create(path)) { AddText(fs, "This is some text"); AddText(fs, "This is some more text,"); AddText(fs, "\r\nand this is on a new line"); AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n"); for (int i=1;i < 120;i++) { AddText(fs, Convert.ToChar(i).ToString()); //Split the output at every 10th character. if (Math.IEEERemainder(Convert.ToDouble(i), 10) == 0) { AddText(fs, "\r\n"); } } } //Open the stream and read it back. using (FileStream fs = File.OpenRead(path)) { byte[] b = new byte[1024]; UTF8Encoding temp = new UTF8Encoding(true); while (fs.Read(b,0,b.Length) > 0) { Console.WriteLine(temp.GetString(b)); } } } private static void AddText(FileStream fs, string value) { byte[] info = new UTF8Encoding(true).GetBytes(value); fs.Write(info, 0, info.Length); } } http://msdn.microsoft.com/en-us/library/system.io.filestream.aspx
StreamReader m_streamReader = new StreamReader ( fs ) ;
//使用StreamReader类来读取文件
m_streamReader.BaseStream.Seek ( 0 , SeekOrigin.Begin ) ;
// 从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容
this.richTextBox1.Text = "" ;
string strLine = m_streamReader.ReadLine ( ) ;
while ( strLine != null )
{
this.richTextBox1.Text += strLine + "\n" ;
strLine = m_streamReader.ReadLine ( ) ;
}
//关闭此StreamReader对象
m_streamReader.Close ( ) ;
using System.IO;
using System.Text;class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt"; // Delete the file if it exists.
if (File.Exists(path))
{
File.Delete(path);
} //Create the file.
using (FileStream fs = File.Create(path))
{
AddText(fs, "This is some text");
AddText(fs, "This is some more text,");
AddText(fs, "\r\nand this is on a new line");
AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n"); for (int i=1;i < 120;i++)
{
AddText(fs, Convert.ToChar(i).ToString()); //Split the output at every 10th character.
if (Math.IEEERemainder(Convert.ToDouble(i), 10) == 0)
{
AddText(fs, "\r\n");
}
}
} //Open the stream and read it back.
using (FileStream fs = File.OpenRead(path))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b,0,b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
}
}
} private static void AddText(FileStream fs, string value)
{
byte[] info = new UTF8Encoding(true).GetBytes(value);
fs.Write(info, 0, info.Length);
}
}
http://msdn.microsoft.com/en-us/library/system.io.filestream.aspx
streamreader sr=new streamreader(fs);
sr.readline();
sr.readxxx();
using(StreamReader sr = File.OpenText(filePath)
{
while(true)
{
string currentLine = sr.ReadLine();
if(currentLine ==null)break;
Console.WriteLine(currentLine);
}
}
if (!Directory.Exists(@"c:\temp"))
{
Directory.CreateDirectory(@"c:\temp");
}
if(!File.Exists(@"c:\temp\1.txt"))
{
FileStream fs = File.Create(@"c:\temp\1.txt");
fs.Close();
}
OpenFileDialog openfile = new OpenFileDialog();
openfile.Filter = "文本类型|*.txt|其他类型|*.*";
if (openfile.ShowDialog() == DialogResult.OK)
{
this.txtPath.Text = openfile.FileName;
}
using (StreamReader Sr = new StreamReader(this.txtPath.Text))
{
string Line;
Line = Sr.ReadToEnd();
this.txtContent.Text = Line;
}