资源预览内容
第1页 / 共12页
第2页 / 共12页
第3页 / 共12页
第4页 / 共12页
第5页 / 共12页
第6页 / 共12页
第7页 / 共12页
第8页 / 共12页
第9页 / 共12页
第10页 / 共12页
亲,该文档总共12页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
实验六实验六 字节流字节流【实验目的实验目的】1对文件进行字符、字节和字符串读写操作。 2复制文件。3查找有关文件和目录的信息【实验准备实验准备】一、复习配套教材相关章节的内容;二、预习本次实验;【实验内容实验内容】1编写一个程序,读取文件 test1.txt 的内容并在控制台输出。如果源文件不存在,则显示相应的错误信息。Test1.txtTest1.txt 的内容为:的内容为:ChineseChinese isis thethe typetype ofof languagelanguagepackagepackage fi;fi;importimport java.io.File;java.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 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);FileInputStream(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(“文件不存在!文件不存在!“);“);catchcatch (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;import 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(“fount.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) / TODO: handle exceptionSystem.out.println(“文件读写异常“);【总结与体会总结与体会】实验七实验七 字符流字符流【实验目的实验目的】1对文件进行字符、字节和字符串读写操作。 2复制文件。3查找有关文件和目录的信息【实验准备实验准备】一、复习配套教材相关章节的内容;二、预习本次实验;【实验内容实验内容】1使用 Java 的输入/输出流技术将一个文本文件的内容按行读出,每读出一行就顺序添加行号,并写入到另一个文件中。 packagepackage s;importimport java.io.BufferedReader; importimport java.io.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 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.write(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.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 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 个字)。【总结与体会总结与体会】
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号