java语言程序设计 第6章_第1页
java语言程序设计 第6章_第2页
java语言程序设计 第6章_第3页
java语言程序设计 第6章_第4页
java语言程序设计 第6章_第5页
已阅读5页,还剩86页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第六章 输入/输出流和文件JAVA语言程序设计目录6.1 输入/输出流6.2 文件读写6.3 本章小结26.1 输入/输出流通常程序需要从外部获取(输入)/输出信息 输入:是指数据从数据源流入程序 (读)。 数据源通常为键盘、文件等。 输出:是指数据从程序流出(写)到目的地。 目的地通常为屏幕、文件等。 信息:可以是任何类型的,例如字符、图像、声音等。使用java.io包中的输入/输出流类 使用过程:1、创建I/0流类对象(打开) 2、使用对象的方法完成读写。 3、关闭I/0流类对象3输入流(读数据)从信息源获取信息,创建一个输入流,程序可从输入流读取信息输出流(写数据)当程序向目标位置写信息

2、,创建一个输出流,程序通过输出流向目标位置写信息6.1.1 I/O流的概念(续) 输入/输出流46.1.2 预定义的I/O流类概述输入/输出流可以从以下几个方面进行分类从流的方向划分输入流输出流从流的分工划分节点流(与数据源或目的地连接)处理流(包装节点流,对数据进行处理,方便读写)从流的内容划分面向字符的流(读写字符)Reader、Writer面向字节的流(读写字节)InputSream、OutputStream输入/输出流5输入/输出流6.1.2 预定义的I/O流类概述(续) 面向字符的流面向字符的流针对字符数据的特点进行过优化,提供一些面向字符的有用特性源或目标通常是文本文件6面向字符的

3、抽象类Reader和Writerjava.io包中所有字符流的抽象基类Reader提供了输入字符的API(方法)Writer提供了输出字符的API(方法)它们的子类又可分为两大类节点流:与数据源或目的地连接 处理流:与节点流连接,对数据进行处理,方便读写多数程序使用这两个抽象类的一系列子类来读入/写出文本信息例如FileReader/FileWriter用来读/写文本文件(例子)6.1.2 预定义的I/O流类概述(续) 面向字符的流输入/输出流76.1.2 预定义的I/O流类概述(续) 面向字符的流输入/输出流阴影部分为节点流8数据源或目标中含有非字符数据(图片、声音等二进制数据),必须用字节

4、流来输入/输出绝大多数数据是被存储为二进制文件的,世界上的文本文件大约只能占到2,通常二进制文件要比含有相同数据量的文本文件小得多6.1.2 预定义的I/O流类概述(续) 面向字节的流输入/输出流9InputStream和OutputStream是用来处理8位字节流的抽象基类,程序使用这两个类的子类来读写8位的字节信息分为两部分节点流处理流例子(读写二进制文件)6.1.2 预定义的I/O流类概述(续) 面向字节的流输入/输出流106.1.2 预定义的I/O流类概述(续) 面向字节的流输入/输出流阴影部分为节点流11标准输入输出流对象System类的静态成员变量包括System.in: Inpu

5、tStream类型的,代表标准输入流,与键盘连接。System.out:PrintStream类型的,代表标准输出流,与屏幕连接。System.err:PrintStream类型的,代表标准错误信息输出流,与屏幕连接。6.1.2 预定义的I/O流类概述(续) 标准输入输出输入/输出流126.1.2 预定义的I/O流类概述(续) 标准输入输出标准I/O重新导向setIn(InputStream): 设置标准输入流setOut(PrintStream):设置标准输出流setErr(PrintStream):设置标准错误输出流输入/输出流13从键盘读入信息并在显示器上显示import java.io

