JAVA語言基礎-面向對象(IO:IO其餘的流)

22.01_IO流(序列流)(瞭解)

  • 1.什麼是序列流
    • 序列流能夠把多個字節輸入流整合成一個, 從序列流中讀取數據時, 將從被整合的第一個流開始讀, 讀完一個以後繼續讀第二個, 以此類推.
  • 2.使用方式java

    • 整合兩個: 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();

    

package com.heima.otherio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;

public class Demo01_SequenceInputStream {

	/**
	 * @param args
	 * 整合兩個輸入流
	 * SequenceInputStream(InputStream s1, InputStream s2)
	 * 整合多個輸入流
	 * SequenceInputStream(Enumeration<? extends InputStream> e)
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		//demo1();
		//demo2();
		FileInputStream fis1 = new FileInputStream("a.txt");
		FileInputStream fis2 = new FileInputStream("b.txt");
		FileInputStream fis3 = new FileInputStream("c.txt");
		
		Vector<FileInputStream> v = new Vector<>();					//建立集合對象
		v.add(fis1);												//將流對象存儲進來
		v.add(fis2);
		v.add(fis3);
		
		Enumeration<FileInputStream> en = v.elements();
		SequenceInputStream sis = new SequenceInputStream(en);		//將枚舉中的輸入流整合成一個
		FileOutputStream fos = new FileOutputStream("d.txt");
		
		int b;
		while((b = sis.read()) != -1) {
			fos.write(b);
		}
		
		sis.close();
		fos.close();
	}

	public static void demo2() throws FileNotFoundException, IOException {
		FileInputStream fis1 = new FileInputStream("a.txt");
		FileInputStream fis2 = new FileInputStream("b.txt");
		SequenceInputStream sis = new SequenceInputStream(fis1, fis2);
		FileOutputStream fos = new FileOutputStream("c.txt");
		
		int b;
		while((b = sis.read()) != -1) {
			fos.write(b);
		}
		
		sis.close();					//sis在關閉的時候,會將構造方法中傳入的流對象也都關閉
		fos.close();
	}

	public static void demo1() throws FileNotFoundException, IOException {
		FileInputStream fis1 = new FileInputStream("a.txt");		//建立字節輸入流關聯a.txt
		FileOutputStream fos = new FileOutputStream("c.txt");		//建立字節輸出流關聯c.txt
		
		int b1;
		while((b1 = fis1.read()) != -1) {							//不斷的在a.txt上讀取字節
			fos.write(b1);											//將讀到的字節寫到c.txt上
		}
		fis1.close();												//關閉字節輸入流
		
		FileInputStream fis2 = new FileInputStream("b.txt");
		int b2;
		while((b2 = fis2.read()) != -1) {
			fos.write(b2);
		}
		
		fis2.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();

    

package com.heima.otherio;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Demo02_ByteArrayOutputStream {

	/**
	 * @param args
	 * ByteArrayOutputStream
	 * 內存輸出流
	 * 
	 * FileInputStream讀取中文的時候出現了亂碼
	 * 
	 * 解決方案
	 * 1,字符流讀取
	 * 2,ByteArrayOutputStream
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		//demo1();
		FileInputStream fis = new FileInputStream("e.txt");
		ByteArrayOutputStream baos = new ByteArrayOutputStream();			//在內存中建立了能夠增加的內存數組
		
		int b;
		while((b = fis.read()) != -1) {
			baos.write(b);													//將讀取到的數據逐個寫到內存中
		}
		
		//byte[] arr = baos.toByteArray();									//將緩衝區的數據所有獲取出來,並賦值給arr數組
		//System.out.println(new String(arr));
		
		System.out.println(baos.toString());								//將緩衝區的內容轉換爲了字符串,在輸出語句中能夠省略調用toString方法
		fis.close();
	}

	public static void demo1() throws FileNotFoundException, IOException {
		FileInputStream fis = new FileInputStream("e.txt");
		byte[] arr = new byte[3];
		int len;
		while((len = fis.read(arr)) != -1) {
			System.out.println(new String(arr,0,len));
		}
		
		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()dom

    

package com.heima.otherio;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Demo08_RandomAccessFile {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		RandomAccessFile raf = new RandomAccessFile("g.txt", "rw");
		//raf.write(97);
		//int x = raf.read();
		//System.out.println(x);
		raf.seek(0);					//在指定位置設置指針
		raf.write(98);
		raf.close();
	}

}

22.06_IO流(對象操做流ObjecOutputStream)(瞭解)

  • 1.什麼是對象操做流
    • 該流能夠將一個對象寫出, 或者讀取一個對象到程序中. 也就是執行了序列化和反序列化的操做.
  • 2.使用方式優化

    • 寫出: new ObjectOutputStream(OutputStream), writeObject()spa

      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();
          }
      
      }

    

package com.heima.otherio;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import com.heima.bean.Person;

public class Demo03_ObjectOutputStream {

	/**
	 * @param args
	 * 序列化:將對象寫到文件上
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		//demo1();
		Person p1 = new Person("張三", 23);
		Person p2 = new Person("李四", 24);
		Person p3 = new Person("王五", 25);
		Person p4 = new Person("趙六", 26);
		
		ArrayList<Person> list = new ArrayList<>();
		list.add(p1);
		list.add(p2);
		list.add(p3);
		list.add(p4);
		
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e.txt"));
		oos.writeObject(list);								//把整個集合對象一次寫出
		oos.close();
	}

	public static void demo1() throws IOException, FileNotFoundException {
		Person p1 = new Person("張三", 23);
		Person p2 = new Person("李四", 24);
		
		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();
          }
      
      }

    

package com.heima.otherio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

import com.heima.bean.Person;

public class Demo04_ObjectInputStream {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 * @throws ClassNotFoundException 
	 * ObjectInputStream
	 * 對象輸入流,反序列化
	 */
	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
		//demo1();
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e.txt"));
		ArrayList<Person> list = (ArrayList<Person>) ois.readObject();		//將集合對象一次讀取
		
