java中的IO详解(下)
BufferedReader的小例子
注意:?BufferedReader只能接受字符流的緩沖區,因為每一個中文需要占據兩個字節,所以需要將System.in這個字節輸入流變為字符輸入流,采用:
1?BufferedReader?buf?=?new?BufferedReader(??
2?????????????????new?InputStreamReader(System.in));?
下面給一個實例:
3?import?java.io.BufferedReader;??
4?import?java.io.IOException;??
5?import?java.io.InputStreamReader;??
6??
7?/**??
8??*?使用緩沖區從鍵盤上讀入內容??
9??*?*/?
10?public?class?BufferedReaderDemo{??
11?????public?static?void?main(String[]?args){??
12?????????BufferedReader?buf?=?new?BufferedReader(??
13?????????????????new?InputStreamReader(System.in));??
14?????????String?str?=?null;??
15?????????System.out.println("請輸入內容");??
16?????????try{??
17?????????????str?=?buf.readLine();??
18?????????}catch(IOException?e){??
19?????????????e.printStackTrace();??
20?????????}??
21?????????System.out.println("你輸入的內容是:"?+?str);??
22?????}??
23?}?
運行結果:
請輸入內容
dasdas
你輸入的內容是:dasdas
Scanner類
其實我們比較常用的是采用Scanner類來進行數據輸入,下面來給一個Scanner的例子吧
24?import?java.util.Scanner;??
25??
26?/**??
27??*?Scanner的小例子,從鍵盤讀數據??
28??*?*/?
29?public?class?ScannerDemo{??
30?????public?static?void?main(String[]?args){??
31?????????Scanner?sca?=?new?Scanner(System.in);??
32?????????//?讀一個整數??
33?????????int?temp?=?sca.nextInt();??
34?????????System.out.println(temp);??
35?????????//讀取浮點數??
36?????????float?flo=sca.nextFloat();??
37?????????System.out.println(flo);??
38?????????//讀取字符??
39?????????//...等等的,都是一些太基礎的,就不師范了。??
40?????}??
41?}?
其實Scanner可以接受任何的輸入流
下面給一個使用Scanner類從文件中讀出內容
42?import?java.io.File;??
43?import?java.io.FileNotFoundException;??
44?import?java.util.Scanner;??
45??
46?/**??
47??*?Scanner的小例子,從文件中讀內容??
48??*?*/?
49?public?class?ScannerDemo{??
50?????public?static?void?main(String[]?args){??
51??
52?????????File?file?=?new?File("d:"?+?File.separator?+?"hello.txt");??
53?????????Scanner?sca?=?null;??
54?????????try{??
55?????????????sca?=?new?Scanner(file);??
56?????????}catch(FileNotFoundException?e){??
57?????????????e.printStackTrace();??
58?????????}??
59?????????String?str?=?sca.next();??
60?????????System.out.println("從文件中讀取的內容是:"?+?str);??
61?????}??
62?}?
【運行結果】:
從文件中讀取的內容是:這些文件中的內容哦!
數據操作流DataOutputStream、DataInputStream類
63?import?java.io.DataOutputStream;??
64?import?java.io.File;??
65?import?java.io.FileOutputStream;??
66?import?java.io.IOException;??
67??
68?public?class?DataOutputStreamDemo{??
69?????public?static?void?main(String[]?args)?throws?IOException{??
70?????????File?file?=?new?File("d:"?+?File.separator?+?"hello.txt");??
71?????????char[]?ch?=?{?'A',?'B',?'C'?};??
72?????????DataOutputStream?out?=?null;??
73?????????out?=?new?DataOutputStream(new?FileOutputStream(file));??
74?????????for(char?temp?:?ch){??
75?????????????out.writeChar(temp);??
76?????????}??
77?????????out.close();??
78?????}??
79?}?
A?B?C
現在我們在上面例子的基礎上,使用DataInputStream讀出內容
80?import?java.io.DataInputStream;??
81?import?java.io.File;??
82?import?java.io.FileInputStream;??
83?import?java.io.IOException;??
84??
85?public?class?DataOutputStreamDemo{??
86?????public?static?void?main(String[]?args)?throws?IOException{??
87?????????File?file?=?new?File("d:"?+?File.separator?+?"hello.txt");??
88?????????DataInputStream?input?=?new?DataInputStream(new?FileInputStream(file));??
89?????????char[]?ch?=?new?char[10];??
90?????????int?count?=?0;??
91?????????char?temp;??
92?????????while((temp?=?input.readChar())?!=?'C'){??
93?????????????ch[count++]?=?temp;??
94?????????}??
95?????????System.out.println(ch);??
96?????}??
97?}?
【運行結果】:
AB
合并流?SequenceInputStream
SequenceInputStream主要用來將2個流合并在一起,比如將兩個txt中的內容合并為另外一個txt。下面給出一個實例:
98?import?java.io.File;??
99?import?java.io.FileInputStream;??
100?import?java.io.FileOutputStream;??
101?import?java.io.IOException;??
102?import?java.io.InputStream;??
103?import?java.io.OutputStream;??
104?import?java.io.SequenceInputStream;??
105??
106?/**??
107??*?將兩個文本文件合并為另外一個文本文件??
108??*?*/?
109?public?class?SequenceInputStreamDemo{??
110?????public?static?void?main(String[]?args)?throws?IOException{??
111?????????File?file1?=?new?File("d:"?+?File.separator?+?"hello1.txt");??
112?????????File?file2?=?new?File("d:"?+?File.separator?+?"hello2.txt");??
113?????????File?file3?=?new?File("d:"?+?File.separator?+?"hello.txt");??
114?????????InputStream?input1?=?new?FileInputStream(file1);??
115?????????InputStream?input2?=?new?FileInputStream(file2);??
116?????????OutputStream?output?=?new?FileOutputStream(file3);??
117?????????//?合并流??
118?????????SequenceInputStream?sis?=?new?SequenceInputStream(input1,?input2);??
119?????????int?temp?=?0;??
120?????????while((temp?=?sis.read())?!=?-1){??
121?????????????output.write(temp);??
122?????????}??
123?????????input1.close();??
124?????????input2.close();??
125?????????output.close();??
126?????????sis.close();??
127?????}??
128?}?
【運行結果】
結果會在hello.txt文件中包含hello1.txt和hello2.txt文件中的內容。
文件壓縮?ZipOutputStream類
先舉一個壓縮單個文件的例子吧:
129?import?java.io.File;??
130?import?java.io.FileInputStream;??
131?import?java.io.FileOutputStream;??
132?import?java.io.IOException;??
133?import?java.io.InputStream;??
134?import?java.util.zip.ZipEntry;??
135?import?java.util.zip.ZipOutputStream;??
136??
137?public?class?ZipOutputStreamDemo1{??
138?????public?static?void?main(String[]?args)?throws?IOException{??
139?????????File?file?=?new?File("d:"?+?File.separator?+?"hello.txt");??
140?????????File?zipFile?=?new?File("d:"?+?File.separator?+?"hello.zip");??
141?????????InputStream?input?=?new?FileInputStream(file);??
142?????????ZipOutputStream?zipOut?=?new?ZipOutputStream(new?FileOutputStream(??
143?????????????????zipFile));??
144?????????zipOut.putNextEntry(new?ZipEntry(file.getName()));??
145?????????//?設置注釋??
146?????????zipOut.setComment("hello");??
147?????????int?temp?=?0;??
148?????????while((temp?=?input.read())?!=?-1){??
149?????????????zipOut.write(temp);??
150?????????}??
151?????????input.close();??
152?????????zipOut.close();??
153?????}??
154?}?
【運行結果】
運行結果之前,我創建了一個hello.txt的文件,原本大小56個字節,但是壓縮之后產生hello.zip之后,居然變成了175個字節,有點搞不懂。
不過結果肯定是正確的,我只是提出我的一個疑問而已。
上面的這個例子測試的是壓縮單個文件,下面的們來看看如何壓縮多個文件。
155?import?java.io.File;??
156?import?java.io.FileInputStream;??
157?import?java.io.FileOutputStream;??
158?import?java.io.IOException;??
159?import?java.io.InputStream;??
160?import?java.util.zip.ZipEntry;??
161?import?java.util.zip.ZipOutputStream;??
162??
163?/**??
164??*?一次性壓縮多個文件??
165??*?*/?
166?public?class?ZipOutputStreamDemo2{??
167?????public?static?void?main(String[]?args)?throws?IOException{??
168?????????//?要被壓縮的文件夾??
169?????????File?file?=?new?File("d:"?+?File.separator?+?"temp");??
170?????????File?zipFile?=?new?File("d:"?+?File.separator?+?"zipFile.zip");??
171?????????InputStream?input?=?null;??
172?????????ZipOutputStream?zipOut?=?new?ZipOutputStream(new?FileOutputStream(??
173?????????????????zipFile));??
174?????????zipOut.setComment("hello");??
175?????????if(file.isDirectory()){??
176?????????????File[]?files?=?file.listFiles();??
177?????????????for(int?i?=?0;?i?<?files.length;?++i){??
178?????????????????input?=?new?FileInputStream(files[i]);??
179?????????????????zipOut.putNextEntry(new?ZipEntry(file.getName()??
180?????????????????????????+?File.separator?+?files[i].getName()));??
181?????????????????int?temp?=?0;??
182?????????????????while((temp?=?input.read())?!=?-1){??
183?????????????????????zipOut.write(temp);??
184?????????????????}??
185?????????????????input.close();??
186?????????????}??
187?????????}??
188?????????zipOut.close();??
189?????}??
190?}?
【運行結果】
先看看要被壓縮的文件吧:
接下來看看壓縮之后的:
大家自然想到,既然能壓縮,自然能解壓縮,在談解壓縮之前,我們會用到一個ZipFile類,先給一個這個例子吧。java中的每一個壓縮文件都是可以使用ZipFile來進行表示的
191?import?java.io.File;??
192?import?java.io.IOException;??
193?import?java.util.zip.ZipFile;??
194??
195?/**??
196??*?ZipFile演示??
197??*?*/?
198?public?class?ZipFileDemo{??
199?????public?static?void?main(String[]?args)?throws?IOException{??
200?????????File?file?=?new?File("d:"?+?File.separator?+?"hello.zip");??
201?????????ZipFile?zipFile?=?new?ZipFile(file);??
202?????????System.out.println("壓縮文件的名稱為:"?+?zipFile.getName());??
203?????}??
204?}?
【運行結果】:
壓縮文件的名稱為:d:\hello.zip
現在我們呢是時候來看看如何加壓縮文件了,和之前一樣,先讓我們來解壓單個壓縮文件(也就是壓縮文件中只有一個文件的情況),我們采用前面的例子產生的壓縮文件hello.zip
205?import?java.io.File;??
206?import?java.io.FileOutputStream;??
207?import?java.io.IOException;??
208?import?java.io.InputStream;??
209?import?java.io.OutputStream;??
210?import?java.util.zip.ZipEntry;??
211?import?java.util.zip.ZipFile;??
212??
213?/**??
214??*?解壓縮文件(壓縮文件中只有一個文件的情況)??
215??*?*/?
216?public?class?ZipFileDemo2{??
217?????public?static?void?main(String[]?args)?throws?IOException{??
218?????????File?file?=?new?File("d:"?+?File.separator?+?"hello.zip");??
219?????????File?outFile?=?new?File("d:"?+?File.separator?+?"unZipFile.txt");??
220?????????ZipFile?zipFile?=?new?ZipFile(file);??
221?????????ZipEntry?entry?=?zipFile.getEntry("hello.txt");??
222?????????InputStream?input?=?zipFile.getInputStream(entry);??
223?????????OutputStream?output?=?new?FileOutputStream(outFile);??
224?????????int?temp?=?0;??
225?????????while((temp?=?input.read())?!=?-1){??
226?????????????output.write(temp);??
227?????????}??
228?????????input.close();??
229?????????output.close();??
230?????}??
231?}?
【運行結果】:
解壓縮之前:
這個壓縮文件還是175字節
解壓之后產生:
又回到了56字節,表示郁悶。
現在讓我們來解壓一個壓縮文件中包含多個文件的情況吧
ZipInputStream類
當我們需要解壓縮多個文件的時候,ZipEntry就無法使用了,如果想操作更加復雜的壓縮文件,我們就必須使用ZipInputStream類
232?import?java.io.File;??
233?import?java.io.FileInputStream;??
234?import?java.io.FileOutputStream;??
235?import?java.io.IOException;??
236?import?java.io.InputStream;??
237?import?java.io.OutputStream;??
238?import?java.util.zip.ZipEntry;??
239?import?java.util.zip.ZipFile;??
240?import?java.util.zip.ZipInputStream;??
241??
242?/**??
243??*?解壓縮一個壓縮文件中包含多個文件的情況??
244??*?*/?
245?public?class?ZipFileDemo3{??
246?????public?static?void?main(String[]?args)?throws?IOException{??
247?????????File?file?=?new?File("d:"?+?File.separator?+?"zipFile.zip");??
248?????????File?outFile?=?null;??
249?????????ZipFile?zipFile?=?new?ZipFile(file);??
250?????????ZipInputStream?zipInput?=?new?ZipInputStream(new?FileInputStream(file));??
251?????????ZipEntry?entry?=?null;??
252?????????InputStream?input?=?null;??
253?????????OutputStream?output?=?null;??
254?????????while((entry?=?zipInput.getNextEntry())?!=?null){??
255?????????????System.out.println("解壓縮"?+?entry.getName()?+?"文件");??
256?????????????outFile?=?new?File("d:"?+?File.separator?+?entry.getName());??
257?????????????if(!outFile.getParentFile().exists()){??
258?????????????????outFile.getParentFile().mkdir();??
259?????????????}??
260?????????????if(!outFile.exists()){??
261?????????????????outFile.createNewFile();??
262?????????????}??
263?????????????input?=?zipFile.getInputStream(entry);??
264?????????????output?=?new?FileOutputStream(outFile);??
265?????????????int?temp?=?0;??
266?????????????while((temp?=?input.read())?!=?-1){??
267?????????????????output.write(temp);??
268?????????????}??
269?????????????input.close();??
270?????????????output.close();??
271?????????}??
272?????}??
273?}?
【運行結果】:
被解壓的文件:
解壓之后再D盤下會出現一個temp文件夾,里面內容:
PushBackInputStream回退流
274?import?java.io.ByteArrayInputStream;??
275?import?java.io.IOException;??
276?import?java.io.PushbackInputStream;??
277??
278?/**??
279??*?回退流操作??
280??*?*/?
281?public?class?PushBackInputStreamDemo{??
282?????public?static?void?main(String[]?args)?throws?IOException{??
283?????????String?str?=?"hello,rollenholt";??
284?????????PushbackInputStream?push?=?null;??
285?????????ByteArrayInputStream?bat?=?null;??
286?????????bat?=?new?ByteArrayInputStream(str.getBytes());??
287?????????push?=?new?PushbackInputStream(bat);??
288?????????int?temp?=?0;??
289?????????while((temp?=?push.read())?!=?-1){??
290?????????????if(temp?==?','){??
291?????????????????push.unread(temp);??
292?????????????????temp?=?push.read();??
293?????????????????System.out.print("(回退"?+?(char)?temp?+?")?");??
294?????????????}else{??
295?????????????????System.out.print((char)?temp);??
296?????????????}??
297?????????}??
298?????}??
299?}?
【運行結果】:
hello(回退,)?rollenholt
300?/**??
301??*?取得本地的默認編碼??
302??*?*/?
303?public?class?CharSetDemo{??
304?????public?static?void?main(String[]?args){??
305?????????System.out.println("系統默認編碼為:"?+?System.getProperty("file.encoding"));??
306?????}??
307?}?
【運行結果】:
系統默認編碼為:GBK
亂碼的產生:
308?import?java.io.File;??
309?import?java.io.FileOutputStream;??
310?import?java.io.IOException;??
311?import?java.io.OutputStream;??
312??
313?/**??
314??*?亂碼的產生??
315??*?*/?
316?public?class?CharSetDemo2{??
317?????public?static?void?main(String[]?args)?throws?IOException{??
318?????????File?file?=?new?File("d:"?+?File.separator?+?"hello.txt");??
319?????????OutputStream?out?=?new?FileOutputStream(file);??
320?????????byte[]?bytes?=?"你好".getBytes("ISO8859-1");??
321?????????out.write(bytes);??
322?????????out.close();??
323?????}??
324?}?
【運行結果】:
??
一般情況下產生亂碼,都是由于編碼不一致的問題。
對象的序列化
對象序列化就是把一個對象變為二進制數據流的一種方法。
一個類要想被序列化,就行必須實現java.io.Serializable接口。雖然這個接口中沒有任何方法,就如同之前的cloneable接口一樣。實現了這個接口之后,就表示這個類具有被序列化的能力。
先讓我們實現一個具有序列化能力的類吧:
325?import?java.io.*;??
326?/**??
327??*?實現具有序列化能力的類??
328??*?*/?
329?public?class?SerializableDemo?implements?Serializable{??
330?????public?SerializableDemo(){??
331???????????
332?????}??
333?????public?SerializableDemo(String?name,?int?age){??
334?????????this.name=name;??
335?????????this.age=age;??
336?????}??
337?????@Override?
338?????public?String?toString(){??
339?????????return?"姓名:"+name+"??年齡:"+age;??
340?????}??
341?????private?String?name;??
342?????private?int?age;??
343?}?
這個類就具有實現序列化能力,
在繼續將序列化之前,先將一下ObjectInputStream和ObjectOutputStream這兩個類
先給一個ObjectOutputStream的例子吧:
344?import?java.io.Serializable;??
345?import?java.io.File;??
346?import?java.io.FileOutputStream;??
347?import?java.io.IOException;??
348?import?java.io.ObjectOutputStream;??
349??
350?/**??
351??*?實現具有序列化能力的類??
352??*?*/?
353?public?class?Person?implements?Serializable{??
354?????public?Person(){??
355??
356?????}??
357??
358?????public?Person(String?name,?int?age){??
359?????????this.name?=?name;??
360?????????this.age?=?age;??
361?????}??
362??
363?????@Override?
364?????public?String?toString(){??
365?????????return?"姓名:"?+?name?+?"??年齡:"?+?age;??
366?????}??
367??
368?????private?String?name;??
369?????private?int?age;??
370?}??
371?/**??
372??*?示范ObjectOutputStream??
373??*?*/?
374?public?class?ObjectOutputStreamDemo{??
375?????public?static?void?main(String[]?args)?throws?IOException{??
376?????????File?file?=?new?File("d:"?+?File.separator?+?"hello.txt");??
377?????????ObjectOutputStream?oos?=?new?ObjectOutputStream(new?FileOutputStream(??
378?????????????????file));??
379?????????oos.writeObject(new?Person("rollen",?20));??
380?????????oos.close();??
381?????}??
382?}?
【運行結果】:
當我們查看產生的hello.txt的時候,看到的是亂碼,呵呵。因為是二進制文件。
雖然我們不能直接查看里面的內容,但是我們可以使用ObjectInputStream類查看:
383?import?java.io.File;??
384?import?java.io.FileInputStream;??
385?import?java.io.ObjectInputStream;??
386??
387?/**??
388??*?ObjectInputStream示范??
389??*?*/?
390?public?class?ObjectInputStreamDemo{??
391?????public?static?void?main(String[]?args)?throws?Exception{??
392?????????File?file?=?new?File("d:"?+?File.separator?+?"hello.txt");??
393?????????ObjectInputStream?input?=?new?ObjectInputStream(new?FileInputStream(??
394?????????????????file));??
395?????????Object?obj?=?input.readObject();??
396?????????input.close();??
397?????????System.out.println(obj);??
398?????}??
399?}?
【運行結果】
姓名:rollen?年齡:20
到底序列化什么內容呢?
其實只有屬性會被序列化。
Externalizable接口
被Serializable接口聲明的類的對象的屬性都將被序列化,但是如果想自定義序列化的內容的時候,就需要實現Externalizable接口。
當一個類要使用Externalizable這個接口的時候,這個類中必須要有一個無參的構造函數,如果沒有的話,在構造的時候會產生異常,這是因為在反序列話的時候會默認調用無參的構造函數。
現在我們來演示一下序列化和反序列話:
400?package?IO;??
401??
402?import?java.io.Externalizable;??
403?import?java.io.File;??
404?import?java.io.FileInputStream;??
405?import?java.io.FileOutputStream;??
406?import?java.io.IOException;??
407?import?java.io.ObjectInput;??
408?import?java.io.ObjectInputStream;??
409?import?java.io.ObjectOutput;??
410?import?java.io.ObjectOutputStream;??
411??
412?/**??
413??*?序列化和反序列化的操作??
414??*?*/?
415?public?class?ExternalizableDemo{??
416?????public?static?void?main(String[]?args)?throws?Exception{??
417?????????ser();?//?序列化??
418?????????dser();?//?反序列話??
419?????}??
420??
421?????public?static?void?ser()?throws?Exception{??
422?????????File?file?=?new?File("d:"?+?File.separator?+?"hello.txt");??
423?????????ObjectOutputStream?out?=?new?ObjectOutputStream(new?FileOutputStream(??
424?????????????????file));??
425?????????out.writeObject(new?Person("rollen",?20));??
426?????????out.close();??
427?????}??
428??
429?????public?static?void?dser()?throws?Exception{??
430?????????File?file?=?new?File("d:"?+?File.separator?+?"hello.txt");??
431?????????ObjectInputStream?input?=?new?ObjectInputStream(new?FileInputStream(??
432?????????????????file));??
433?????????Object?obj?=?input.readObject();??
434?????????input.close();??
435?????????System.out.println(obj);??
436?????}??
437?}??
438??
439?class?Person?implements?Externalizable{??
440?????public?Person(){??
441??
442?????}??
443??
444?????public?Person(String?name,?int?age){??
445?????????this.name?=?name;??
446?????????this.age?=?age;??
447?????}??
448??
449?????@Override?
450?????public?String?toString(){??
451?????????return?"姓名:"?+?name?+?"??年齡:"?+?age;??
452?????}??
453??
454?????//?復寫這個方法,根據需要可以保存的屬性或者具體內容,在序列化的時候使用??
455?????@Override?
456?????public?void?writeExternal(ObjectOutput?out)?throws?IOException{??
457?????????out.writeObject(this.name);??
458?????????out.writeInt(age);??
459?????}??
460??
461?????//?復寫這個方法,根據需要讀取內容?反序列話的時候需要??
462?????@Override?
463?????public?void?readExternal(ObjectInput?in)?throws?IOException,??
464?????????????ClassNotFoundException{??
465?????????this.name?=?(String)?in.readObject();??
466?????????this.age?=?in.readInt();??
467?????}??
468??
469?????private?String?name;??
470?????private?int?age;??
471?}?
【運行結果】:
姓名:rollen?年齡:20
本例中,我們將全部的屬性都保留了下來,
Serializable接口實現的操作其實是吧一個對象中的全部屬性進行序列化,當然也可以使用我們上使用是Externalizable接口以實現部分屬性的序列化,但是這樣的操作比較麻煩,
當我們使用Serializable接口實現序列化操作的時候,如果一個對象的某一個屬性不想被序列化保存下來,那么我們可以使用transient關鍵字進行說明:
下面舉一個例子:
472?package?IO;??
473??
474?import?java.io.File;??
475?import?java.io.FileInputStream;??
476?import?java.io.FileOutputStream;??
477?import?java.io.ObjectInputStream;??
478?import?java.io.ObjectOutputStream;??
479?import?java.io.Serializable;??
480??
481?/**??
482??*?序列化和反序列化的操作??
483??*?*/?
484?public?class?serDemo{??
485?????public?static?void?main(String[]?args)?throws?Exception{??
486?????????ser();?//?序列化??
487?????????dser();?//?反序列話??
488?????}??
489??
490?????public?static?void?ser()?throws?Exception{??
491?????????File?file?=?new?File("d:"?+?File.separator?+?"hello.txt");??
492?????????ObjectOutputStream?out?=?new?ObjectOutputStream(new?FileOutputStream(??
493?????????????????file));??
494?????????out.writeObject(new?Person1("rollen",?20));??
495?????????out.close();??
496?????}??
497??
498?????public?static?void?dser()?throws?Exception{??
499?????????File?file?=?new?File("d:"?+?File.separator?+?"hello.txt");??
500?????????ObjectInputStream?input?=?new?ObjectInputStream(new?FileInputStream(??
501?????????????????file));??
502?????????Object?obj?=?input.readObject();??
503?????????input.close();??
504?????????System.out.println(obj);??
505?????}??
506?}??
507??
508?class?Person1?implements?Serializable{??
509?????public?Person1(){??
510??
511?????}??
512??
513?????public?Person1(String?name,?int?age){??
514?????????this.name?=?name;??
515?????????this.age?=?age;??
516?????}??
517??
518?????@Override?
519?????public?String?toString(){??
520?????????return?"姓名:"?+?name?+?"??年齡:"?+?age;??
521?????}??
522??
523?????//?注意這里??
524?????private?transient?String?name;??
525?????private?int?age;??
526?}?
【運行結果】:
姓名:null?年齡:20
最后在給一個序列化一組對象的例子吧:
527?import?java.io.File;??
528?import?java.io.FileInputStream;??
529?import?java.io.FileOutputStream;??
530?import?java.io.ObjectInputStream;??
531?import?java.io.ObjectOutputStream;??
532?import?java.io.Serializable;??
533??
534?/**??
535??*?序列化一組對象??
536??*?*/?
537?public?class?SerDemo1{??
538?????public?static?void?main(String[]?args)?throws?Exception{??
539?????????Student[]?stu?=?{?new?Student("hello",?20),?new?Student("world",?30),??
540?????????????????new?Student("rollen",?40)?};??
541?????????ser(stu);??
542?????????Object[]?obj?=?dser();??
543?????????for(int?i?=?0;?i?<?obj.length;?++i){??
544?????????????Student?s?=?(Student)?obj[i];??
545?????????????System.out.println(s);??
546?????????}??
547?????}??
548??
549?????//?序列化??
550?????public?static?void?ser(Object[]?obj)?throws?Exception{??
551?????????File?file?=?new?File("d:"?+?File.separator?+?"hello.txt");??
552?????????ObjectOutputStream?out?=?new?ObjectOutputStream(new?FileOutputStream(??
553?????????????????file));??
554?????????out.writeObject(obj);??
555?????????out.close();??
556?????}??
557??
558?????//?反序列化??
559?????public?static?Object[]?dser()?throws?Exception{??
560?????????File?file?=?new?File("d:"?+?File.separator?+?"hello.txt");??
561?????????ObjectInputStream?input?=?new?ObjectInputStream(new?FileInputStream(??
562?????????????????file));??
563?????????Object[]?obj?=?(Object[])?input.readObject();??
564?????????input.close();??
565?????????return?obj;??
566?????}??
567?}??
568??
569?class?Student?implements?Serializable{??
570?????public?Student(){??
571??
572?????}??
573??
574?????public?Student(String?name,?int?age){??
575?????????this.name?=?name;??
576?????????this.age?=?age;??
577?????}??
578??
579?????@Override?
580?????public?String?toString(){??
581?????????return?"姓名:??"?+?name?+?"??年齡:"?+?age;??
582?????}??
583??
584?????private?String?name;??
585?????private?int?age;??
586?}?
【運行結果】:
姓名:?hello?年齡:20
姓名:?world?年齡:30
姓名:?rollen?年齡:40
總結
以上是生活随笔為你收集整理的java中的IO详解(下)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中的IO详解(上)
- 下一篇: Java I/O中的对象序列化