6、.*;public class Echo public static void main(String args) throws IOException BufferedReader in = new BufferedReader( new InputStreamReader(System.in); String s; while(s = in.readLine().length() != 0) System.out.println(s); 6.1.2 预定义的I/O流类概述(续) 例6_1输入/输出流运行结果Hello!Hello!14System.in程序启动时由Java系统自动创建的流对

7、象,它是原始的字节流,不能直接从中读取字符,需要对其进行进一步的处理InputStreamReader(System.in)以System.in为参数创建一个InputStreamReader流对象,相当于字节流和字符流之间的一座桥梁,读取字节并将其转换为字符BufferedReader in对InputStreamReader处理后的信息进行缓冲,以提高效率6.1.2 预定义的I/O流类概述(续) 例6_1说明输入/输出流15用一行表达式实现:BufferedReader stdin = new BufferedReader ( new InputStreamReader(System.in

8、) ); 6.1.2 预定义的I/O流类概述(续) 处理流输入/输出流16 IO异常多数IO方法在遇到错误时会抛出异常,因此调用这些方法时必须在方法头声明抛出IOException异常或者在try块中执行IO,然后捕获IOException6.1.2 预定义的I/O流类概述(续) I/O异常输入/输出流176.2 文件读写写文本文件读文本文件写二进制文件读二进制文件File类处理压缩文件对象序列化随机文件读写186.2.1 写文本文件 本节知识点FileWriter类继承Write类,节点流,连接所要写的目标文件。写入单个字符或字符串到文件中(write())。BufferedWriter 类

9、继承Write类,处理流,连接节点流,提高写的效率。换行方法newLine()文件读写19在C盘根目录创建文本文件Hello.txt,并往里写入若干行文本import java.io.*; class Ex6_2 public static void main ( String args ) throws IOException /main方法中声明抛出IO异常 String fileName = C:Hello.txt; FileWriter writer = new FileWriter( fileName ); /创建流对象 writer.write( “Hello!n”); /使用流对

10、象的写方法 writer.write( This is my first text file,n ); writer.write( You can see how this is done.n ); writer.write(输入一行中文也可以n); writer.close(); /关闭流 6.2.1 写文本文件(续) 例6_2文件读写FileWriter类(节点流)20打开C盘根目录下的Hello.txt文件换行有些问题,例6_4中将解决这个问题6.2.1 写文本文件(续) 例6_2运行结果文件读写21Writer类的流可实现内部格式到外部磁盘文件格式的转换“Hello.txt”是一个普通

11、的ASCII码文本文件,每个英文字符占一个字节,中文字符占两个字节Java程序中的字符串则是每个字符占两个字节的,采用Unicode编码close方法清空流里的内容并关闭它。如果不调用该方法,可能系统还没有完成所有数据的写操作,程序就结束了6.2.1 写文本文件(续) 例6_2说明(续) 文件读写22处理IO异常import java.io.*; class Ex6_3 public static void main ( String args ) String fileName = c:Hello.txt ; try /将所有IO操作放入try块中 FileWriter writer = n

12、ew FileWriter( fileName); writer.write( Hello!n); writer.write( This is my first text file,n ); writer.write( You can see how this is done. n ); writer.write(输入一行中文也可以n); writer.close(); catch ( IOException iox) System.out.println(Problem writing + fileName ); 6.2.1 写文本文件(续) 例6_3文件读写23使用BufferedWrit

13、er完成例6-2实现的功能import java.io.*; /ex6_4class Ex6_4 public static void main ( String args ) throws IOExceptionString fileName = C:/newHello.txt ;BufferedWriter out = new BufferedWriter( new FileWriter( fileName ) ); out.write( Hello! ); out.newLine() ; out.write( This is another text file using Buffere

14、dWriter, ); out.newLine(); ; out.write( So I can use a common way to start a newline ); out.close(); 6.2.1 写文本文件(续) 例6_4文件读写24用任何文本编辑器打开newHello.txt都会出现正确的换行效果6.2.1 写文本文件(续) 例6_4运行结果文件读写256.2.2 读文本文件 本节知识点Reader FileReader BufferedReader和readLine() 文本文件复制 文件读写26FileReader类继承自Reader,节点流,连接所要读的文件。读取单个

15、字符 read()BufferedReader 继承自Reader, 处理流,提高读取的效率。读文本文件的缓冲器类 具有readLine()方法,可以对换行符进行鉴别,一行一行地读取输入流中的内容6.2.2 读文本文件(续) 文件读写27 6.2.2 读文本文件(续) 文件输入方法:BufferedReader in = new BufferedReader(new FileReader( fileName) ); 文件读写28从Hello.txt中读取文本并显示在屏幕上import java.io.*;class Ex6_5 public static void main ( String

16、args ) String fileName = C:/Hello.txt , line; try BufferedReader in = new BufferedReader( new FileReader( fileName ) ); line = in.readLine(); /读取一行内容 while ( line != null ) System.out.println( line ); line = in.readLine(); in.close(); catch ( IOException iox ) System.out.println(Problem reading + fi

17、leName ); 6.2.2 读文本文件(续) 例6_5文件读写29运行该程序,屏幕上将逐行显示出Hello.txt文件中的内容FileReader对象:创建后将打开文件,如果文件不存在,会抛出一个IOExceptionBufferedReader类的readLine()方法:读取一行文本。如果其中不再有数据,返回nullReader类的read()方法:读取一个表示某个字符的int型整数,如果读到文件末尾,返回 -1。据此,可修改本例中的读文件部分:int c;while(c=in.read()!= -1) System.out.print(char)c);close()方法:为了操作系统

