实验六31011134倪庆雨

上传人:飞*** 文档编号:44405323 上传时间:2018-06-09 格式:DOC 页数:12 大小:61KB
返回 下载 相关 举报
实验六31011134倪庆雨_第1页
第1页 / 共12页
实验六31011134倪庆雨_第2页
第2页 / 共12页
实验六31011134倪庆雨_第3页
第3页 / 共12页
实验六31011134倪庆雨_第4页
第4页 / 共12页
实验六31011134倪庆雨_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《实验六31011134倪庆雨》由会员分享,可在线阅读,更多相关《实验六31011134倪庆雨(12页珍藏版)》请在金锄头文库上搜索。

1、实验六实验六 字节流字节流【实验目的实验目的】1对文件进行字符、字节和字符串读写操作。 2复制文件。3查找有关文件和目录的信息【实验准备实验准备】一、复习配套教材相关章节的内容;二、预习本次实验;【实验内容实验内容】1编写一个程序,读取文件 test1.txt 的内容并在控制台输出。如果源文件不存在,则显示相应的错误信息。Test1.txtTest1.txt 的内容为:的内容为:ChineseChinese isis thethe typetype ofof languagelanguagepackagepackage fi;fi;importimport java.io.File;java.

2、io.File;importimport java.io.FileInputStream;java.io.FileInputStream;importimport java.io.FileNotFoundException;java.io.FileNotFoundException;importimport java.io.IOException;java.io.IOException;publicpublic classclass FileInputStreamTestFileInputStreamTest /*/* * paramparam argsargs*/*/publicpublic

3、 staticstatic voidvoid main(Stringmain(String args)args) / TODOTODO Auto-generatedAuto-generated methodmethod stubstubtrytryFileFile f=newf=new File(“test1.txt“);/test1.txtFile(“test1.txt“);/test1.txt 放在放在 srcsrc 同同级目录级目录FileInputStreamFileInputStream fis=newfis=new FileInputStream(f);FileInputStrea

4、m(f);intint i=0;i=0;while(i=fis.read()!=-1)while(i=fis.read()!=-1)System.out.print(char)i);System.out.print(char)i); catchcatch (FileNotFoundException(FileNotFoundException e)e) / TODOTODO Auto-generatedAuto-generated catchcatch blockblockSystem.out.println(“System.out.println(“文件不存在!文件不存在!“);“);cat

5、chcatch (IOException(IOException e)e) / TODOTODO Auto-generatedAuto-generated catchcatch blockblockSystem.out.println(“System.out.println(“文件读取异常文件读取异常“);“); 运行结果:运行结果:Chinese is the type of language2、编写一个程序实现如下功能,从当前目录下的文件 fin.txt 中读取 80 个字节(实际读到的字节数可能比 80 少)并将读来的字节写入当前目录下的文件 fout.txt 中。package s;i

6、mport java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;public class ReadWrite /* param args*/public static void main(String args) / TODO Auto-generated method stubtryFile f=new File(“fin.txt“);/将 fin.txt 放入 src的同级目录File f1=new File(“fou

7、nt.txt“);/将 fount.txt 放入src 的同级目录FileInputStream fis=new FileInputStream(f);FileOutputStream fos=new FileOutputStream(f1);int i=0;for(int j=0;j80;j+)while(i=fis.read()!=-1)System.out.println(char)i);fos.write(char)i);catch(FileNotFoundException e1)System.out.println(“文件不存在“);catch (Exception e) / TO

8、DO: handle exceptionSystem.out.println(“文件读写异常“);【总结与体会总结与体会】实验七实验七 字符流字符流【实验目的实验目的】1对文件进行字符、字节和字符串读写操作。 2复制文件。3查找有关文件和目录的信息【实验准备实验准备】一、复习配套教材相关章节的内容;二、预习本次实验;【实验内容实验内容】1使用 Java 的输入/输出流技术将一个文本文件的内容按行读出,每读出一行就顺序添加行号,并写入到另一个文件中。 packagepackage s;importimport java.io.BufferedReader; importimport java.i

9、o.BufferedWriter; importimport java.io.FileNotFoundException; importimport java.io.FileReader; importimport java.io.FileWriter; importimport java.io.IOException;publicpublic classclass FileRead_Writer /* paramparam args*/ publicpublic staticstatic voidvoid main(String args) / TODOTODO Auto-generated

10、 method stubtrytry FileReader fileReader = newnew FileReader(“fin.txt“); FileWriter fileWriter = newnew FileWriter(“fout.txt“);BufferedReader bf = newnew BufferedReader(fileReader);BufferedWriter bw = newnew BufferedWriter(fileWriter);String line = bf.readLine(); whilewhile(line != nullnull) bw.writ

11、e(line); line = bf.readLine(); bf.close(); bw.close(); catchcatch (FileNotFoundException e) e.printStackTrace();System.out.print(“test.txt (系统找不到指定的文件。)“); catchcatch (IOException e) e.printStackTrace();2编写一个程序,接收从键盘输入的数据,并把从键盘输入的内容写到 input.txt 文件中,如果输入“quit”则程序结束。 packagepackage s;importimport java

12、.io.BufferedWriter; importimport java.io.FileNotFoundException; importimport java.io.FileWriter; importimport java.io.IOException; importimport java.util.Scanner;publicpublic classclass Jieshu /* paramparam args*/ publicpublic staticstatic voidvoid main(String args) / TODOTODO Auto-generated method

13、stub trytry FileWriter fileWriter = newnew FileWriter(“fout.txt“); BufferedWriter bw = newnew BufferedWriter(fileWriter);Scanner input = newnew Scanner(System.in);String stop = “start“;whilewhile(!stop.equals(“quit“) stop = input.next(); bw.write(stop); bw.close(); catchcatch (FileNotFoundException e) e.printStackTrace();System.out.print(“test.txt (系统找不到指定的文件。)“); catchcatch (IOException e) e.printStackTrace();3、编写一个程序实现如下功能,文件 fin.txt 是无行结构(无换行符)的汉语文件,从 fin 中读取字符,写入文件 fou.txt 中,每 40个字符一行(最后一行可能少于 40 个字)。【总结与体会总结与体会】

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号