FileStream没有代码之分阿,是一个二进制流
要读文本还是用StreamReader吧
只要读写文件用同样的编码方式就不会出现乱码用来生成文本文件,
StreamWriter sw = new StreamWriter("TestFile.txt", System.Text.Encoding.Default);然后再用
StreamReader sr = new StreamReader("TestFile.txt", System.Text.Encoding.Default);
要读文本还是用StreamReader吧
只要读写文件用同样的编码方式就不会出现乱码用来生成文本文件,
StreamWriter sw = new StreamWriter("TestFile.txt", System.Text.Encoding.Default);然后再用
StreamReader sr = new StreamReader("TestFile.txt", System.Text.Encoding.Default);
{
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);
}
本示例使用 StreamWriter 类的 WriteLine 方法将字符串写入到文本文件中。示例
// Compose a string that consists of three lines.
string lines = "First line.\r\nSecond line.\r\nThird line.";// Write the string to a file.
System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt");
file.WriteLine(lines);file.Close();
编译代码
复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。将 "c:\\test.txt" 替换为实际的文件名。可靠编程
以下情况可能会导致异常: 该文件存在并且是只读的 (IOException)。
路径名可能太长 (PathTooLongException)。
磁盘可能已满 (IOException)。
安全性
如果该文件不存在,本示例将创建新文件。如果该文件已经存在,应用程序将改写它。为了追加现有文件,请将布尔值参数设为 true,如下所示:System.IO.StreamWriter file =
new System.IO.StreamWriter("c:\\test.txt", true);
请参见
通过使用特定编码在字符与字节之间进行转换,利用 System.IO 命名空间类可以将字符作为流(也就是连续的数据组)从文件中读取或写入文件。它包括一对类:StreamReader 和 StreamWriter,它们使您得以从文件读取字符顺序流或将字符顺序流写入文件中。StreamReader 和 StreamWriter 类镜像 BinaryReader 和 BinaryWriter 类的功能,但它们以文本而不是二进制形式读取和写入信息。有关更多信息,请参见通过 BinaryReader 和 BinaryWriter 类进行文件访问。StreamReader 类是从名为 TextReader 的抽象类派生的,后者从流或文件读取字符。StreamWriter 类是从名为 TextWriter 的抽象类派生的,后者将字符写入流或文件。安全说明 将数据写入文件时,如果要写入数据的文件不存在,则应用程序可能需要创建该文件。为此,应用程序需要具有创建该文件所在的那个目录的写权限。但是,如果该文件已经存在,则应用程序只需要该文件本身的“写”权限。只要可能,在部署时创建该文件并且只授予对该文件的“写”权限,要比授予对整个目录的写权限更安全。此外,将数据写入用户目录要比写入根目录或 Program Files 目录更安全。
读取和写入文件
以下示例将一行文本写入文件。 ' Write text to a file
Sub WriteTextToFile()
Dim file As New System.IO.StreamWriter("c:\test.txt")
file.WriteLine("Here is the first line.")
file.Close()
End Sub
以下示例将文件中的文本读取到一个字符串变量中,然后将该文本写到控制台。 Sub ReadTextFromFile()
Dim file As New System.IO.StreamReader("c:\test.txt")
Dim words As String = file.ReadToEnd()
Console.WriteLine(words)
file.Close()
End Sub
以下示例在现有文件中添加文本。 Sub AppendTextToFile()
Dim file As New System.IO.StreamWriter("c:\test.txt", True)
file.WriteLine("Here is another line.")
file.Close()
End Sub
以下示例一次从文件中读取一行,然后将每行文本打印到控制台。 Sub ReadTextLinesFromFile()
Dim file As New System.IO.StreamReader("c:\test.txt")
Dim oneLine As String
oneLine = file.ReadLine()
While (oneLine <> "")
Console.WriteLine(oneLine)
oneLine = file.ReadLine()
End While
file.Close()
End Sub
文件编码
默认情况下,StreamReader 和 StreamWriter 类都使用 UTF-8 编码。UTF-8 编码正确处理 Unicode 字符并确保操作系统的本地化版本之间保持一致。可使用 StreamReader 自动检测文件的编码,或者将文件的编码指定为构造函数上的参数。StreamWriter 在其构造函数上采用一个编码参数。如果指定编码,则 Visual Basic 写入文件以指示所使用的编码。