3月3号 阶段性考试 IO
3月3号
阶段性考试(考试6个小时,讲评+重写6个小时):
记录完成每一题所需要的时长。
1、将a.jpg的文件字节与b.jpg文件文件字节合并为一个文件c.jpg。也就是c.jpg中文件的前一部分是a.jpg的内容,后一部分是b.jpg的内容。不用管生成的c.jpg是否是合法的图片。
2、编写一个程序,将d:\code目录下的所有.jpg文件复制到d:\code2目录下,并将文件的扩展名从.jpg改为.bmp(不用进行文件格式转换)。
3、读取一个文本文件,统计其中数字字符的个数。
4、一个文本文件含有如下内容,分别表示姓名和成绩:
张三 90
李四 96
王五 78
赵六 82
用户输入要查询的姓名,打印出此人的成绩,如果不输入姓名直接按回车则显示所有人的姓名以及成绩。注意:这个文本文件的行数可能会变,而且文件可能会非常大。
1 1、 2 [code] 3 using System; 4 using System.Collections.Generic; 5 using System.IO; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 10 namespace PictureMerge 11 { 12 class Program 13 { 14 static void Main(string[] args) 15 { 16 using (Stream stream1 = File.Open(@"g:\a.jpg", FileMode.Open)) 17 using (Stream stream2 = File.Open(@"g:\b.jpg", FileMode.Open)) 18 using (Stream stream3 = File.Open(@"f:\c.jpg", FileMode.Create)) 19 { 20 byte[] bytes = new byte[1024 * 1024];//设定一个缓存为1Mb的字节数组 21 int len; 22 while ((len = stream1.Read(bytes,0,bytes.Length)) > 0) 23 { 24 stream3.Write(bytes, 0, len); 25 } 26 while ((len = stream2.Read(bytes, 0, bytes.Length)) > 0) 27 { 28 stream3.Write(bytes, 0, len); 29 } 30 31 } 32 Console.WriteLine("已合并!"); 33 Console.ReadKey(); 34 } 35 } 36 }[/code]
2、编写一个程序,将d:\code目录下的所有.jpg文件复制到d:\code2目录下,并将文件的扩展名从.jpg改为.bmp(不用进行文件格式转换)。
2, [code] using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CopyJpgConvertToBmp { class Program { static void Main(string[] args) { string DestDir = @"f:\"; string[] JpgFiles = Directory.GetFiles(@"g:\","*jpg"); foreach (string JpgFile in JpgFiles) { string FileName = Path.GetFileNameWithoutExtension(JpgFile); string DestFileName = DestDir + @"\" + FileName + ".bmp"; //File.Copy(JpgFile, DestFileName);//File.Copy是系统内置的,,也可以自己写方法CopyFile如下 CopyFile(JpgFile, DestFileName); } Console.WriteLine("完成"); Console.ReadKey(); } /// <summary> /// 将一个文件拷贝到另一个文件中去 /// </summary> /// <param name="srcFile">源文件</param> /// <param name="destFile">目标文件</param> public static void CopyFile(string srcFile,string destFile) { using(Stream src=File.Open(srcFile,FileMode.Open)) using(Stream dest=File.Open(destFile,FileMode.Create)) { byte[] bytes = new byte[1024 * 1024];//设置缓存大小 int len; while ((len = src.Read(bytes, 0, bytes.Length)) > 0) { dest.Write(bytes, 0, len); } } } } } [/code]
3、读取一个文本文件,统计其中数字字符的个数。
3、 [code] using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CountDigital { class Program { static void Main(string[] args) { using (Stream stream = File.OpenRead(@"g:\1.txt")) using (StreamReader reader = new StreamReader(stream,Encoding.Default))//使其以一种特定的编码从字节流中读取字符 { int count = 0; int i; while(( i=reader.Read())!=-1)//char的最小值是0..-1说明已经读取完了char { char c=(char)i; if (c >= '0' && c <='9') { count++; } Console.WriteLine(c); } Console.WriteLine("其中数字的个数是" + count); } Console.ReadKey(); } } } [/code]
4、一个文本文件含有如下内容,分别表示姓名和成绩:
张三 90
李四 96
王五 78
赵六 82
用户输入要查询的姓名,打印出此人的成绩,如果不输入姓名直接按回车则显示所有人的姓名以及成绩。注意:这个文本文件的行数可能会变,而且文件可能会非常大。
4、 [code] using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StudentScoreCheck { class Program { static void Main(string[] args) { Console.WriteLine("请输入查询的姓名:"); string NameToQuery = Console.ReadLine(); bool IsFound = false; using(Stream streamStu=File.OpenRead(@"g:\10.txt")) using (StreamReader reader = new StreamReader(streamStu, Encoding.Default)) { string line; while ((line = reader.ReadLine()) != null) { string[] strs = line.Split(' '); if (string.IsNullOrEmpty(NameToQuery))//返回结果: // 如果 value 参数为 null 或空字符串 (""),则为 true;否则为 false。 { Console.WriteLine(strs[0] + "的成绩是" + strs[1]); } else { if (strs[0] == NameToQuery) { IsFound = true;//重新赋值为true Console.WriteLine(strs[0] + "的成绩是" + strs[1]); break; } } } } if (IsFound == false) { Console.WriteLine("!!!没有找到" + NameToQuery + "的记录!!!"); } Console.ReadKey(); } } } [/code]
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号