java答案第六节.doc

  1. 1、本文档共20页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
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

文档评论(0)

xuefei111 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档