概述
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:/temp/MyTest.txt";
try
{
if (File.Exists(path))
{
File.Delete(path);
}
using (StreamWriter sw = new StreamWriter(path))
{
sw.WriteLine("This");
sw.WriteLine("is some text");
sw.WriteLine("to test");
sw.WriteLine("Reading");
}
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
Console.Write((char)sr.Read());
}
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
下面的代码示例演示如何使用 Read()()() 方法重载读取单个字符,并将 ASCII 整数的输出格式设置为十进制和十六进制数。
using System;
using System.IO;
class StrmRdrRead
{
public static void Main()
{
//Create a FileInfo instance representing an existing text file.
FileInfo MyFile=new FileInfo(@"c:/csc.txt");
//Instantiate a StreamReader to read from the text file.
StreamReader sr=MyFile.OpenText();
//Read a single character.
int FirstChar=sr.Read();
//Display the ASCII number of the character read in both decimal and hexadecimal format.
Console.WriteLine("The ASCII number of the first character read is {0:D} in decimal and {1:X} in hexadecimal.",
FirstChar, FirstChar);
//
sr.Close();
}
}
最后
以上就是唠叨曲奇为你收集整理的 StreamReader 的用法(C#)的全部内容,希望文章能够帮你解决 StreamReader 的用法(C#)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复