System.IO全面总结(二)
2008-08-28 17:44
281 查看
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace StreamReader类
{
class Program
{
//static void Main(string[] args)
//{
//}
public static void Main()
{
string path = @"c: empMyTest.txt";
try
{
if (File.Exists(path))
{
File.Delete(path);
}
using (StreamWriter sw = new StreamWriter(path))//path 参数可以是文件名,包括统一命名约定 (UNC) 共享上的文件。如果此文件已存在,将覆盖它;否则,将创建一个新文件。
{ //不要求 path 参数必须是存储在磁盘上的文件;它可以是系统中支持通过流进行访问的任何部分。
sw.WriteLine("This"); //WriteLine方法是继承TextWriter基类的
sw.WriteLine("is some text");
sw.WriteLine("to test");
sw.WriteLine("Reading");
}
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0) //Peek方法返回下一个可用的字符,但不使用它。次方法的返回值:下一个要读取的字符,如果没有更多的可用字符或此流不支持查找,则为 -1。
{
Console.WriteLine(sr.ReadLine()); //ReadLine方法是重写基类TextRead的
}
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
}
/*
StreamReader.ReadLine 方法 :从当前流中读取一行字符并将数据作为字符串返回。此方法的返回值:输入流中的下一行;如果到达了输入流的末尾,则为空引用(在VB中为Nothing,C#中为null)。
----继承关系:
System.Object
System.MarshalByRefObject
System.IO.TextReader
System.IO.StreamReader
System.IO.StringReader
----继承关系:
System.Object
System.MarshalByRefObject
System.IO.TextWriter
System.CodeDom.Compiler.IndentedTextWriter
System.IO.StreamWriter
System.IO.StringWriter
System.Web.HttpWriter
System.Web.UI.HtmlTextWriter
StreamReader的构造函数必须要求指定文件必须已经存在,如果不存在则会发生异常
StreamWriter的构造函数不要求指定的文件必须已经存在,如果不存在则会创建该文件,如果存在则会改写或者追加,这要看你用那一个构造函数而定
StreamReader中的Close,Peek,Read,ReadLine,ReadToEnd都是重写基类TextRead中的虚拟方法
StreamWriter中的Close,Flush,Write是重写基类TextWriter中的虚拟方法,而WriteLine是继承基类TextWriter(虚拟的)中的方法
一般在用完一个流之后要用Close关闭这个流,例如StreamReader和StreamWriter
*/
//using System;
//using System.IO;
//using System.Text;
//class Test
//{
// public static void Main()
// {
// string path = @"c: empMyTest.txt";
// try
// {
// // Delete the file if it exists.
// if (File.Exists(path))
// {
// // Note that no lock is put on the
// // file and the possibliity exists
// // that another process could do
// // something with it between
// // the calls to Exists and Delete.
// File.Delete(path);
// }
// // Create the file.
// using (FileStream fs = File.Create(path))
// {
// Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
// // Add some information to the file.
// fs.Write(info, 0, info.Length);
// }
// // Open the stream and read it back.
// using (StreamReader sr = File.OpenText(path)) //File.OpenText (string Path)//返回类型为SteamReader,打开现有UTF-8编码文本文件以进行读取
// {
// string s = "";
// while ((s = sr.ReadLine()) != null)
// {
// Console.WriteLine(s);
// }
// }
// }
// catch (Exception Ex)
// {
// Console.WriteLine(Ex.ToString());
// }
// }
//}
//using System;
//using System.IO;
//class Test
//{
// public static void Main()
// {
// string path = @"c: empMyTest.txt";
// if (!File.Exists(path))
// {
// // Create a file to write to.
// using (StreamWriter sw = File.CreateText(path))
// {
// sw.WriteLine("Hello");
// sw.WriteLine("And");
// sw.WriteLine("Welcome");
// }
// }
// // Open the file to read from.
// using (StreamReader sr = File.OpenText(path))
// {
// string s = "";
// while ((s = sr.ReadLine()) != null)
// {
// Console.WriteLine(s);
// }
// }
// }
//}
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace StreamReader类
{
class Program
{
//static void Main(string[] args)
//{
//}
public static void Main()
{
string path = @"c: empMyTest.txt";
try
{
if (File.Exists(path))
{
File.Delete(path);
}
using (StreamWriter sw = new StreamWriter(path))//path 参数可以是文件名,包括统一命名约定 (UNC) 共享上的文件。如果此文件已存在,将覆盖它;否则,将创建一个新文件。
{ //不要求 path 参数必须是存储在磁盘上的文件;它可以是系统中支持通过流进行访问的任何部分。
sw.WriteLine("This"); //WriteLine方法是继承TextWriter基类的
sw.WriteLine("is some text");
sw.WriteLine("to test");
sw.WriteLine("Reading");
}
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0) //Peek方法返回下一个可用的字符,但不使用它。次方法的返回值:下一个要读取的字符,如果没有更多的可用字符或此流不支持查找,则为 -1。
{
Console.WriteLine(sr.ReadLine()); //ReadLine方法是重写基类TextRead的
}
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
}
/*
StreamReader.ReadLine 方法 :从当前流中读取一行字符并将数据作为字符串返回。此方法的返回值:输入流中的下一行;如果到达了输入流的末尾,则为空引用(在VB中为Nothing,C#中为null)。
----继承关系:
System.Object
System.MarshalByRefObject
System.IO.TextReader
System.IO.StreamReader
System.IO.StringReader
----继承关系:
System.Object
System.MarshalByRefObject
System.IO.TextWriter
System.CodeDom.Compiler.IndentedTextWriter
System.IO.StreamWriter
System.IO.StringWriter
System.Web.HttpWriter
System.Web.UI.HtmlTextWriter
StreamReader的构造函数必须要求指定文件必须已经存在,如果不存在则会发生异常
StreamWriter的构造函数不要求指定的文件必须已经存在,如果不存在则会创建该文件,如果存在则会改写或者追加,这要看你用那一个构造函数而定
StreamReader中的Close,Peek,Read,ReadLine,ReadToEnd都是重写基类TextRead中的虚拟方法
StreamWriter中的Close,Flush,Write是重写基类TextWriter中的虚拟方法,而WriteLine是继承基类TextWriter(虚拟的)中的方法
一般在用完一个流之后要用Close关闭这个流,例如StreamReader和StreamWriter
*/
//using System;
//using System.IO;
//using System.Text;
//class Test
//{
// public static void Main()
// {
// string path = @"c: empMyTest.txt";
// try
// {
// // Delete the file if it exists.
// if (File.Exists(path))
// {
// // Note that no lock is put on the
// // file and the possibliity exists
// // that another process could do
// // something with it between
// // the calls to Exists and Delete.
// File.Delete(path);
// }
// // Create the file.
// using (FileStream fs = File.Create(path))
// {
// Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
// // Add some information to the file.
// fs.Write(info, 0, info.Length);
// }
// // Open the stream and read it back.
// using (StreamReader sr = File.OpenText(path)) //File.OpenText (string Path)//返回类型为SteamReader,打开现有UTF-8编码文本文件以进行读取
// {
// string s = "";
// while ((s = sr.ReadLine()) != null)
// {
// Console.WriteLine(s);
// }
// }
// }
// catch (Exception Ex)
// {
// Console.WriteLine(Ex.ToString());
// }
// }
//}
//using System;
//using System.IO;
//class Test
//{
// public static void Main()
// {
// string path = @"c: empMyTest.txt";
// if (!File.Exists(path))
// {
// // Create a file to write to.
// using (StreamWriter sw = File.CreateText(path))
// {
// sw.WriteLine("Hello");
// sw.WriteLine("And");
// sw.WriteLine("Welcome");
// }
// }
// // Open the file to read from.
// using (StreamReader sr = File.OpenText(path))
// {
// string s = "";
// while ((s = sr.ReadLine()) != null)
// {
// Console.WriteLine(s);
// }
// }
// }
//}
相关文章推荐
- System.IO全面总结(一)
- System.IO全面总结(二)
- System.IO全面总结(一)
- System.IO全面总结(一
- System.IO全面总结(二)
- IO多路复用之epoll全面总结(必看篇)
- 黑马程序员——7.1.IO(其他对象API(System类、Runtime类、Math类、Date类)字节流与字符流、IO异常处理、缓冲区、键盘录入、转换流、异常日志、流的操作总结)
- 接口java.io.Serializable的用处 (全面搜集总结)
- 接口java.io.Serializable的用处 (全面搜集总结)
- JavaIO总结
- 网络IO之阻塞、非阻塞、同步、异步总结
- java.io.IOException: No FileSystem for scheme: hdfs
- 市场数据系统(MDS, Market Data System)设计总结二三点
- 捕获报错的方法try +catch +System.IO.File.AppendAllText+Exception e.StackTrace
- Visual C++开发工具与调试技巧全面总结
- Android 中的 Service 全面总结
- 高级IO学习总结2
- System.IO之内存映射文件共享内存
- Android SystemProperties设置/取得系统属性的用法总结
- Java IO使用总结