對象操作流 可以用於讀寫任意類型的對象 ObjectOutputStream :對象輸出字元流 WriteObject ObjectInputStream :對象輸入字元流 ReadObject 註意: 使用對象輸出流寫出對象,只能使用對象輸入流來讀取對象 只能將支持java.io.Serializ ...
//讀取鍵盤錄入的數據寫到a.txt
//方式一
private static void method() throws IOException {
//創建輸入流對象
InputStream is = System.in;
Reader r = new InputStreamReader(is);
//創建輸出流對象
FileWriter fw = new FileWriter("a.txt");
//讀取數據
byte[] bys = new byte[1024];
int len;
while((len = is.read(bys))!=-1)
{
fw.write(new String(bys, 0, len));
fw.flush();
}
fw.close();
is.close();
}
//方式二
private static void method2() throws IOException {
//創建輸入流對象
InputStream is = System.in;
Reader r = new InputStreamReader(is);
//創建輸出流對象
FileWriter fw = new FileWriter("a.txt");
//讀取數據
char[] chs = new char[1024];
int len;
while((len = r.read(chs))!=-1)
{
fw.write(chs, 0 , len);
fw.flush();
}
fw.close();
r.close();
}
// 將文件中的數據輸出到控制台
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
//OutputStream os = System.out;
//Writer w = new OutputStreamWriter(System.out);
//BufferedWriter bw = new BufferedWriter(w);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String line;
while((line = br.readLine())!=null)
{
bw.write(line);
bw.newLine();
}
bw.close();
br.close();
//使用列印流複製文本文件
private static void method3() throws FileNotFoundException, IOException {
//創建輸入流對象
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
//創建列印流對象
PrintWriter pw = new PrintWriter(new FileWriter("b.txt"),true); // 此處true,是自動刷新
String line;
while((line = br.readLine())!=null)
{
pw.println(line);
}
pw.close();
br.close();
}
//使用位元組流進行文件的複製(複製二進位文件)
private static void method() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("這裡是一個文件名");
FileOutputStream fos = new FileOutputStream("另一個問件名");
// 一次讀一個位元組
// int by;
// while((by = fis.read() ) !=-1){
// fos.write(by);
// }
// 一次讀取一個字元數組
int len;
byte[] bys = new byte[1024];
while((len = fis.read(bys))!=1){
fos.write(len);
}
fis.close();
fos.close();
}
對象操作流
可以用於讀寫任意類型的對象
ObjectOutputStream :對象輸出字元流
WriteObject
ObjectInputStream :對象輸入字元流
ReadObject
註意:
使用對象輸出流寫出對象,只能使用對象輸入流來讀取對象
只能將支持java.io.Serializable 介面的對象寫入流中
Serializable:序列號,是一個標識介面,只起表示作用,沒有方法
當一個類的對象需要進行IO流進行讀寫的時候,這個類必須實現此介面
//eg:創建用於文件讀寫的學生類對象
public class Student implements Serializable{
private static final long serialVersionUID = -4114259825335049236L; // 固定序列號
String name;
int age;
public Student(String name,int age) {
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
//將學生對象寫入文件
private static void method() throws IOException, FileNotFoundException {
//創建對象輸出流的對象
//FileOutputStream fos = new FileOutputStream("a.txt"); // 位元組輸出流
//ObjectOutputStream oos = new ObjectOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"));
//創建學生對象
Student s = new Student("zhangsan", 18);
Student s2 = new Student("lisi",19);
//寫出學生對象
oos.writeObject(s);
oos.writeObject(s2);
//釋放資源
oos.close();
}
//將文件中的學生對象讀取出來
private static void method2() throws IOException, FileNotFoundException, ClassNotFoundException {
//創建對象輸入流的對象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt"));
/* //讀取對象
java.lang.Object obj = ois.readObject();
System.out.println(obj);
java.lang.Object obj2 = ois.readObject();
System.out.println(obj2);
java.lang.Object obj3 = ois.readObject();
System.out.println(obj3);*/
try{
while(true)
{
java.lang.Object obj = ois.readObject();
System.out.println(obj);
}
} catch(EOFException e){
System.out.println("讀到了文件的末尾");
}
//釋放資源
ois.close();
}
// 另一種方式寫入
private static void method3() throws IOException, FileNotFoundException {
// 另一種寫入對象文件的方式(將所有對象寫入到集合對象中)
//創建對象輸出流對象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("b.txt"));
//創建集合對象
ArrayList<Student> list = new ArrayList<Student>();
//添加學生對象
list.add(new Student("wangwu", 19));
list.add(new Student("zhaoliu", 20));
//寫出集合對象
oos.writeObject(list);
//釋放資源
oos.close();
}
//另一種讀取方式
private static void method4() throws IOException, FileNotFoundException, ClassNotFoundException {
//創建對象輸出流對象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("b.txt"));
//讀取數據
java.lang.Object obj = ois.readObject();
//System.out.println(obj);
// 向下轉型,獲取具體的子類對象
ArrayList<Student> list = (ArrayList<Student>) obj;
for(Student stu:list)
{
System.out.println(stu);
}
//釋放資源
ois.close();
}