1.什麼是序列流面試
2.使用方式數組
FileInputStream fis1 = new FileInputStream("a.txt"); //建立輸入流對象,關聯a.txt FileInputStream fis2 = new FileInputStream("b.txt"); //建立輸入流對象,關聯b.txt SequenceInputStream sis = new SequenceInputStream(fis1, fis2); //將兩個流整合成一個流 FileOutputStream fos = new FileOutputStream("c.txt"); //建立輸出流對象,關聯c.txt int b; while((b = sis.read()) != -1) { //用整合後的讀 fos.write(b); //寫到指定文件上 } sis.close(); fos.close();
FileInputStream fis1 = new FileInputStream("a.txt"); //建立輸入流對象,關聯a.txt FileInputStream fis2 = new FileInputStream("b.txt"); //建立輸入流對象,關聯b.txt FileInputStream fis3 = new FileInputStream("c.txt"); //建立輸入流對象,關聯c.txt Vector<InputStream> v = new Vector<>(); //建立vector集合對象 v.add(fis1); //將流對象添加 v.add(fis2); v.add(fis3); Enumeration<InputStream> en = v.elements(); //獲取枚舉引用 SequenceInputStream sis = new SequenceInputStream(en); //傳遞給SequenceInputStream構造 FileOutputStream fos = new FileOutputStream("d.txt"); int b; while((b = sis.read()) != -1) { fos.write(b); } sis.close(); fos.close();
1.什麼是內存輸出流dom
2.使用方式優化
FileInputStream fis = new FileInputStream("a.txt"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int b; while((b = fis.read()) != -1) { baos.write(b); } //byte[] newArr = baos.toByteArray(); //將內存緩衝區中全部的字節存儲在newArr中 //System.out.println(new String(newArr)); System.out.println(baos); fis.close();
FileInputStream fis = new FileInputStream("a.txt"); //建立字節輸入流,關聯a.txt ByteArrayOutputStream baos = new ByteArrayOutputStream(); //建立內存輸出流 byte[] arr = new byte[5]; //建立字節數組,大小爲5 int len; while((len = fis.read(arr)) != -1) { //將文件上的數據讀到字節數組中 baos.write(arr, 0, len); //將字節數組的數據寫到內存緩衝區中 } System.out.println(baos); //將內存緩衝區的內容轉換爲字符串打印 fis.close();
A:隨機訪問流概述code
1.什麼是對象操做流對象
2.使用方式索引
public class Demo3_ObjectOutputStream { /** * @param args * @throws IOException * 將對象寫出,序列化 */ public static void main(String[] args) throws IOException { Person p1 = new Person("張三", 23); Person p2 = new Person("李四", 24); // FileOutputStream fos = new FileOutputStream("e.txt"); // fos.write(p1); // FileWriter fw = new FileWriter("e.txt"); // fw.write(p1); //不管是字節輸出流,仍是字符輸出流都不能直接寫出對象 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e.txt"));//建立對象輸出流 oos.writeObject(p1); oos.writeObject(p2); oos.close(); } }
public class Demo3_ObjectInputStream { /** * @param args * @throws IOException * @throws ClassNotFoundException * @throws FileNotFoundException * 讀取對象,反序列化 */ public static void main(String[] args) throws IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e.txt")); Person p1 = (Person) ois.readObject(); Person p2 = (Person) ois.readObject(); System.out.println(p1); System.out.println(p2); ois.close(); } }
* 將對象存儲在集合中寫出接口
Person p1 = new Person("張三", 23); Person p2 = new Person("李四", 24); Person p3 = new Person("馬哥", 18); Person p4 = new Person("輝哥", 20); ArrayList<Person> list = new ArrayList<>(); list.add(p1); list.add(p2); list.add(p3); list.add(p4); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("f.txt")); oos.writeObject(list); //寫出集合對象 oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("f.txt")); ArrayList<Person> list = (ArrayList<Person>)ois.readObject(); //泛型在運行期會被擦除,索引運行期至關於沒有泛型 //想去掉黃色能夠加註解 @SuppressWarnings("unchecked") for (Person person : list) { System.out.println(person); } ois.close();
注意圖片
1.什麼是數據輸入輸出流內存
2.使用方式
DataOutputStream dos = new DataOutputStream(new FileOutputStream("b.txt")); dos.writeInt(997); dos.writeInt(998); dos.writeInt(999); dos.close();
* DataInputStream(InputStream), readInt(), readLong()
DataInputStream dis = new DataInputStream(new FileInputStream("b.txt")); int x = dis.readInt(); int y = dis.readInt(); int z = dis.readInt(); System.out.println(x); System.out.println(y); System.out.println(z); dis.close();
1.什麼是打印流
PrintStream ps = System.out; ps.println(97); //其實底層用的是Integer.toString(x),將x轉換爲數字字符串打印 ps.println("xxx"); ps.println(new Person("張三", 23)); Person p = null; ps.println(p); //若是是null,就返回null,若是不是null,就調用對象的toString()
2.使用方式
PrintWriter pw = new PrintWriter(new FileOutputStream("g.txt"), true); pw.write(97); pw.print("你們好"); pw.println("你好"); //自動刷出,只針對的是println方法 pw.close();
1.什麼是標準輸入輸出流(掌握)
2.修改標準輸入輸出流(瞭解)
System.setIn(new FileInputStream("a.txt")); //修改標準輸入流 System.setOut(new PrintStream("b.txt")); //修改標準輸出流 InputStream in = System.in; //獲取標準輸入流 PrintStream ps = System.out; //獲取標準輸出流 int b; while((b = in.read()) != -1) { //從a.txt上讀取數據 ps.write(b); //將數據寫到b.txt上 } in.close(); ps.close();
System.setIn(new FileInputStream("IO圖片.png")); //改變標準輸入流 System.setOut(new PrintStream("copy.png")); //改變標準輸出流 InputStream is = System.in; //獲取標準輸入流 PrintStream ps = System.out; //獲取標準輸出流 int len; byte[] arr = new byte[1024 * 8]; while((len = is.read(arr)) != -1) { ps.write(arr, 0, len); } is.close(); ps.close();
A:BufferedReader的readLine方法。
A:Properties的概述
B:案例演示
A:Properties的特殊功能
B:案例演示
B:案例演示