收藏 分销(赏)

高级程序设计实验报告.doc

上传人:a199****6536 文档编号:2128745 上传时间:2024-05-17 格式:DOC 页数:12 大小:154.53KB
下载 相关 举报
高级程序设计实验报告.doc_第1页
第1页 / 共12页
高级程序设计实验报告.doc_第2页
第2页 / 共12页
点击查看更多>>
资源描述
高级程序设计 实 验 报 告 班级 学号: 姓名: 实验名称: 指导老师: 日期: 实验十一 文件和流I/O 一、实验题目:文件和流I/O 二、实验内容: 1.掌握磁盘的基本操作 2.掌握目录的基本操作 3.文件的基本操作 4.文本文件的读取和写入 5.字符串的读取和写入 6.二进制文件的读取和写入I 7. 二进制文件的读取和写入2 三、主要程序代码: 1. using System; using System.IO; using System.Collections.Generic; using System.Text; namespace 实验1 { class Test { public static void Main() { DriveInfo[] allDrives = 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(" Available 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(string[] args) { String path0 = @"C:\WINDOWS\security"; String path1 = @"C:\WINDOWS"; try{ Console.WriteLine("应用程序当前工作目录为:\n {0}",Directory.GetCurrentDirectory()); string [] subdirectotyEntries =Directory.GetDirectories(Directory.GetDirectoryRoot(Directory.GetCurrentDirectory())); Console.WriteLine("根目录中子目录总数为{0},子目录名称分别为:",subdirectotyEntries.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.GetFiles(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; using System.Text; namespace 文件的基本操作 { class Program { static void Main(string[] args) { string s; string path = @"c:\temp\SrcFile.txt"; FileInfo fi1 = new FileInfo(path); if (!fi1.Exists) { using (StreamWriter sw = fi1.CreateText()) { bool bContinue = true; while (bContinue) { Console.Write("请输入文件内容: "); s = Console.ReadLine(); if (s != "") sw.WriteLine(s); else bContinue = false; } } } fi1 = new FileInfo(path); Console.WriteLine("源文件 {0}所在的目录为:\n\t{1}", path, fi1.DirectoryName); Console.WriteLine("源文件 {0}长度为:\n\t{1}", path, fi1.Length); Console.WriteLine("源文件 {0}内容为: ", path); using (StreamReader sr = fi1.OpenText()) { s = ""; while ((s = sr.ReadLine()) != null) Console.WriteLine("\t{0}", s); } try { string path2 = @"c:\temp\DesFile.txt"; FileInfo fi2 = new FileInfo(path2); fi2.Delete(); fi1.CopyTo(path2); Console.WriteLine("源文件成功复制至目标文件:\n\t{0}", path2); fi2.Delete(); Console.WriteLine("目标文件{0}成功删除", path2); } catch (Exception e) { Console.WriteLine("\n操作失败:{0}", e.ToString()); } Console.ReadKey(); } } } 4.using System; using System.IO; using System.Collections.Generic; using System.Text; namespace 文本文件的读取和写入 { class Program { private const string FILE_NAME = @"c:\temp\TestFile.txt"; static void Main(string[] args) { using (StreamWriter sw = new StreamWriter(FILE_NAME)) { sw.WriteLine("文本文件的写入/读取示例:"); 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.ReadLine()); 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 line; while ((line = sr.ReadLine()) != null) Console.WriteLine(line); } } catch (Exception e) { Console.WriteLine("该文件不能读取,原因如下:"); Console.WriteLine(e.Message); } Console.ReadKey(); } } 5.using System; using System.IO; using System.Collections.Generic; using System.Text; namespace 字符串的读取和写入 { class Program { static void Main(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(".........\n{0}", textReaderText); string aLine, 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("-------------\n{0}", aParagraph); int a; Char b; 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("-----------\n{0}", c.ToString()); Console.ReadKey(); } } } 6. using System; using System.IO; using System.Collections.Generic; using System.Text; namespace 二进制文件的读取和写入 { class Program { static void Main(string[] args) { string path = @"c:\temp\MyTest.txt"; if (File.Exists(path)) File.Delete(path); 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以下是1-100之间的偶数:\r\n"); int j = 0; for (int i = 1; i <= 100; i++) { if (i % 2 == 0) { AddText(fs, i.ToString()+ " "); j++; } if (j % 10 == 0) AddText(fs, "\r\n"); } } 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)); } Console.ReadKey(); } private static void AddText(FileStream fs, string value) { BinaryReader sb = new BinaryReader(fs, Encoding.Default); byte[] info = new UTF8Encoding(true).GetBytes(value); fs.Write(info, 0, info.Length); } } }      7. using System; using System.IO; using System.Collections.Generic; using System.Text; namespace 二进制文件的读取和写入 { class program { private const string FILE_NAME = @"c:\temp\Test.data"; static void Main(string[] args) { if (File.Exists(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(); 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. 2 3. 4.. 5. 6. 7. 五、实验收获与心得: 通过本章的实习让我更清楚的对文件和流的操作有了一个熟悉和认识,掌握了怎么用C#编写文件的基本操作,文本文件的读取和写入,字符串的读取和写入,二进制文件的读取和写入。其中第五幅图输出最后字符出现问题,还请老师解惑。 12
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 研究报告 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服