文档详情

Java语言程序设计(郑莉)第六章课后习题答案(24页).doc

发布:2015-09-10约1.69万字共21页下载文档
文本预览下载声明
Java语言程序设计 第六章课后习题答案 1.将本章例6-1至6-18中出现的文件的构造方法均改为使用File类对象作为参数实现。 个人理解:File类只能对整文件性质进行处理,而没法通过自己直接使用file.Read()或者是file.write()类似方法对文件内容进行写或者读取。注意:是直接;下面只提供一个例2变化,其他的你自己做,10几道啊,出这题的人真他妈有病。 import java.io.*; public class test6_2{ public static void main(String[] args) throws IOException { String fileName = D:\\Hello.txt; File writer=new File(fileName); writer.createNewFile(); BufferedWriter input = new BufferedWriter(new FileWriter(writer)); input.write(Hello !\n); input.write(this is my first text file,\n); input.write(你还好吗?\n); input.close(); } } 运行结果:(电脑系统问题,没法换行,所以一般使用BuffereWriter中newLine()实现换行) 2.模仿文本文件复制的例题,编写对二进制文件进行复制的程序. // CopyMaker类 import java.io.*; class CopyMaker { String sourceName, destName; BufferedInputStream source; BufferedOutputStream dest; int line; //打开源文件和目标文件,无异常返回true private boolean openFiles() { try { source = new BufferedInputStream(new FileInputStream( sourceName )); } catch ( IOException iox ) { System.out.println(Problem opening + sourceName ); return false; } try { dest = new BufferedOutputStream(new FileOutputStream( destName )); } catch ( IOException iox ) { System.out.println(Problem opening + destName ); return false; } return true; } //复制文件 private boolean copyFiles() { try { line = source.read(); while ( line != -1 ) { dest.write(line); line = source.read(); } } catch ( IOException iox ) { System.out.println(Problem reading or writing ); return false; } return true; } //关闭源文件和目标文件 private boolean closeFiles() { boolean retVal=true; try { source.close(); } ca
显示全部
相似文档