Java IO Serialization
生活随笔
收集整理的這篇文章主要介紹了
Java IO Serialization
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Java IO & Serialization
專為開卷考試準(zhǔn)備,內(nèi)容包括基本的文本文件和二進制文件的讀寫以及序列化反序列化操作
IO demo
package helloworld;import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException;public class TestIO {public static void main(String[] args) {String fileName = "test.txt";try {FileWriter fWriter = new FileWriter(fileName);fWriter.write("你好\n");fWriter.write("Hello World");fWriter.close();// 帶緩沖的寫法,寫入大量內(nèi)容的時候可以更加高效一些BufferedWriter fWriter2 = new BufferedWriter(new FileWriter("test2.txt")); // 裝飾器模式fWriter2.write("加入buffer\n");fWriter2.write("Hi World");fWriter2.close();// 讀取文件BufferedReader fReader = new BufferedReader (new FileReader(fileName));StringBuffer content = new StringBuffer();String line;while ((line = fReader.readLine())!=null) {content.append(line);}System.out.println(content);// 你好\nHello WorldfReader.close();} catch (IOException e) {e.printStackTrace();}// 二進制文件try {// 打開流DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream("test.bin"));dataOutputStream.writeInt(123);dataOutputStream.writeUTF("方浩 the great");dataOutputStream.writeDouble(1234.56);dataOutputStream.close(); // 關(guān)閉流// 讀取二進制文件DataInputStream dataInputStream = new DataInputStream(new FileInputStream("test.bin"));System.out.println(dataInputStream.readInt());System.out.println(dataInputStream.readUTF());System.out.println(dataInputStream.readDouble());dataInputStream.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}} }Serialization demo
package helloworld;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable;// 要序列化的的對象需要實現(xiàn)序列化接口 class Employee implements Serializable {/*** */private static final long serialVersionUID = 1L;String name;String address;void mailCheck() {System.out.println("Mailing a check to " + name + " " + address);}}public class TestSerialization {public static void main(String[] args) {Employee employee = new Employee();employee.name = "Tim";employee.address = "Phokka Kuan, Ambehta Peer";// 序列化try {ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("test.ser"));// 把對象存入文件out.writeObject(employee);out.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}// 反序列化try {ObjectInputStream in = new ObjectInputStream(new FileInputStream("test.ser"));Employee e = (Employee)in.readObject();in.close();e.mailCheck();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }轉(zhuǎn)載于:https://www.cnblogs.com/fanghao/p/10883720.html
總結(jié)
以上是生活随笔為你收集整理的Java IO Serialization的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python-03 爬虫相关
- 下一篇: 吴裕雄 Bootstrap 前端框架开发