18、可以更为有效地利用有限的资源,应该在读取完毕后,调用该方法6.2.2 读文本文件(续) 例6_5说明文件读写30指定源文件和目标文件名,将源文件的内容拷贝至目标文件。调用方式为:copy sourceFile destinationFile6.2.2 读文本文件(续) 例6_6文件读写31共包括两个类CopyMakerprivate boolean openFiles()private boolean copyFiles()private boolean closeFiles()public boolean copy(String src, String dst ) Ex6_6main() 6

19、.2.2 读文本文件(续) 例6_6文件读写326.2.2 读文本文件(续) 例6_6import java.io.*; class CopyMaker String sourceName, destName; BufferedReader source; BufferedWriter dest; String line;33private boolean openFiles() try source = new BufferedReader(new FileReader( sourceName ); catch ( IOException iox ) System.out.println(P

20、roblem opening + sourceName ); return false; try dest = new BufferedWriter(new FileWriter( destName ); catch ( IOException iox ) System.out.println(Problem opening + destName ); return false; return true; 6.2.2 读文本文件(续) 例6_6文件读写34private boolean copyFiles() try line = source.readLine(); while ( line

21、 != null ) dest.write(line); dest.newLine(); line = source.readLine(); catch ( IOException iox ) System.out.println(Problem reading or writing ); return false; return true; 6.2.2 读文本文件(续) 例6_6文件读写35private boolean closeFiles() boolean retVal=true; try source.close(); catch ( IOException iox ) System

22、.out.println(Problem closing + sourceName ); retVal = false; try dest.close(); catch ( IOException iox ) System.out.println(Problem closing + destName ); retVal = false; return retVal; 6.2.2 读文本文件(续) 例6_6文件读写36 public boolean copy(String src, String dst ) sourceName = src ; destName = dst ; return o

23、penFiles() & copyFiles() & closeFiles(); public class Ex6_6 /一个文件中只能有一个公有类public static void main ( String args ) if ( args.length = 2 ) new CopyMaker().copy(args0, args1); else System.out.println(Please Enter File names); 6.2.2 读文本文件(续) 例6_6文件读写37此文件Ex6_6.java编译后生成Ex6_6.class和CopyMaker.class两个字节码文件

24、运行结果在命令行方式下执行如下命令java Ex6_6 c:/Hello.txt c:/CopyHello.txt则在C盘根目录下会出现CopyHello.txt文件,内容与Hello.txt完全相同6.2.2 读文本文件(续) 例6_6运行结果文件读写返回386.2.3 写二进制文件 本节知识点二进制文件 OutputStream ()FileOutputStream BufferedOutputStream DataOutputStream writeInt() writeDouble() writeBytes() 文件读写39二进制文件文本文件中都是字符。二进制文件中存在非字符信息(wo

25、rd文件中的图形、字体等信息)。6.2.3 写二进制文件(续) 二进制文件文件读写40抽象类OutputStream派生类FileOutputStream 节点流,连接所要写的文件。 写入一个字节,用write()派生类DataOutputStream 处理流,连接节点流,提供更多写的方法。具有写各种基本数据类型的方法其常用的一些方法见表6-26.2.3 写二进制文件(续) OutputStream类文件读写416.2.3 写二进制文件(续) 表6_2426.2.3 写二进制文件(续) 表6_2436.2.3 写二进制文件(续) 例6_7将三个int型数字255/0/1写入数据文件data1.

26、datimport java.io.*; class Ex6_7 public static void main ( String args ) String fileName = c:/data1.dat ; int value0 = 255, value1 = 0, value2 = -1; try DataOutputStream out = new DataOutputStream( new FileOutputStream( fileName ) ); out.writeInt( value0 ); out.writeInt( value1 ); out.writeInt( valu

27、e2 ); out.close(); catch ( IOException iox ) System.out.println(Problem writing + fileName ); 文件读写44运行结果运行程序后,在C盘生成数据文件data1.dat用写字板打开没有任何显示用ultraEdit打开查看其二进制信息,内容为00 00 00 FF 00 00 00 00 FF FF FF FF,每个int数字都是32个bit的说明FileOutputStream类的构造方法负责打开文件“data1.dat”用于写数据FileOutputStream类的对象与DataOutputStream对

28、象连接,写基本类型的数据6.2.3 写二进制文件(续) 例6_7运行结果文件读写45BufferedOutputStream处理流,连接节点流。对于大量数据的写入,可提高效率。用法示例:DataOutputStream out = new DataOutputStream( new BufferedOutputStream( new FileOutputStream( fileName ) ) ); 6.2.3 写二进制文件(续) BufferedOutputStream类文件读写46向文件中写入各种数据类型的数,并统计写入的字节数import java.io.*; class Ex6_8 p

29、ublic static void main ( String args ) throws IOException String fileName = mixedTypes.dat ; DataOutputStream dataOut = new DataOutputStream( new BufferedOutputStream( new FileOutputStream( fileName ) ) ); dataOut.writeInt( 0 ); System.out.println( dataOut.size() + bytes have been written.); dataOut

30、.writeDouble( 31.2 ); System.out.println( dataOut.size() + bytes have been written.); dataOut.writeBytes(JAVA); System.out.println( dataOut.size() + bytes have been written.); dataOut.close(); 6.2.3 写二进制文件(续) 例6_8文件读写47运行结果4 bytes have been written12 bytes have been written16 bytes have been written

31、说明这个程序可作为字节计数器6.2.3 写二进制文件(续) 例6_8运行结果文件读写48向文件中写入内容为-1的一个字节,并读出。import java.io.*;public class Ex6_9 public static void main(String args) throws Exception DataOutputStream out=new DataOutputStream( new FileOutputStream(c:/trytry.dat); out.writeByte(-1); out.close(); DataInputStream in=new DataInputS

32、tream( new FileInputStream(c:/trytry.dat); int a=in.readByte(); System.out.println(Integer.toHexString (a); System.out.println(a); in.skip (-1); /往前一个位置,以便下面重新读出 a=in.readUnsignedByte(); System.out.println(Integer.toHexString (a); System.out.println(a); in.close(); 6.2.3 写二进制文件(续) 例6_9文件读写49运行结果ffff

33、ffff-1ff255说明用ultraEdit打开c:/trytry.dat文件,其内容为FF如果用readByte读入,其高24位都将补1,所以结果还是-1如果用readUnsignedByte读入,其高24位都将补0,结果就变成了255写的字节是连续的,中间没有分隔符,所以应该记住写的数据类型、个数等情况,以便将来利用6.2.3 写二进制文件(续) 例6_9运行结果文件读写506.2.4 读二进制文件 本节知识点FileInputStream 继承InputStream,节点流,与要读取的文件连接。 读取一个字节(read())DataInputStream 继承InputStream,处

34、理流,提供读取各种类型的方法。 readint 读取int型数据。BufferedInputSteam 继承InputStream,处理流,提高读取效率.文件读写51读取例6-7创建的数据文件中的3个int型数字,显示相加结果import java.io.*;class Ex6_10 public static void main ( String args ) String fileName = c:data1.dat; int sum = 0; try DataInputStream instr = new DataInputStream( new BufferedInputStream(

35、new FileInputStream( fileName ); sum += instr.readInt(); sum += instr.readInt(); sum += instr.readInt(); System.out.println( The sum is: + sum ); instr.close(); catch ( IOException iox ) System.out.println(Problem reading + fileName ); 6.2.4 读二进制文件(续) 例6_10文件读写52该程序显示结果是254分析readInt方法可以从输入流中读入4个字节并将

36、其当作int型数据由于知道文件中存储的是3个int型数据,所以使用了3个读入语句如果不知道数据的个数该怎么办呢?因为DataInputStream的读入操作如遇到文件结尾就会抛出EOFException异常,所以我们可以将读操作放入try块中6.2.4 读二进制文件(续) 例6_10运行结果文件读写53将读操作放入try块中,使遇到文件结尾就会抛出EOFException异常,进入到相应的catch块中try while ( true ) sum += instr.readInt();catch ( EOFException eof ) System.out.println( The sum

37、is: + sum ); instr.close();6.2.4 读二进制文件(续) 例6_10修改文件读写54如果没有读到结尾,在读取过程中发生的异常属于IOException,这样就需要我们再加一个catch块处理这种异常一个try块后面可以跟不止一个catch块,用于处理各种可能发生的异常我们可以在上段代码后再加上用于捕捉IOException的代码段如下catch ( IOException eof ) System.out.println( Problem reading input ); instr.close(); 6.2.4 读二进制文件(续) 例6_10修改文件读写55如果c

38、atch块中的close方法也发生异常,现在就没法捕获了。解决方法可以有在main方法中抛出异常比较简单缺点是没有catch块,因而无法对异常进行进一步处理,例如给出提示信息使用嵌套的try块6.2.4 读二进制文件(续) 例6_10修改文件读写56import java.io.*;class Ex6_11 public static void main ( String args ) String fileName = c:/data1.dat ; long sum = 0; try DataInputStream instr = new DataInputStream( new Buffe

39、redInputStream(new ileInputStream(fileName); try while ( true ) sum += instr.readInt(); catch ( EOFException eof ) System.out.println( The sum is: + sum ); instr.close(); catch ( IOException iox ) System.out.println(IO Problems with + fileName ); 6.2.4 读二进制文件(续) 例6_11文件读写57由于文本文件的存储方式其实也是二进制代码,因此也可使

40、用InputStream类的方法读取用InputStream类读取文本文件并打印在屏幕上import java.io.*;public class Ex6_12 public static void main(String args) throws IOException FileInputStream s=new FileInputStream(c:/Hello.txt); int c; while (c = s.read() != -1) /读取1字节,结束返回-1 System.out.write(c); s.close(); 6.2.4 读二进制文件(续) 例6_12文件读写返回58D

41、ataOutputStream的writeByte方法public final void writeByte(int b) throws IOException将int的最不重要字节写入输出流DataInputStream的readUnsignedByte方法public final int readUnsignedByte() throws IOException从输入流中读取1字节存入int的最不重要字节6.2.4 读二进制文件(续) 读写字节文件读写59从命令行输入源文件名和目标文件名,将源文件复制为目标文件。import java.io.*; class CopyBytes publi

42、c static void main ( String args ) DataInputStream instr; DataOutputStream outstr; if ( args.length != 2 ) System.out.println(Please enter file names); return; try instr = new DataInputStream(new BufferedInputStream(new FileInputStream( args0 ); outstr = new DataOutputStream(new BufferedOutputStream

43、(new FileOutputStream( args1 ); 6.2.4 读二进制文件(续) 文件复制程序文件读写60 try int data; while ( true ) data = instr.readUnsignedByte() ; outstr.writeByte( data ) ; catch ( EOFException eof ) outstr.close(); instr.close(); return; catch ( FileNotFoundException nfx ) System.out.println(Problem opening files ); cat

44、ch ( IOException iox ) System.out.println(IO Problems ); 6.2.4 读二进制文件(续) 文件复制程序文件读写616.2.5 File 类 表示磁盘文件信息定义了一些与平台无关的方法来操纵文件创建、删除文件重命名文件判断文件的读写权限及是否存在设置和查询文件的最近修改时间等构造文件流可以使用File类的对象作为参数文件读写626.2.5 File类(续) 构造方法文件读写636.2.5 File类(续) 常用方法文件读写64例6_13 在C盘创建文件Hello.txt,如果存在则删除旧文件,不存在则直接创建新的import java.io

45、.*;public class Ex6_13 public static void main(String args) File f=new File(c:+File.separator+Hello.txt); if (f.exists() f.delete();else try f.createNewFile(); catch(Exception e) System.out.println(e.getMessage(); 6.2.5 File类(续) 例6_13文件读写65运行结果因为在例6_2中已经创建了c:Hello.txt,所以第一次运行将删除这个文件第二次运行则又创建了一个此名的空文

46、件分析在试图打开文件之前,可以使用File类的isFile方法来确定File对象是否代表一个文件而非目录)还可通过exists方法判断同名文件或路径是否存在,进而采取正确的方法,以免造成误操作6.2.5 File类(续) 例6_13运行结果文件读写66import java.io.*; class NewCopyBytes public static void main ( String args ) DataInputStream instr; DataOutputStream outstr; if ( args.length != 2 ) System.out.println(Please

47、 Enter file names!); return; File inFile = new File( args0 ); File outFile = new File( args1 ); if ( outFile.exists() ) System.out.println( args1 + already exists); return; if ( !inFile.exists() ) System.out.println( args0 + does not exist); return; 6.2.5 File类(续) 改进的文件复制程序文件读写67 try instr = new Dat

48、aInputStream(new BufferedInputStream( new FileInputStream( inFile ); outstr = new DataOutputStream(new BufferedOutputStream( new FileOutputStream( outFile ); try int data; while ( true ) data = instr.readUnsignedByte() ; outstr.writeByte( data ) ; catch ( EOFException eof ) outstr.close(); instr.clo

49、se(); return; catch ( FileNotFoundException nfx ) System.out.println(Problem opening files ); catch ( IOException iox ) System.out.println(IO Problems ); 6.2.5 File类(续) 改进的文件复制程序文件读写686.2.7 对象序列化保存对象的信息,在需要的时候,再读取这个对象内存中的对象在程序结束时就会被垃圾回收机制清除用于对象信息存储和读取的输入输出流类:ObjectInputStreamObjectOutputStream文件读写69

50、ObjectInputStream和ObjectOutputStream实现对象的读写通过ObjectOutputStream把对象写入磁盘文件通过ObjectInputStream把对象读入程序不保存对象的transient和static类型的变量对象要想实现序列化,其所属的类必须实现Serializable接口6.2.7 对象序列化(续)ObjectInputStream/ObjectOutputStream类文件读写70必须通过另一个流构造ObjectOutputStream:FileOutputStream out = new FileOutputStream(theTime); Ob

51、jectOutputStream s = new ObjectOutputStream(out);s.writeObject(Today);s.writeObject(new Date();s.flush();6.2.7 对象序列化(续) 写入ObjectOutputStream文件读写71必须通过另一个流构造ObjectInputStream:FileInputStream in = new FileInputStream(theTime);ObjectInputStream s = new ObjectInputStream(in);String today = (String)s.rea

52、dObject();Date date = (Date)s.readObject();6.2.7 对象序列化(续) 用ObjectInputStream读入文件读写72空接口,使类的对象可实现序列化Serializable 接口的定义package java.io;public interface Serializable / theres nothing in here!;实现Serializable接口的语句public class MyClass implements Serializable .使用关键字transient可以阻止对象的某些成员被自动写入文件6.2.7 对象序列化(续)

53、 Seriealizable接口文件读写73创建一个书籍对象,并把它输出到一个文件book.dat中,然后再把该对象读出来,在屏幕上显示对象信息class Book implements Serializable int id;String name;String author;float price;public Book(int id,String name,String author,float price) this.id=id;=name;this.author=author;this.price=price;6.2.7 对象序列化(续) 例6_17文件读写74im

54、port java.io.*;public class Ex6_17 public static void main(String args) throws IOException,ClassNotFoundException Book book=new Book(100032,Java Programming Skills,Wang Sir,30); ObjectOutputStream oos=new ObjectOutputStream( new FileOutputStream(c:/book.dat); oos.writeObject(book); oos.close(); 6.2.

55、7 对象序列化(续) 例6_17文件读写75 book=null; ObjectInputStream ois=new ObjectInputStream( new FileInputStream(c:/book.dat); book=(Book)ois.readObject(); ois.close(); System.out.println(ID is:+book.id); System.out.println(name is:+); System.out.println(author is:+book.author); System.out.println(price

56、is:+book.price);6.2.7 对象序列化(续) 例6_17文件读写76运行结果将生成book.dat文件,并在屏幕显示:ID is:100032name is:Java Programming Skillsauthor is:Wang Sirprice is:30.0说明如果希望增加Book类的功能,使其还能够具有借书方法borrowBook,并保存借书人的借书号borrowerID,可对Book类添加如下内容:transient int borrowerID;public void borrowBook(int ID)this.borrowerID=ID;6.2.7 对象序列化

57、(续) 例6_17运行结果文件读写77在main方法中创建了Book类的一个对象后,紧接着调用borrowBook方法book.borrowBook(2018);从读入的对象中输出borrowerIDSystem.out.println(Borrower ID is:+book.borrowerID);运行结果显示borrrowID为0,因为声明为transient,所以不保存如果去掉transient关键子,则可以正确读出2018。这对于保护比较重要的信息(例如密码等)是很有必要的6.2.7 对象序列化(续) 例6_17修改文件读写78Externalizable 接口实现该接口可以控制对象

58、的读写API中的说明为public interface Externalizable extends Serializable其中有两个方法writeExternal()和readExternal(),因此实现该接口的类必须实现这两个方法ObjectOutputStream的writeObject()方法只写入对象的标识,然后调用对象所属类的writeExternal()ObjectInputStream的readObject()方法调用对象所属类的readExternal()6.2.7 对象序列化(续) Externalizable接口文件读写79import java.io.*;impor

59、t java.util.*;public class Blip3 implements Externalizable int i; String s; public Blip3() System.out.println(Blip3 Constructor); public Blip3(String x, int a) System.out.println(Blip3(String x, int a); s = x; i = a; public String toString() return s + i; 6.2.7 对象序列化(续)Thinking in Java c11:Blip3.jav

60、a文件读写80public void writeExternal(ObjectOutput out) throws IOException System.out.println(Blip3.writeExternal); / You must do this: out.writeObject(s); out.writeInt(i); public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException System.out.println(Blip3.readExternal); / You mu

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论