JAVASE基礎-day22(IO(其他流)&Properties)
阿新 • • 發佈:2019-02-15
22.01_IO流(序列流)(瞭解)
- 1.什麼是序列流
- 序列流可以把多個位元組輸入流整合成一個, 從序列流中讀取資料時, 將從被整合的第一個流開始讀, 讀完一個之後繼續讀第二個, 以此類推.
2.使用方式
- 整合兩個: SequenceInputStream(InputStream, InputStream)
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();
22.02_IO流(序列流整合多個)(瞭解)
- 整合多個: SequenceInputStream(Enumeration)
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();
22.03_IO流(記憶體輸出流*)(掌握)
- 1.什麼是記憶體輸出流
- 該輸出流可以向記憶體中寫資料, 把記憶體當作一個緩衝區, 寫出之後可以一次性獲取出所有資料
2.使用方式
- 建立物件: new ByteArrayOutputStream()
- 寫出資料: write(int), write(byte[])
- 獲取資料: toByteArray()
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();
22.04_IO流(記憶體輸出流之黑馬面試題)(掌握)
- 定義一個檔案輸入流,呼叫read(byte[] b)方法,將a.txt檔案中的內容打印出來(byte陣列大小限制為5)
-
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();
22.05_IO流(隨機訪問流概述和讀寫資料)(瞭解)
A:隨機訪問流概述
- RandomAccessFile概述
- RandomAccessFile類不屬於流,是Object類的子類。但它融合了InputStream和OutputStream的功能。
- 支援對隨機訪問檔案的讀取和寫入。
B:read(),write(),seek()
22.06_IO流(物件操作流ObjecOutputStream)(瞭解)
- 1.什麼是物件操作流
- 該流可以將一個物件寫出, 或者讀取一個物件到程式中. 也就是執行了序列化和反序列化的操作.
2.使用方式
寫出: new ObjectOutputStream(OutputStream), writeObject()
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(); } }
22.07_IO流(物件操作流ObjectInputStream)(瞭解)
讀取: new ObjectInputStream(InputStream), readObject()
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(); } }
22.08_IO流(物件操作流優化)(瞭解)
* 將物件儲存在集合中寫出
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();
22.09_IO流(加上id號)(瞭解)
- 注意
- 要寫出的物件必須實現Serializable接口才能被序列化
- 不用必須加id號
22.10_IO流(資料輸入輸出流)(瞭解)
- 1.什麼是資料輸入輸出流
- DataInputStream, DataOutputStream可以按照基本資料型別大小讀寫資料
- 例如按Long大小寫出一個數字, 寫出時該資料佔8位元組. 讀取的時候也可以按照Long型別讀取, 一次讀取8個位元組.
2.使用方式
DataOutputStream(OutputStream), writeInt(), writeLong()
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();
22.11_IO流(列印流的概述和特點)(掌握)
1.什麼是列印流
- 該流可以很方便的將物件的toString()結果輸出, 並且自動加上換行, 而且可以使用自動刷出的模式
System.out就是一個PrintStream, 其預設向控制檯輸出資訊
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.使用方式
- 列印: print(), println()
- 自動刷出: PrintWriter(OutputStream out, boolean autoFlush, String encoding)
列印流只操作資料目的
PrintWriter pw = new PrintWriter(new FileOutputStream("g.txt"), true); pw.write(97); pw.print("大家好"); pw.println("你好"); //自動刷出,只針對的是println方法 pw.close();
22.12_IO流(標準輸入輸出流概述和輸出語句)
- 1.什麼是標準輸入輸出流(掌握)
- System.in是InputStream, 標準輸入流, 預設可以從鍵盤輸入讀取位元組資料
- System.out是PrintStream, 標準輸出流, 預設可以向Console中輸出字元和位元組資料
2.修改標準輸入輸出流(瞭解)
- 修改輸入流: System.setIn(InputStream)
- 修改輸出流: System.setOut(PrintStream)
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();
22.13_IO流(修改標準輸入輸出流拷貝圖片)(瞭解)
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();
22.14_IO流(兩種方式實現鍵盤錄入)(瞭解)
- A:BufferedReader的readLine方法。
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- B:Scanner
22.15_IO流(Properties的概述和作為Map集合的使用)(瞭解)
- A:Properties的概述
- Properties 類表示了一個持久的屬性集。
- Properties 可儲存在流中或從流中載入。
- 屬性列表中每個鍵及其對應值都是一個字串。
- B:案例演示
- Properties作為Map集合的使用
22.16_IO流(Properties的特殊功能使用)(瞭解)
- A:Properties的特殊功能
- public Object setProperty(String key,String value)
- public String getProperty(String key)
- public Enumeration stringPropertyNames()
- B:案例演示
- Properties的特殊功能
22.17_IO流(Properties的load()和store()功能)(瞭解)
- A:Properties的load()和store()功能
- B:案例演示
- Properties的load()和store()功能
package com.heima.otherio;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
public class Demo10_Properties {
/**
* @param args
* Properties是Hashtable的子類
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
//demo1();
//demo2();
Properties prop = new Properties();
System.out.println("讀取前:"+prop);
prop.load(new FileInputStream("config.properties")); //將檔案上的鍵值對讀取到集合中
prop.setProperty("tel", "18988888888");
prop.store(new FileOutputStream("config.properties"), null);//第二個引數是用來描述檔案列表的如果不描述,可以傳null
System.out.println("讀取後:"+prop);
}
public static void demo2() {
Properties prop = new Properties();
prop.setProperty("name", "張三");
prop.setProperty("tel", "18912345678");
//System.out.println(prop);
Enumeration<String> en = (Enumeration<String>) prop.propertyNames();
while(en.hasMoreElements()) {
String key = en.nextElement(); //獲取Properties中的每一個鍵
String value = prop.getProperty(key); //根據鍵獲取值
System.out.println(key + "="+ value);
}
}
public static void demo1() {
Properties prop = new Properties();
prop.put("abc", 123);
System.out.println(prop);
}
}