ImageVerifierCode 换一换
格式:DOC , 页数:20 ,大小:418KB ,
资源ID:5957022      下载积分:10 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.docduoduo.com/d-5957022.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录   微博登录 

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(java答案第六章.doc)为本站会员(hyngb9260)主动上传,道客多多仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知道客多多(发送邮件至docduoduo@163.com或直接QQ联系客服),我们立即给予删除!

java答案第六章.doc

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 fi

2、leName = “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.

3、模仿文本文件复制的例题,编写对二进制文件进行复制的程序./ CopyMaker类import java.io.*; class CopyMaker String sourceName, destName;BufferedInputStream source;BufferedOutputStream dest;int line;/打开源文件和目标文件,无异常返回trueprivate boolean openFiles() try source = new BufferedInputStream(new FileInputStream( sourceName );catch ( IOExcept

4、ion 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.

5、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(); catch ( IOException iox ) System.out.println(“Prob

6、lem closing “ + 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() /test6_2public class test6_2

7、 public static void main ( String args ) String s1=“lin.txt“,s2=“newlin.txt“;if(new CopyMaker().copy(s1, s2)System.out.print(“复制成功“ );elseSystem.out.print(“复制失败“ ); 运行前的两个文本:lin.txt 和 newlin.txt(为空)运行后: 3.创建一存储若干随机整数的文本文件,文件名、整数的个数及范围均由键盘输入。/ memory 存储类import java.io.*;import java.util.Random;public

8、 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();for (int

9、i=1;i“);fuck.newLine();fuck.write(“Power of 2Value“);for (int i=0;i“+i+“+Math.pow(i, 2)+“);fuck.write(“);fuck.newLine();fuck.close();运行结果:6.用记事本程序创建一篇包含几十个英语单词的小文章,要求从屏幕输出每一个单词。/test6_6import java.io.*;public class test6_6 public static void main(String args)throws IOException FileReader fr=new File

10、Reader(“naruto.txt“);int s;while(s=fr.read()!=-1)if(s=aci=cj;cj=temp;s=new String(c); /注意 不能用c.toStringreturn s;public static void main(String args) Scanner in=new Scanner(System.in);String s=in.next();/从键盘输入一组字符WriteToFile(s,“D:naruto.txt“); /将字符串保存到D:naruto.txts=null;/将s清空,下面好读取,不然无法辨别 s是读取到数据还是原来

11、的数据s=ReadFromFile(“D:naruto.txt“); /从文件D:naruto.txt读出字符串s=sort(s); /将s进行排序WriteToFile(s,“D:newnaruto.txt“); /将排序后的内容保存到另一文件 D:newnaruto.txtSystem.out.println(s); 运行结果:文件 D:naruto.txt 和 D:newnaruto.txt中内容:8.创建一个学生类(包括姓名、年龄、班级、密码) ,创建若干该类的对象并保存在文件中(密码不保存) ,从文件读取对象后显示在屏幕上。/Student 类import java.io.Seria

12、lizable;class Student implements SerializableString 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 a

13、rgs) throws IOException,ClassNotFoundExceptionStudent 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.

14、dat“);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);pu

15、blic 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 int

16、 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条商品记

17、录public void writeThing(Thing th,int n) throws Exception RandomAccessFile rafw=new RandomAccessFile(FileName,“rw“);rafw.seek(n*32); /将位置指示器移到指定位置上,每个商品占 32个字节for(int i=0;i8)System.arraycopy(name.toCharArray(),0,borrow,0,8);else System.arraycopy(name.toCharArray(),0,borrow,0,name.toCharArray().length

18、);for(int i=0;i8;i+)rafw.writeChar (borrowi);rafw.close(); /修改存入n号商品价格public void setThingPrice(int n,double price) throws Exception RandomAccessFile rafw=new RandomAccessFile(FileName,“rw“);rafw.seek(n*32+8+4); /将位置指示器移到指定位置上,每个商品占24 个字节,名字占8字节rafw.writeDouble(price);rafw.close();/修改存入n号商品库存量public

19、 void setThingCount(int n,int count) throws Exception RandomAccessFile rafw=new RandomAccessFile(FileName,“rw“);rafw.seek(n*32+8+4+8); /将位置指示器移到指定位置上,每个商品占24个字节,名字占8字节rafw.writeFloat(count);rafw.close();/test6_10import java.util.Scanner;public class test6_10public static void main(String args)throws

20、 Exception Scanner scanner=new Scanner(System.in);int number;String name;Work work=new Work(“d:/naruto.txt“);Thing thing=new Thing(“立白洗衣粉“ ,1,3.5,55),new Thing(“编辑者林楚金“ ,2,0,0),new Thing(“舒肤佳“ ,4,8.9,89),new Thing(“杜蕾丝超薄系列“ ,5,1.2,444444),;for(int i=0;ithing.length;i+)work.writeThing(thingi,i);for(int i=0;ithing.length;i+)work.readThing(i);System.out.println(“请输入要查询商品编号 “);number=scanner.nextInt();work.readThing(number-1);System.out.println(“请输入要修改某一商品名字:编号和名字 “);number=scanner.nextInt();name=scanner.next();work.setThingName(number-1,name);work.readThing(number-1);运行结果:

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报