1,从磁盘中读取数据:
package com.wjb.demo;
import java.io.*;
/**
* Created by wjb on 2017/5/5.
*/
public class InputStreamTest {
public static void main(String[] args) {
File file = new File("F:\\Wjb.java");
try {
BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
String info = null;
StringBuffer sb = new StringBuffer();
while((info = bf.readLine()) != null){
sb.append(info);
}
bf.close();
System.out.println(sb.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2,序列化与反序列化:
对象必须实现serializable接口
package com.wjb.demo;
import java.io.*;
/**
* Created by wjb on 2017/5/5.
*/
public class SerializableTest implements Serializable {
private static final long serialVersionUID = 4466065210971417002L;
public int num = 100;
public static void main(String[] args) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("F:\\aaaa.dat"));
SerializableTest serializableTest = new SerializableTest();
oos.writeObject(serializableTest);
oos.flush();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("F:\\aaaa.dat"));
try {
SerializableTest serializableTest2 = (SerializableTest) ois.readObject();
System.out.println(serializableTest2.num);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}