Java语言程序设计第八章.ppt

上传人:小飞机 文档编号:6510222 上传时间:2023-11-07 格式:PPT 页数:17 大小:211KB
返回 下载 相关 举报
Java语言程序设计第八章.ppt_第1页
第1页 / 共17页
Java语言程序设计第八章.ppt_第2页
第2页 / 共17页
Java语言程序设计第八章.ppt_第3页
第3页 / 共17页
Java语言程序设计第八章.ppt_第4页
第4页 / 共17页
Java语言程序设计第八章.ppt_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《Java语言程序设计第八章.ppt》由会员分享,可在线阅读,更多相关《Java语言程序设计第八章.ppt(17页珍藏版)》请在三一办公上搜索。

1、1,Java面向对象程序设计,第8章 Java的I/O操作,2,本章主要内容,File类面向字节的I/O操作FileInputStream、FileOutputStreamBufferedInputStream、BufferedOutputStream面向字符的I/O操作FileReader、FileWriterBufferedReader、BufferedWriter对象的序列化,3,File类,什么是文件?文件可认为是相关记录或放在一起的数据的集合。在文件系统中,文件夹和文件从本质上没有区别,文件夹只是一种特殊的文件,里面保存了一组文件的名字而已。文件一般存储在哪里?可以通过java.io

2、.File类对文件或者文件夹进行一些操作,如修改属性、删除文件、创建文件夹等。,4,File类,构造方法File(Fileparent,Stringchild):根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。File(Stringpathname):通过将给定路径名字符串转换成抽象路径名来创建一个新 File 实例。File(Stringparent,Stringchild):根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。常用实例方法exists()、createNewFile()、delete()、getAbs

3、olutePath()、canWrite()、isDirectory()、isFile()、length()、lastModified()、listFiles()、mkdir(),演示File类,5,流的概念,流是指一连串流动的字符,是以先进先出方式发送信息的通道。,InputStream,OutputStream,来自数据源的数据流,流向目的地的数据流,6,流的分类,字节流InputStreamOutputStream字符流ReaderWriter,7,用FileInputStream 读文件,引入相关的类构造一个文件输入流对象利用文件输入流类的方法读取文件的数据关闭文件输入流对象,演示Fi

4、leInputStream类,import java.io.*;public class TestFileInputStream public static void main(String args)throws FileNotFoundException,IOException FileInputStream fis=null;try fis=new FileInputStream(new File(d:/java/a.txt);System.out.println(fis.available();int ch;while(ch=fis.read()!=-1)System.out.prin

5、tln(char)ch);finally fis.close();,8,用FileOutputStream 写文件,引入相关的类构造一个文件输出流对象利用文件输出流类的方法写文件关闭文件输出流对象,演示FileOutputStream类,import java.io.*;public class TestFileOutputStream public static void main(String args)throws FileNotFoundException,IOException FileOutputStream fos=null;String str=你好!rnJava;byte b

6、ytes=str.getBytes();try fos=new FileOutputStream(new File(d:/java/a.txt);for(int i=0;i bytes.length;i+)fos.write(bytesi);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 写入此输出流。,9,使用FileI

7、nputStream和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);int i=0;while(i=in.read()!=-1)out.write(i);finally in.close();out.close();,演示使用字节流文件复制,10,使用字节流的Filte

8、r,BufferedOutputStream与BufferedInputStream是完全相对应的,对数据流提供缓冲技术。使用BufferedOutputStream与BufferedInputStream来包装的字节流,当每次向流写入或输出时,不必每次都访问极慢的外部设备。,演示使用字节流的Filter实现文件复制,String from=d:/java/a.txt;String to=d:/java/b.txt;BufferedInputStream in=null;BufferedOutputStream out=null;try in=new BufferedInputStream(n

9、ew FileInputStream(from);out=new BufferedOutputStream(new FileOutputStream(to);int i=0;while(i=in.read()!=-1)out.write(i);finally in.close();out.close();,11,用FileReader 读文件,引入相关的类构造一个FileReader对象利用FileReader类的方法读取文件的数据关闭FileReader对象,演示FileReader类,import java.io.*;public class TestFileReader public s

10、tatic void main(String args)throws FileNotFoundException,IOException FileReader fr=null;try fr=new FileReader(new File(d:/java/a.txt);int ch;while(ch=fr.read()!=-1)System.out.println(char)ch);finally fr.close();,12,用FileWriter 写文件,引入相关的类构造一个FileWriter对象利用FileWriter类的方法写文件关闭FileWriter对象,演示FileWriter类

11、,import java.io.*;public class TestFileWriter public static void main(String args)throws FileNotFoundException,IOException FileWriter fw=null;String str=你好!rnJava;/char chars=str.toCharArray();try fw=new FileWriter(new File(d:/java/a.txt);fw.write(str);/fw.write(chars);finally fw.close();,void write

12、(char cbuf,int off,int len):将字符写入数组的某一部分。void write(int c):写入单个字符。void write(String str,int off,int len):写入一部分字符串。,13,对象的序列化,当应用程序结束,所有创建的对象都会走向消亡。有些应用场景下,能够将对象的状态保存下来,并在下一次应用程序启动时重新启用被保存的信息是非常有必要的。利用I/O操作可以实现这种需求,可以通过把一些关键信息按照一定的顺序保存到文件中,并在下一次启动应用程序的时候打开文件按顺序读取信息。Java的对象序列化可以将任何一个实现了Serializable接口的

13、对象自动转换成一个字节序列,并且能够在其后将这个字节序列完全恢复为原来的对象。该字节序列不仅可以保存到硬盘上,还可以传输到网络上。,演示对象的序列化,14,关键代码,序列化一个对象 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

14、.readObject();,15,transient 关键字,有的特殊需求却并不要求保存所有信息。例如,考虑到安全因素,用户并不希望应用程序保存他的密码,仅仅记住用户名就可以了,密码由用户自己记住更加安全。否则,如果序列化之后,木马程序会很方便的把所有信息全部盗走。为了能够有效控制序列化的信息,可以用transient(瞬时)关键字逐个字段的关闭序列化。,演示transient关键字,16,序列化存储多个对象,Student stu1=new Student(001,张三,Date.valueOf(1981-01-01),计算机);Student stu2=new Student(002,李

15、四,Date.valueOf(1982-02-02),会计);Map map=new HashMap();map.put(001,stu1);map.put(002,stu2);ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(d:/java/stus.tmp);out.writeObject(map);out.close();,演示序列化多个对象,17,总结,File类面向字节的I/O操作FileInputStream、FileOutputStreamBufferedInputStream、BufferedOutputStream面向字符的I/O操作FileReader、FileWriterBufferedReader、BufferedWriter对象的序列化ObjectInputStream、ObjectOutputStreamtransient关键字,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号