其他流(常用非基本流)
阿新 • • 發佈:2020-07-30
包裝流,也叫緩衝流,高效流
字元輸出流,內建陣列[1024]
緩衝位元組流內建陣列[8192]
轉換流(本身是字元流)是位元組流到字元流的橋樑
idea預設UTF8編碼,而eclipse是gbk
序列化
注意:序列化物件所在類需先實現Serializable介面
重點:
IO流file,四種基本流
緩衝流buffer, 通過緩衝區讀寫,減少系統IO次數(輸入輸出次數),從而提高讀寫的效率。
轉換流input/output,解決不同編碼的檔案轉換問題
序列化object,節序列,寫出到檔案之後,相當於,檔案中持久儲存了一個物件的資訊;反之,該位元組序列,還可以從檔案中讀取回來,重構物件,對它進行反序列化。
序列化舉例
import java.io.*;
import java.util.ArrayList;
public class Test05 {
public static void main(String[] args) throws Exception {
//將物件寫入檔案
//ArrayList<Student> al = new ArrayList();
//al.add(new Student("fbb",18));
//al.add(new Student("zwj", 20));
//
//ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day20\\c.txt"));
//oos.writeObject(al);
//
//oos.close();
//從檔案中讀取物件
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day20\\c.txt"));
ArrayList<Student> al = (ArrayList<Student>) ois.readObject();
ois.close();
for (Student student : al) {
System.out.println(student.getName() + student.getAge());
}
}
}
class Student implements Serializable {//序列化物件所在類需先實現Serializable介面
private String name;
//private transient int age; //transient瞬時的,無法序列化,不能永久儲存
private int age;
private String address;//寫入文字後加的成員變數,此時類的class檔案已更改,要再次將物件讀取到記憶體,需記錄下之前序列號
public static final long serialVersionUID = 6845409090868647L; //將一開始的class檔案的序列號記錄為一個靜態成員常量
public Student() {
}
public Student(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
下面是一些程式碼:
//緩衝位元組流
import java.io.*;
public class Test01 {
public static void main(String[] args) throws Exception {
File f = new File("day20\\b.wmv");
f.delete();
FileInputStream fis = new FileInputStream("day20\\a.wmv");
FileOutputStream fos = new FileOutputStream("day20\\b.wmv");
BufferedInputStream bis = newBufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
long start = System.currentTimeMillis();
int ch;
byte[] arr = new byte[1024];
while ((ch = bis.read(arr)) != -1) {
bos.write(arr, 0, ch);
}
long end = System.currentTimeMillis();
bos.close();//同時也關閉了它所包裝的fos
bis.close();//同時也關閉了它所包裝的fis
//fos.close();
//fis.close();
System.out.println(end - start);
}
}
//緩衝字元流
//對每一段以“1.先帝創業...”形式開頭的出師表進行排序,輸出到一個新文字檔案中
import java.io.*;
import java.util.HashMap;
public class Test02 {
public static void main(String[] args) throwsException {
BufferedReader br = new BufferedReader(new FileReader("day20\\a.txt"));
HashMap<String,String> hm = new HashMap();
String ch;
while ((ch = br.readLine()) != null) {
String[] split = ch.split("\\.");
hm.put(split[0], split[1]);
}
br.close();
BufferedWriter bw = new BufferedWriter(new FileWriter("day20\\aa.txt"));
String key = null;
for (int i = 1; i <= hm.size(); i++) {
key = ""+i;
bw.write(key + "." + hm.get(key));
bw.newLine();
}
bw.close();
}
}
上一個程式碼可改寫:
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
public class Test03 {
public static void main(String[] args) throws Exception {
FileReader fr = new FileReader("day20\\a.txt");
BufferedReader br = new BufferedReader(fr);
ArrayList<String> al = new ArrayList();
String ch;
while ((ch = br.readLine()) != null) {
al.add(ch);
}
br.close();
Collections.sort(al);
BufferedWriter bw = new BufferedWriter(new FileWriter("day20\\aaa.txt"));
for (String s : al) {
bw.write(s);
bw.newLine();
}
bw.close();
}
}
//列印流,用途不大包裝流,也叫緩衝流,高效流 字元輸出流,內建陣列[1024] 緩衝位元組流內建陣列[8192] 轉換流(本身是字元流)是位元組流到字元流的橋樑 idea預設UTF8編碼,而eclipse是gbk 序列化 注意:序列化物件所在類需先實現Serializable介面 重點: IO流file,四種基本流 緩衝流buffer, 通過緩衝區讀寫,減少系統IO次數(輸入輸出次數),從而提高讀寫的效率。 轉換流input/output,解決不同編碼的檔案轉換問題 序列化object,節序列,寫出到檔案之後,相當於,檔案中持久儲存了一個物件的資訊;反之,該位元組序列,還可以從檔案中讀取回來,重構物件,對它進行反序列化。 序列化舉例 import java.io.*; import java.util.ArrayList; public class Test05 { public static void main(String[] args) throws Exception { //將物件寫入檔案 //ArrayList<Student> al = new ArrayList(); //al.add(new Student("fbb",18)); //al.add(new Student("zwj", 20)); // //ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day20\\c.txt")); //oos.writeObject(al); // //oos.close(); //從檔案中讀取物件 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day20\\c.txt")); ArrayList<Student> al = (ArrayList<Student>) ois.readObject(); ois.close(); for (Student student : al) { System.out.println(student.getName() + student.getAge()); } } } class Student implements Serializable {//序列化物件所在類需先實現Serializable介面 private String name; //private transient int age; //transient瞬時的,無法序列化,不能永久儲存 private int age; private String address;//寫入文字後加的成員變數,此時類的class檔案已更改,要再次將物件讀取到記憶體,需記錄下之前序列號 public static final long serialVersionUID = 6845409090868647L; //將一開始的class檔案的序列號記錄為一個靜態成員常量 public Student() { } public Student(String name, int age, String address) { this.name = name; this.age = age; this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", address='" + address + '\'' + '}'; } }
import java.io.FileOutputStream;
import java.io.PrintStream;
public class Test06 {
public static void main(String[] args) throws Exception {
PrintStream ps = new PrintStream(new FileOutputStream("day20\\d.txt"));
ps.println(666);
ps.write(97);
System.setOut(ps);//改變列印流向
System.out.println(985);
}
}