Java JDK 6学习笔记 教学课件 ppt 作者 978-7-302-14995-8 第14章

上传人:E**** 文档编号:89355026 上传时间:2019-05-23 格式:PPT 页数:39 大小:320KB
返回 下载 相关 举报
Java JDK 6学习笔记 教学课件 ppt 作者 978-7-302-14995-8 第14章_第1页
第1页 / 共39页
Java JDK 6学习笔记 教学课件 ppt 作者 978-7-302-14995-8 第14章_第2页
第2页 / 共39页
Java JDK 6学习笔记 教学课件 ppt 作者 978-7-302-14995-8 第14章_第3页
第3页 / 共39页
Java JDK 6学习笔记 教学课件 ppt 作者 978-7-302-14995-8 第14章_第4页
第4页 / 共39页
Java JDK 6学习笔记 教学课件 ppt 作者 978-7-302-14995-8 第14章_第5页
第5页 / 共39页
点击查看更多>>
资源描述

《Java JDK 6学习笔记 教学课件 ppt 作者 978-7-302-14995-8 第14章》由会员分享,可在线阅读,更多相关《Java JDK 6学习笔记 教学课件 ppt 作者 978-7-302-14995-8 第14章(39页珍藏版)》请在金锄头文库上搜索。

1、第14章,输入 输出 档案 位串流 字符串流,File类别,不同的操作系统对于文件系统路径的设定各有差别 Windows Linux,“C:WorkspaceCH14“,“/home/justin/workspace/ch14“,File类别,File实例用作一个档案或目录的抽象表示,File file = new File(args0); if(file.isFile() /是否为档案 System.out.println(args0 + “檔案“); System.out.print( file.canRead() ?“可读“ :“不可读“); System.out.print( file

2、.canWrite() ?“可写“ :“不可写“); System.out.println( file.length() +“位組“); ,File类别,else /列出所有的档案及目录 File files = file.listFiles(); ArrayList fileList = new ArrayList(); for(int i = 0; i files.length; i+) /先列出目录 if(filesi.isDirectory() /是否为目录 /取得路径名 System.out.println(“ + filesi.getPath() + “); else /档案先存入

3、fileList,待会再列出 fileList.add(filesi); ,File类别,/列出档案 for(File f: fileList) System.out.println(f.toString(); System.out.println(); ,RandomAccessFile类别,File file = new File(args0); /建立RandomAccessFile实例并以读写模式开启档案 RandomAccessFile randomAccessFile = new RandomAccessFile(file, “rw“); for(int i = 0; i stud

4、ents.length; i+) /使用对应的write方法写入数据 randomAccessFile.writeChars(studentsi.getName(); randomAccessFile.writeInt(studentsi.getScore(); ,RandomAccessFile类别,/使用seek()方法操作存取位置 randomAccessFile.seek(num-1) * Student.size(); Student student = new Student(); /使用对应的read方法读出数据 student.setName(readName(randomAc

5、cessFile); student.setScore(randomAccessFile.readInt(); System.out.println(“姓名:“ + student.getName(); System.out.println(“分数:“ + student.getScore(); /设定关闭档案 randomAccessFile.close();,RandomAccessFile类别,private static String readName(RandomAccessFile randomAccessfile) throws IOException char name = n

6、ew char15; for(int i = 0; i name.length; i+) namei = randomAccessfile.readChar(); /将空字符取代为空格符并传回 return new String(name).replace(0, ); ,RandomAccessFile类别,读写档案时几个必要的流程 开启档案并指定读写方式 使用对应的写入方法 使用对应的读出方法 关闭档案,InputStream、OutputStream,数据流动抽象化为一个串流(Stream),InputStream、OutputStream,InputStream是所有表示位输入串流的类别

7、之父类别 System中的标准输入串流in对象就是一个InputStream类型的实例 OutputStream是所有表示位输出串流的类别之父类别 System中的标准输出串流对象out其类型是java.io.PrintStream,OutputStream的子类别,InputStream、OutputStream,很少直接操作InputStream或OutputStream上的方法,这些方法比较低阶 通常会操作它们的子类别,try System.out.print(“输入字元: “); System.out.println(“输入字符十进制表示: “ + System.in.read();

8、catch(IOException e) e.printStackTrace(); ,FileInputStream、FileOutputStream,建立FileInputStream或FileOutputStream的实例时,必须指定档案位置及文件名,实例被建立时档案的串流就会开启 不使用串流时,您必须关闭档案串流,以释放与串流相依的系统资源,FileInputStream fileInputStream = new FileInputStream(new File(args0); FileOutputStream fileOutputStream = new FileOutputStre

9、am(new File(args1); fileInputStream.close(); fileOutputStream.close();,FileInputStream、FileOutputStream,while(true) if(fileInputStream.available() 1024) /剩余的资料比1024字节少 /一位一位读出再写入目标文件 int remain = -1; while(remain = fileInputStream.read() != -1) fileOutputStream.write(remain); break; else /从来源档案读取数据至

10、缓冲区 fileInputStream.read(buffer); /将数组数据写入目标文件 fileOutputStream.write(buffer); ,FileInputStream、FileOutputStream,以附加的模式来写入档案,FileOutputStream fileOutputStream = new FileOutputStream(args1, true);,BufferedInputStream、BufferedOutputStream,BufferedInputStream的资料成员buf是个位数组,默认为2048字节 BufferedOutputStream

11、的资料成员buf是个位数组,默认为512个字节,BufferedInputStream、BufferedOutputStream,BufferedInputStream bufferedInputStream = new BufferedInputStream( new FileInputStream(srcFile); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream( new FileOutputStream(desFile); System.out.println(“复制档案:“ + srcFile.

12、length() +“字节“); while(bufferedInputStream.read(data) != -1) bufferedOutputStream.write(data); /将缓冲区中的数据全部写出 bufferedOutputStream.flush(); /关闭串流 bufferedInputStream.close(); bufferedOutputStream.close();,BufferedInputStream、BufferedOutputStream,BufferedInputStream、BufferedOutputStream并没有改变InputStrea

13、m或OutputStream的行为 只是在操作对应的方法之前,动态的为它们加上一些是缓冲区功能,DataInputStream、DataOutputStream,DataOutputStream dataOutputStream = new DataOutputStream( new FileOutputStream(args0); for(Member member : members) /写入UTF字符串 dataOutputStream.writeUTF(member.getName(); /写入int资料 dataOutputStream.writeInt(member.getAge(

14、); /出清所有数据至目的地 dataOutputStream.flush(); /关闭串流 dataOutputStream.close();,提供一些对Java基本数据型态写入的方法,DataInputStream、DataOutputStream,DataInputStream dataInputStream = new DataInputStream( new FileInputStream(args0); /读出数据并还原为对象 for(int i = 0; i members.length; i+) /读出UTF字符串 String name = dataInputStream.r

15、eadUTF(); /读出int资料 int score = dataInputStream.readInt(); membersi = new Member(name, score); /关闭串流 dataInputStream.close();,ObjectInputStream、ObjectOutputStream,要直接储存对象,定义该对象的类别必须实作java.io.Serializable界面 serialVersionUID代表了可串行化对象版本 从档案读回对象时两个对象的serialVersionUID不相同的话,就会丢出java.io.InvalidClassExceptio

16、n,public class User implements Serializable private static final long serialVersionUID = 1L; ,ObjectInputStream、ObjectOutputStream,在写入对象时,您要使用writeObject()方法 读出对象时则使用readObject()方法,被读出的对象都是以Object的型态传回,ObjectInputStream、ObjectOutputStream,public static void writeObjectsToFile( Object objs, String filename) File file = new

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号