1、高级程序设计实 验 报 告班级学号:姓名:实验名称:指导老师:日期:实验十一 文件和流I/O一、实验题目:文件和流I/O二、实验内容:1掌握磁盘的基本操作2掌握目录的基本操作3文件的基本操作4文本文件的读取和写入 5字符串的读取和写入6二进制文件的读取和写入I7. 二进制文件的读取和写入2三、主要程序代码:1. using System;using System.IO;using System.Collections.Generic;using System.Text;namespace 实验1class Test public static void Main() DriveInfo all
2、Drives = DriveInfo.GetDrives(); foreach (DriveInfo d in allDrives) Console.WriteLine(Drive 0, d.Name); Console.WriteLine( File type: 0, d.DriveType); if (d.IsReady = true) Console.WriteLine( Volume label: 0, d.VolumeLabel); Console.WriteLine( File system: 0, d.DriveFormat); Console.WriteLine( Availa
3、ble space to current user:0, 15 bytes,d.AvailableFreeSpace); Console.WriteLine(Total available space: 0, 15 bytes,d.TotalFreeSpace); Console.WriteLine( Total size of drive: 0, 15 bytes , d.TotalSize); Console.ReadKey(); 2. using System;using System.IO;namespace 实验11 class Program static void Main(st
4、ring args) String path0 = C:WINDOWSsecurity; String path1 = C:WINDOWS; try Console.WriteLine(应用程序当前工作目录为:n 0,Directory.GetCurrentDirectory(); string subdirectotyEntries =Directory.GetDirectories(Directory.GetDirectoryRoot(Directory.GetCurrentDirectory(); Console.WriteLine(根目录中子目录总数为0,子目录名称分别为:,subdi
5、rectotyEntries.Length); foreach (string subdirectory in subdirectotyEntries) Console.WriteLine( 0, subdirectory); string dirs =Directory.GetFiles(path0); Console.WriteLine(目录0下的总文件总数为 1。文件名称分别为:, path0, dirs.Length); foreach (string dir in dirs) Console.WriteLine( 0, dir); string dirs1 = Directory.G
6、etFiles(path1, *.ini); Console.WriteLine(目录0下拓展名为.sys的文件总数为 1。文件名称分别为:,path1, dirs1.Length); foreach (string dir in dirs1) Console.WriteLine( 0, dir); catch (Exception err) Console.WriteLine(n操作失败: 0, err.Message); Console.ReadLine(); 3 using System;using System.IO;using System.Collections.Generic;u
7、sing System.Text;namespace 文件的基本操作 class Program static void Main(string args) string s; string path = c:tempSrcFile.txt; FileInfo fi1 = new FileInfo(path); if (!fi1.Exists) using (StreamWriter sw = fi1.CreateText() bool bContinue = true; while (bContinue) Console.Write(请输入文件内容: ); s = Console.ReadL
8、ine(); if (s != ) sw.WriteLine(s); else bContinue = false; fi1 = new FileInfo(path); Console.WriteLine(源文件 0所在的目录为:nt1, path, fi1.DirectoryName); Console.WriteLine(源文件 0长度为:nt1, path, fi1.Length); Console.WriteLine(源文件 0内容为: , path); using (StreamReader sr = fi1.OpenText() s = ; while (s = sr.ReadLi
9、ne() != null) Console.WriteLine(t0, s); try string path2 = c:tempDesFile.txt; FileInfo fi2 = new FileInfo(path2); fi2.Delete(); fi1.CopyTo(path2); Console.WriteLine(源文件成功复制至目标文件:nt0, path2); fi2.Delete(); Console.WriteLine(目标文件0成功删除, path2); catch (Exception e) Console.WriteLine(n操作失败:0, e.ToString(
10、); Console.ReadKey(); 4using System;using System.IO;using System.Collections.Generic;using System.Text;namespace 文本文件的读取和写入 class Program private const string FILE_NAME = c:tempTestFile.txt; static void Main(string args) using (StreamWriter sw = new StreamWriter(FILE_NAME) sw.WriteLine(文本文件的写入/读取示例:
11、); sw.WriteLine(.); sw.WriteLine(写入正数、浮点数、Boolean值、字符、字符串、日期); Console.WriteLine(请输入整数: ); int i = int.Parse(Console.ReadLine(); sw.WriteLine(i); Console.WriteLine(请输入浮点数: ); float f = float.Parse(Console.ReadLine(); sw.WriteLine(f); Console.WriteLine(请输入Boolean值: ); bool b = bool.Parse(Console.Read
12、Line(); sw.WriteLine(b); Console.WriteLine(请输入字符: ); char c = char.Parse(Console.ReadLine(); sw.WriteLine(c); Console.WriteLine(请输入字符串: ); string s = (Console.ReadLine(); sw.WriteLine(s); sw.Write(当前日期为: ); sw.WriteLine(DateTime.Now); try using (StreamReader sr = new StreamReader(FILE_NAME) String l
13、ine; while (line = sr.ReadLine() != null) Console.WriteLine(line); catch (Exception e) Console.WriteLine(该文件不能读取,原因如下:); Console.WriteLine(e.Message); Console.ReadKey(); 5using System;using System.IO;using System.Collections.Generic;using System.Text;namespace 字符串的读取和写入 class Program static void Mai
14、n(string args) string s, textReaderText = ; bool bContinue = true; while (bContinue) Console.Write(请输入文件内容(以 结束):); s = Console.ReadLine(); if (s != ) textReaderText = textReaderText + s + n; else bContinue = false; Console.WriteLine(原始文本内容如下 :); Console.WriteLine(.n0, textReaderText); string aLine,
15、 aParagraph = null; StringReader d = new StringReader(textReaderText); while (true) aLine = d.ReadLine(); if (aLine != null) aParagraph = aParagraph + aLine + ; else aParagraph = aParagraph + n; break; Console.WriteLine(修改后的文本(连续的段落aParagraph)内容如下:); Console.WriteLine(-n0, aParagraph); int a; Char b
16、; StringWriter c = new StringWriter(); d= new StringReader(aParagraph); while (true) a= d.Read(); if (a= -1) break; b = Convert.ToChar(a); if (b= ) c.Write(.n); d.Read(); else c.Write(b); Console.WriteLine(n还原后的原始文本内容textReaderText:); Console.WriteLine(-n0, c.ToString(); Console.ReadKey(); 6 using S
17、ystem;using System.IO;using System.Collections.Generic;using System.Text;namespace 二进制文件的读取和写入 class Program static void Main(string args) string path = c:tempMyTest.txt; if (File.Exists(path) File.Delete(path); using (FileStream fs = File.Create(path) AddText(fs, This is some text.); AddText(fs, Th
18、is is some more text.); AddText(fs, rnand This is on a new line.); AddText(fs, rn以下是1-100之间的偶数:rn); int j = 0; for (int i = 1; i 0) Console.WriteLine(temp.GetString(b); Console.ReadKey(); private static void AddText(FileStream fs, string value) BinaryReader sb = new BinaryReader(fs, Encoding.Default
19、); byte info = new UTF8Encoding(true).GetBytes(value); fs.Write(info, 0, info.Length); 7using System;using System.IO;using System.Collections.Generic;using System.Text;namespace 二进制文件的读取和写入 class program private const string FILE_NAME = c:tempTest.data; static void Main(string args) if (File.Exists(
20、FILE_NAME) Console.WriteLine(文件0已经存在,删除之!, FILE_NAME); File.Delete(FILE_NAME); FileStream fs = new FileStream(FILE_NAME, FileMode.CreateNew); BinaryWriter w = new BinaryWriter(fs); for (int i = 0; i 128; i+) w.Write(int)i); for (int i = 0; i 128; i+) w.Write(Convert.ToChar(i); w.Close(); fs.Close();
21、 fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read); BinaryReader r = new BinaryReader(fs); Console.WriteLine(0-127的整数:); for (int i = 0; i 128; i+) Console.Write(r.ReadInt32() + ); Console.WriteLine(n0-127的ASCII字符:); for (int i = 0; i 128; i+) Console.Write(r.ReadChar() + ); r.Close(); fs.Close(); Console.ReadKey(); 四、主要运行界面及运行结果:1. 23.4.5. 6. 7. 五、实验收获与心得:通过本章的实习让我更清楚的对文件和流的操作有了一个熟悉和认识,掌握了怎么用C#编写文件的基本操作,文本文件的读取和写入,字符串的读取和写入,二进制文件的读取和写入。其中第五幅图输出最后字符出现问题,还请老师解惑。12