1 / 22

面向对象程序设计

面向对象程序设计. 第 9 讲 Java 的 I/O 操作. 回顾. 对象数组 Arrays 类的使用 java.util 包 List 集合 Set 集合 Map 集合 Java 新特性 —— 泛型. 本讲主要内容. File 类 面向字节的 I/O 操作 FileInputStream 、 FileOutputStream BufferedInputStream 、 BufferedOutputStream 面向字符的 I/O 操作 FileReader 、 FileWriter BufferedReader 、 BufferedWriter

kaethe
Download Presentation

面向对象程序设计

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 面向对象程序设计 第9讲 Java的I/O操作

  2. 回顾 • 对象数组 • Arrays类的使用 • java.util包 • List集合 • Set集合 • Map集合 • Java新特性——泛型

  3. 本讲主要内容 • File类 • 面向字节的I/O操作 • FileInputStream、FileOutputStream • BufferedInputStream、 BufferedOutputStream • 面向字符的I/O操作 • FileReader、FileWriter • BufferedReader、BufferedWriter • 对象的序列化

  4. 文件 • 什么是文件? • 文件可认为是相关记录或放在一起的数据的集合。在文件系统中,文件夹和文件从本质上没有区别,文件夹只是一种特殊的文件,里面保存了一组文件的名字而已。 • 文件一般存储在哪里? • 可以通过java.io.File类对文件或者文件夹进行一些操作,如修改属性、删除文件、创建文件夹等。

  5. File类 • 构造方法 • File(Stringpathname):通过将给定路径名字符串转换成抽象路径名来创建一个新 File 实例。 • File(Stringparent, Stringchild):根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。 • File(Fileparent, Stringchild) :根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。 • 常用实例方法 • exists() 、createNewFile() 、delete()、getAbsolutePath()、canWrite() 、isDirectory()、isFile() 、length() 、lastModified() 、listFiles() 、mkdir() 演示File类

  6. 流的概念 A E C B F D • 流是指一连串流动的字符,是以先进先出方式发送信息的通道。 InputStream OutputStream 来自数据源的数据流 流向目的地的数据流

  7. 流的分类 • 字节流 • InputStream • OutputStream • 字符流 • Reader • Writer

  8. 用FileInputStream 读文件 import java.io.*; public class TestFileInputStream { public static void main(String[] args) throws FileNotFoundException,IOException{ FileInputStreamfis = null; try { fis = new FileInputStream(new File("d:/java/a.txt")); System.out.println(fis.available()); intch; while ((ch = fis.read()) != -1) { System.out.println((char)ch); } } finally { fis.close(); } } } • 引入相关的类 • 构造一个文件输入流对象 • 利用文件输入流类的方法读取文件的数据 • 关闭文件输入流对象 演示FileInputStream类

  9. 用FileOutputStream 写文件 import java.io.*; public class TestFileOutputStream { public static void main(String[] args) throws FileNotFoundException,IOException{ FileOutputStream fos = null; String str = "你好!\r\nJava"; byte[] bytes = str.getBytes(); try { fos = new FileOutputStream(new File("d:/java/a.txt")); for (int i = 0; i < bytes.length; i++) { fos.write(bytes[i]); } } finally { fos.close(); } } } • 引入相关的类 • 构造一个文件输出流对象 • 利用文件输出流类的方法写文件 • 关闭文件输出流对象 •  void write(byte[] b) :将 b.length 个字节写入此输出流。 • void write(byte[] b, int off, int len):将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。 • void write(int b) :将指定 byte 写入此输出流。 演示FileOutputStream类

  10. 使用FileInputStream和FileOutputStream实现文件复制 • String from = "d:/java/in.dat"; • String to = "d:/java/out.dat"; • FileInputStream in = null; • FileOutputStream out = null; • try{ • in = new FileInputStream(from); • out = new FileOutputStream(to); • inti = 0; • while((i = in.read( )) != -1) { • out.write(i); • } • } finally { • in.close( ); out.close( ); • } 演示使用字节流文件复制

  11. 使用字节流的Filter String from = "d:/java/a.txt"; String to = "d:/java/b.txt"; BufferedInputStream in = null; BufferedOutputStream out = null; try { in = new BufferedInputStream(new FileInputStream(from)); out = new BufferedOutputStream(new FileOutputStream(to)); inti = 0; while ((i = in.read()) != -1) { out.write(i); } } finally { in.close(); out.close(); } • BufferedOutputStream与BufferedInputStream是完全相对应的,对数据流提供缓冲技术。 • 使用BufferedOutputStream与BufferedInputStream来包装的字节流,当每次向流写入或输出时,不必每次都访问极慢的外部设备。 演示使用字节流的Filter实现文件复制

  12. 用FileReader 读文件 import java.io.*; public class TestFileReader { public static void main(String[] args) throws FileNotFoundException, IOException { FileReaderfr = null; try { fr = new FileReader (new File("d:/java/a.txt")); intch; while ((ch = fr.read()) != -1) { System.out.println((char)ch); } } finally { fr.close(); } } } • 引入相关的类 • 构造一个FileReader对象 • 利用FileReader类的方法读取文件的数据 • 关闭FileReader对象 演示FileReader类

  13. 用FileWriter 写文件 import java.io.*; public class TestFileWriter { public static void main(String[] args) throws FileNotFoundException,IOException{ FileWriter fw = null; String str = "你好!\r\nJava"; //char[] chars = str.toCharArray(); try { fw = new FileWriter(new File("d:/java/a.txt")); fw.write(str); //fw.write(chars); } finally { fw.close(); } } } • 引入相关的类 • 构造一个FileWriter对象 • 利用FileWriter类的方法写文件 • 关闭FileWriter对象 • void write(char[] cbuf, int off, int len) :将字符写入数组的某一部分。 • void write(int c) :写入单个字符。 • void write(String str, int off, int len):写入一部分字符串。 演示FileWriter类

  14. 对象的序列化 • 当应用程序结束,所有创建的对象都会走向消亡。 • 有些应用场景下,能够将对象的状态保存下来,并在下一次应用程序启动时重新启用被保存的信息是非常有必要的。 • 利用I/O操作可以实现这种需求,可以通过把一些关键信息按照一定的顺序保存到文件中,并在下一次启动应用程序的时候打开文件按顺序读取信息。 • Java的对象序列化可以将任何一个实现了Serializable接口的对象自动转换成一个字节序列,并且能够在其后将这个字节序列完全恢复为原来的对象。该字节序列不仅可以保存到硬盘上,还可以传输到网络上 。 演示对象的序列化

  15. 关键代码 • 序列化一个对象 • ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("d:/java/stu.tmp")); • out.writeObject(obj); • 还原字节序列成为对象 • ObjectInputStream in = new ObjectInputStream( new FileInputStream("d:/java/stu.tmp")); • Object obj = in.readObject( );

  16. transient 关键字 • 有的特殊需求却并不要求保存所有信息 。 • 例如,考虑到安全因素,用户并不希望应用程序保存他的密码,仅仅记住用户名就可以了,密码由用户自己记住更加安全。否则,如果序列化之后,木马程序会很方便的把所有信息全部盗走。 • 为了能够有效控制序列化的信息,可以用transient(瞬时)关键字逐个字段的关闭序列化。 演示transient关键字

  17. 序列化存储多个对象 • Student stu1 = new Student( "001","张三",Date.valueOf("1981-01-01"),"计算机"); • Student stu2 = new Student( "002","李四",Date.valueOf("1982-02-02"),"会计"); • Map<String, Student> map = new HashMap<String, Student>(); • map.put("001",stu1); • map.put("002",stu2); • ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("d:/java/stus.tmp")); • out.writeObject(map); • out.close(); 演示序列化多个对象

  18. 本讲小结 • File类 • 面向字节的I/O操作 • FileInputStream、FileOutputStream • BufferedInputStream、 BufferedOutputStream • 面向字符的I/O操作 • FileReader、FileWriter • BufferedReader、BufferedWriter • 对象的序列化 • ObjectInputStream、ObjectOutputStream • transient关键字

  19. 实验一使用字节流实现文件的复制 • 目的 • 理解Java的I/O,掌握字节流操作文件的方式。 • 训练内容 • 1、使用java.io包完成图片复制的操作,要求原始图片保存在C:\img\1.jpg,目标图片保存在D:\下。 • 提示: • 使用File类实例化目录及文件对象。 • 使用FileInputStream和BufferedInputStream构建文件字节输入流对象。 • 使用FileOutputStream和BufferedOutputStream构建文件字节输出流对象。 • 注意字节流的关闭。 • 2、总结实训过程中涉及到的知识点及难点,遇到的问题和解决的办法,有哪些收获。 北京科技大学天津学院-信息工程系

  20. 实验二 使用字符流操作文件 • 目的 • 掌握使用File类中字符流实现文件I/O操作。 • 训练内容 • 1、使用java.io包完成如下操作:读取D:\java\content.txt文本文件中的内容(该内容含有中英文),例如,原有内容如第左图所示。 • 2、将读取后的字符串内容追加另外一段内容,然后重新写入该文本文件中去。追加后内容如第右图。 北京科技大学天津学院-信息工程系

  21. 实验二 使用字符流操作文件——续 • 训练内容 • 提示: • 使用File类实例化目录及文件对象。 • 使用FileReader和BufferedReader构建文件字符输入流对象。 • 使用FileWriter和BufferedWriter构建文件字符输出流对象。 • 使用BufferString类实现字符串的追加。 • 注意字符流的关闭。 • 3、总结实训过程中涉及到的知识点及难点,遇到的问题和解决的办法,有哪些收获。 北京科技大学天津学院-信息工程系

  22. 选作实验 实现对象的序列化 • 目的 • 理解序列化。 • 训练内容 • 1、构建游戏帐户实体类,包括用户名、密码、分数、过关程度等属性,使该类实现java.io.Serializable接口,实现序列化操作,模拟序列化控制某一个游戏玩家的当前信息。 • 注意:为安全起见,密码不能进行序列化存储。 • 2、总结实训过程中涉及到的知识点及难点,遇到的问题和解决的办法,有哪些收获。 北京科技大学天津学院-信息工程系

More Related