1、Java语言程序设计第六章课后习题答案1.将本章例6-1至6-18中出现的文件的构造方法均改为使用File类对象作为参数实现。个人理解:File类只能对整文件性质进行处理,而没法通过自己直接使用file.Read()或者是file.write()类似方法对文件内容进行写或者读取。注意:是直接;下面只提供一个例2变化,其他的你自己做,10几道啊,出这题的人真他妈有病。import java.io.*;public class test6_2public static void main(String args) throws IOException String fileName = D:Hel
2、lo.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.模仿文本文件复制的例题,编写对二进制文件进
3、行复制的程序./ 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 ) Syste
4、m.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 (
5、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(); catch ( IOException iox ) System.out.println(Problem closing +
6、sourceName ); retVal = false; try dest.close(); catch ( IOException iox ) System.out.println(Problem closing + destName ); retVal = false; return retVal; /执行复制 public boolean copy(String src, String dst ) sourceName = src ; destName = dst ; return openFiles() & copyFiles() & closeFiles(); /test6_2pu
7、blic class test6_2 public static void main ( String args ) String s1=lin.txt,s2=newlin.txt; if(new CopyMaker().copy(s1, s2) System.out.print(复制成功); else System.out.print(复制失败); 运行前的两个文本:lin.txt和newlin.txt(为空)运行后: 3.创建一存储若干随机整数的文本文件,文件名、整数的个数及范围均由键盘输入。/ memory存储类import java.io.*;import java.util.Rand
8、om;public class memory private String name;private int count;private int Max;private int Min;public memory(String n,int c,int min,int max)this.name=n;this.count=c;this.Min=min;this.Max=max;public void startmemory()tryFileWriter out=new FileWriter(name);int limit=Max-Min;Random random = new Random();
9、for (int i=1;i=count;i+)int number=Min+random.nextInt(limit);System.out.print(number);System.out.print( );out.write(number+ );out.close();catch(IOException iox) System.out.println(方法startmemory()有问题); /test6_3import java.io.*;import java.util.Scanner;public class test6_3 public static void main(Stri
10、ng args) throws IOException/BufferedReader String fileName;int count,min,max;Scanner in = new Scanner(System.in);System.out.println(输入要存储的文件名);fileName=in.next();System.out.println(输入随机数个数);count=in.nextInt();System.out.println(输入随机数最小值);min=in.nextInt();System.out.println(输入随机数最大值);max=in.nextInt()
11、;memory M=new memory(fileName,count,min,max);M.startmemory();运行结果:naruto文件存储二进制数:4.分别使用FileWriter和BufferedWriter往文件中写入10万个随机数,比较用时的多少。/FileWriter方法import java.io.*;public class fileWriter public static void main(String args) throws IOExceptionlong time = System.currentTimeMillis();/当前时间FileWriter fi
12、lewriter=new FileWriter(filewriter.txt);int number;for(int i=1;i=100000;i+)number=(int)(Math.random()*10000);filewriter.write(number+ );filewriter.close();time=System.currentTimeMillis()-time;/时间差System.out.println(用时为:+time+微秒.);运行结果:/BufferedWriter方法import java.io.*;public class bufferedWriter pub
13、lic static void main(String args) throws IOExceptionlong time = System.currentTimeMillis();/当前时间BufferedWriter filewriter=new BufferedWriter(new FileWriter(filewriter.txt);int number;for(int i=1;i=100000;i+)number=(int)(Math.random()*10000);filewriter.write(number+ );filewriter.close();time=System.c
14、urrentTimeMillis()-time;/时间差System.out.println(用时为:+time+微秒.);运行结果:有用时可知: BufferedWriter比FileWriter写入的速度快, 当需要写入大量内容,前者效率高。5.生成一html文件,使其能显示2的幂次(09)的表格如下:Power of 2Value0112/test6_5类import java.io.*;public class test6_5public static void main(String args) throws IOExceptionBufferedWriter fuck =new B
15、ufferedWriter( new FileWriter(6_5.html);fuck.write();fuck.newLine();fuck.write(Power of 2Value);for (int i=0;i=9;i+)fuck.write(+i+Math.pow(i, 2)+);fuck.write();fuck.newLine();fuck.close();运行结果:6.用记事本程序创建一篇包含几十个英语单词的小文章,要求从屏幕输出每一个单词。/test6_6import java.io.*;public class test6_6 public static void mai
16、n(String args)throws IOException FileReader fr=new FileReader(naruto.txt);int s;while(s=fr.read()!=-1)if(s=a& s=A & s=Z)System.out.print(char)s);else System.out.print(n);fr.close();运行结果:7.从键盘敲入一系列字母,将其存储到文件中,对其进行升序排序后,存储到另一个文件,并显示在屏幕上。/test6_7import java.io.*;import java.util.Scanner;public class te
17、st6_7 /将字符串存入文件 public static void WriteToFile(String s,String fileName) try FileWriter writer = new FileWriter(fileName); writer.write(s); writer.close(); catch (IOException iox) System.out.println(写入字符串s到文件时出错!); /将字符串从文件中读取出来 public static String ReadFromFile(String fileName) String s=new String(
18、); try BufferedReader in = new BufferedReader(new FileReader(fileName); s=in.readLine(); catch(IOException io) System.out.println(从文件中读出字符串时出错!); return s; /将字符串排序,然后返回字符串 public static String sort(String s) char c=s.toCharArray(); /将字符串转换成字符数组 char temp; for(int i=0;ic.length-1;i+)/这里运用冒泡排序法 for(in
19、t j=i+1;jcj) temp=ci; ci=cj; cj=temp; s=new String(c); /注意 不能用c.toString return s; public static void main(String args) Scanner in=new Scanner(System.in); String s=in.next();/从键盘输入一组字符 WriteToFile(s,D:naruto.txt); /将字符串保存到D:naruto.txt s=null;/将s清空,下面好读取,不然无法辨别s是读取到数据还是原来的数据 s=ReadFromFile(D:naruto.t
20、xt); /从文件D:naruto.txt读出字符串 s=sort(s); /将s进行排序 WriteToFile(s,D:newnaruto.txt); /将排序后的内容保存到另一文件 D:newnaruto.txt System.out.println(s); 运行结果:文件D:naruto.txt和D:newnaruto.txt中内容:8.创建一个学生类(包括姓名、年龄、班级、密码),创建若干该类的对象并保存在文件中(密码不保存),从文件读取对象后显示在屏幕上。/Student类import java.io.Serializable;class Student implements Se
21、rializableString name;int age;int grade;transient String secret;public Student(String name,int age,int grade,String secret)this.name=name;this.age=age;this.grade=grade;this.secret=secret;/test6_8import java.io.*;public class tset6_8 public static void main(String args) throws IOException,ClassNotFou
22、ndExceptionStudent student=new Student(苍井空,19,101,changjingkong),new Student(吉沢明,19,103,jizeming),new Student(武藤兰,20,104,wutenglan),new Student(我爱女优,21,105,woainvyou);/创建输出 ObjectOutputStream oos=new ObjectOutputStream( new FileOutputStream(naruto.dat); for(int i=0;istudent.length;i+) oos.writeObjec
23、t(studenti); oos.close(); /将对象数组student全部清空,后边你会明白的。 for(int i=0;istudent.length;i+) studenti=null; /创建输入 ObjectInputStream ois=new ObjectInputStream( new FileInputStream(naruto.dat); for(int i=0;istudent.length;i+) studenti=(Student)ois.readObject(); /重新从文件中读入,之前的清空就因为此读入。注意,读入要强制转化为学生类 ois.close()
24、; /显示信息 for(int i=0;i8)System.arraycopy(name.toCharArray(),0, this.Name, 0,8);/如果字符长度大于8,只取前8个else System.arraycopy(name.toCharArray(),0,this.Name,0,name.toCharArray().length);/如果字符长度小于8,有几个填几个this.Number=number;this.Price=price;this.Count=count;public String getName() return new String(this.Name);p
25、ublic void setName(String name) if(name.toCharArray().length8)System.arraycopy(name.toCharArray(),0, this.Name, 0,8);else System.arraycopy(name.toCharArray(),0,this.Name,0,name.toCharArray().length);public double getPrice() return Price;public void setPrice(double price) this.Price = price;public in
26、t getNumber() return Number;public void setNumber(int number) this.Number = number;public int getCount() return Count;public void setCount(int count) this.Count = count;/操作Work类(包含输入、查询、更改操作)import java.io.*;public class WorkString FileName;public Work(String FileName)this.FileName=FileName;/写第n条商品记录public void writeThing(Thing th,int n) t