		for (Person person : list) {
			System.out.println(person);
		}
		
		ois.close();
	}

	public static void demo1() throws IOException, FileNotFoundException,
			ClassNotFoundException {
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e.txt"));
		
		Person p1 = (Person) ois.readObject();
		Person p2 = (Person) ois.readObject();
		//Person p3 = (Person) ois.readObject();			//當文件讀取到了末尾時出現EOFException
		
		System.out.println(p1);
		System.out.println(p2);
		
		ois.close();
	}

}

22.08_IO流(對象操做流優化)(瞭解)

* 將對象存儲在集合中寫出code

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();

    

package com.heima.otherio;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo09_Data {

	/**
	 * @param args
	 * @throws IOException 
	 * 00000000 00000000 00000011 11100101	int類型997
	 * 11100101
	 * 00000000 00000000 00000000 11100101
	 */
	public static void main(String[] args) throws IOException {
		//demo1();
		//demo2();
		//demo3();
		DataInputStream dis = new DataInputStream(new FileInputStream("h.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();
	}

	public static void demo3() throws FileNotFoundException, IOException {
		DataOutputStream dos = new DataOutputStream(new FileOutputStream("h.txt"));
		dos.writeInt(997);
		dos.writeInt(998);
		dos.writeInt(999);
		dos.close();
	}

	public static void demo2() throws FileNotFoundException, IOException {
		FileInputStream fis = new FileInputStream("h.txt");
		int x = fis.read();
		int y = fis.read();
		int z = fis.read();
		
		System.out.println(x);
		System.out.println(y);
		System.out.println(z);
		
		fis.close();
	}

	public static void demo1() throws FileNotFoundException, IOException {
		FileOutputStream fos = new FileOutputStream("h.txt");
		fos.write(997);
		fos.write(998);
		fos.write(999);
		
		fos.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();

    

package com.heima.otherio;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;

import com.heima.bean.Person;

public class Demo05_PrintStream {

	/**
	 * @param args
	 * @throws IOException 
	 * PrintStream和PrintWriter分別是打印的字節流和字符流
	 * 只操做數據目的的
	 */
	public static void main(String[] args) throws IOException {
		//demo1();
		PrintWriter pw = new PrintWriter(new FileOutputStream("f.txt"),true);
		//pw.println(97);							//自動刷出功能只針對的是println方法
		//pw.write(97);
		pw.print(97);
		pw.println(97);
		pw.close();
	}

	public static void demo1() {
		System.out.println("aaa");
		PrintStream ps = System.out;			//獲取標註輸出流
		ps.println(97);							//底層經過Integer.toString()將97轉換成字符串並打印
		ps.write(97);							//查找碼錶,找到對應的a並打印
		
		Person p1 = new Person("張三", 23);
		ps.println(p1);							//默認調用p1的toString方法
		
		Person p2 = null;						//打印引用數據類型,若是是null,就打印null,若是不是null就打印對象的toString方法
		ps.println(p2);
		ps.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();

    

package com.heima.otherio;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;

public class Demo06_SystemInOut {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		//demo1();
		System.setIn(new FileInputStream("a.txt"));			//改變標準輸入流
		System.setOut(new PrintStream("b.txt"));			//改變標註輸出流
		
		InputStream is = System.in;							//獲取標準的鍵盤輸入流,默認指向鍵盤,改變後指向文件
		PrintStream ps = System.out;						//獲取標準輸出流,默認指向的是控制檯,改變後就指向文件
		
		int b;
		while((b = is.read()) != -1) {
			ps.write(b);
		}
		//System.out.println();								//也是一個輸出流,不用關,由於沒有和硬盤上的文件產生關聯的管道
		is.close();
		ps.close();
		
	}

	public static void demo1() throws IOException {
		InputStream is = System.in;
		int x = is.read();
		System.out.println(x);
		
		is.close();
		
		InputStream is2 = System.in;
		int y = is2.read();
		System.out.println(y);
	}

}
package com.heima.otherio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Demo07_SystemIn {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		/*BufferedReader br = new BufferedReader(new InputStreamReader(System.in));		//InputStreamReader轉換流
		String line = br.readLine();
		System.out.println(line);
		br.close();*/
		
		Scanner sc = new Scanner(System.in);
		String line = sc.nextLine();
		System.out.println(line);
		sc.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();
package com.heima.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;

public class Test2 {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		System.setIn(new FileInputStream("雙元.jpg"));			//改變標準輸入流
		System.setOut(new PrintStream("copy.jpg")); 			//改變標準輸出流
		
		InputStream is = System.in;
		PrintStream ps = System.out;
		
		byte[] arr = new byte[1024];
		int len;
		
		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();
		prop.load(new FileInputStream("config.properties"));		//將文件上的鍵值對讀取到集合中
		prop.setProperty("tel", "18912345678");
		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);
	}

}
  • 定義一個文件輸入流,調用read(byte[] b)方法,將a.txt文件中的內容打印出來(byte數組大小限制爲5)

    

package com.heima.test;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Test1 {

	/**
	 * @param args
	 * 定義一個文件輸入流,調用read(byte[] b)方法,將a.txt文件中的內容打印出來(byte數組大小限制爲5)
	 * 
	 * 分析:
	 * 1,reda(byte[] b)是字節輸入流的方法,建立FileInputStream,關聯a.txt
	 * 2,建立內存輸出流,將讀到的數據寫到內存輸出流中
	 * 3,建立字節數組,長度爲5
	 * 4,將內存輸出流的數據所有轉換爲字符串打印
	 * 5,關閉輸入流
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		//1,reda(byte[] b)是字節輸入流的方法,建立FileInputStream,關聯a.txt
		FileInputStream fis = new FileInputStream("a.txt");
		//2,建立內存輸出流,將讀到的數據寫到內存輸出流中
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		//3,建立字節數組,長度爲5
		byte[] arr = new byte[5];
		int len;
		
		while((len = fis.read(arr)) != -1) {
			baos.write(arr, 0, len);
			//System.out.println(new String(arr,0,len));
		}
		//4,將內存輸出流的數據所有轉換爲字符串打印
		System.out.println(baos); 					//即便沒有調用,底層也會默認幫咱們調用toString()方法
		//5,關閉輸入流
		fis.close();
	}
}
相關文章
相關標籤/搜索