标签:catch 工作 length 特定 同步方法 error 文件创建 文本 微软
1 using System; 2 using System.IO; 3 using System.Text; 4 5 class Test 6 { 7 8 public static void Main() 9 { 10 string path = @"c:\temp\MyTest.txt"; 11 12 // Delete the file if it exists. 13 if (File.Exists(path)) 14 { 15 File.Delete(path); 16 } 17 18 //Create the file. 19 using (FileStream fs = File.Create(path)) 20 { 21 AddText(fs, "This is some text"); 22 AddText(fs, "This is some more text,"); 23 AddText(fs, "\r\nand this is on a new line"); 24 AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n"); 25 26 for (int i=1;i < 120;i++) 27 { 28 AddText(fs, Convert.ToChar(i).ToString()); 29 30 } 31 } 32 33 //Open the stream and read it back. 34 using (FileStream fs = File.OpenRead(path)) 35 { 36 byte[] b = new byte[1024]; 37 UTF8Encoding temp = new UTF8Encoding(true); 38 while (fs.Read(b,0,b.Length) > 0) 39 { 40 Console.WriteLine(temp.GetString(b)); 41 } 42 } 43 } 44 45 private static void AddText(FileStream fs, string value) 46 { 47 byte[] info = new UTF8Encoding(true).GetBytes(value); 48 fs.Write(info, 0, info.Length); 49 } 50 }
1 using System; 2 using System.IO; 3 4 class Test 5 { 6 public static void Main() 7 { 8 try 9 { 10 // Create an instance of StreamReader to read from a file. 11 // The using statement also closes the StreamReader. 12 using (StreamReader sr = new StreamReader("TestFile.txt")) 13 { 14 String line; 15 // Read and display lines from the file until the end of 16 // the file is reached. 17 while ((line = sr.ReadLine()) != null) 18 { 19 Console.WriteLine(line); 20 } 21 } 22 } 23 catch (Exception e) 24 { 25 // Let the user know what went wrong. 26 Console.WriteLine("The file could not be read:"); 27 Console.WriteLine(e.Message); 28 } 29 } 30 }
1 using System; 2 using System.IO; 3 using System.Text; 4 5 namespace ConsoleApplication 6 { 7 class Program 8 { 9 static void Main(string[] args) 10 { 11 ReadCharacters(); 12 } 13 14 static async void ReadCharacters() 15 { 16 StringBuilder stringToRead = new StringBuilder(); 17 stringToRead.AppendLine("Characters in 1st line to read"); 18 stringToRead.AppendLine("and 2nd line"); 19 stringToRead.AppendLine("and the end"); 20 21 using (StringReader reader = new StringReader(stringToRead.ToString())) 22 { 23 string readText = await reader.ReadToEndAsync(); 24 Console.WriteLine(readText); 25 } 26 } 27 } 28 } 29 // The example displays the following output://// Characters in 1st line to read// and 2nd line// and the end//
using System; using System.IO; public class TextToFile { private const string FILE_NAME = "MyFile.txt"; public static void Main(String[] args) { if (File.Exists(FILE_NAME)) { Console.WriteLine("{0} already exists.", FILE_NAME); return; } using (StreamWriter sw = File.CreateText(FILE_NAME)) { sw.WriteLine ("This is my file."); sw.WriteLine ("I can write ints {0} or floats {1}, and so on.", 1, 4.2); sw.Close(); } } }
名称 | 说明 |
BinaryReader (Stream) | 基于所提供的流,用 UTF8Encoding 初始化 BinaryReader 类的新实例。 由 .NET Compact Framework 支持。 |
BinaryReader (Stream, Encoding) | 基于所提供的流和特定的字符编码,初始化 BinaryReader 类的新实例。 由 .NET Compact Framework 支持。 |
1 public AppSettings() 2 { 3 // Create default application settings. 4 aspectRatio = 1.3333F; 5 lookupDir = @"C:\AppDirectory"; 6 autoSaveTime = 30; 7 showStatusBar = false; 8 9 if(File.Exists(fileName)) 10 { 11 BinaryReader binReader = 12 new BinaryReader(File.Open(fileName, FileMode.Open)); 13 try 14 { 15 // If the file is not empty, 16 // read the application settings. 17 if(binReader.PeekChar() != -1) 18 { 19 aspectRatio = binReader.ReadSingle(); 20 lookupDir = binReader.ReadString(); 21 autoSaveTime = binReader.ReadInt32(); 22 showStatusBar = binReader.ReadBoolean(); 23 } 24 } 25 26 // If the end of the stream is reached before reading 27 // the four data values, ignore the error and use the 28 // default settings for the remaining values. 29 catch(EndOfStreamException e) 30 { 31 Console.WriteLine("{0} caught and ignored. " + 32 "Using default values.", e.GetType().Name); 33 } 34 finally 35 { 36 binReader.Close(); 37 } 38 } 39 40 }
如果使用FileAccess.Write,则无论FileShare是什么值其他进程都无法再次写入,不过配合合适的FileShare其他进程还是可以Read)
1 using System; 2 using System.IO; 3 class MyStream 4 { 5 private const string FILE_NAME = "Test.data"; 6 public static void Main(String[] args) 7 { 8 // Create the new, empty data file. 9 if (File.Exists(FILE_NAME)) 10 { 11 Console.WriteLine("{0} already exists!", FILE_NAME); 12 return; 13 } 14 FileStream fs = new FileStream(FILE_NAME, FileMode.CreateNew); 15 // Create the writer for data. 16 BinaryWriter w = new BinaryWriter(fs); 17 // Write data to Test.data. 18 for (int i = 0; i < 11; i++) 19 { 20 w.Write( (int) i); 21 } 22 w.Close(); 23 fs.Close(); 24 // Create the reader for data. 25 fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read); 26 BinaryReader r = new BinaryReader(fs); 27 // Read data from Test.data. 28 for (int i = 0; i < 11; i++) 29 { 30 Console.WriteLine(r.ReadInt32()); 31 } 32 r.Close(); 33 fs.Close(); 34 } 35 }
标签:catch 工作 length 特定 同步方法 error 文件创建 文本 微软
原文地址:http://www.cnblogs.com/lulianqi/p/6187625.html