IO基礎之輸入與輸出流

定義:

  1. 可以從其中讀入一個字節系列的對象稱爲輸入流
  2. 可以向其中寫入一個字節系列的對象稱爲輸出流 
  3. 字節系列的來源地和目的地可以是文件,而且通常是文件,也可以是網絡連接和內存塊
  4. 抽象類Inputtream和OutputStream構成的輸入/輸出(I / O)類層次結構的基礎,基於一個字節byte進行操作
  5. 抽象類讀者和作家中繼承出來專門處理的Unicode字符的單獨的類層次結構,這些類主要擁有讀入和寫出操作都是基於兩個字節的字符

讀取字節:

  1. InputStream類中的抽象方法:abstract int read(),這個方法讀入一個字節並返回讀入字節,或者在遇到輸入源結尾時,返回-1
  2. 子類必須覆蓋方法abstract int read(),FileInputStream類中,這個方法將從某個文件中讀取字節

常用讀取字節方法:

  1. int read(byte [] b),從數據流讀入一個字節數組,返回當前讀入的字節數,返回-1
  2. void close(),關閉數據流,這個方法可以拋出IOException異常
  3. long skip(long n),返回實際跳過的字節數(如果碰到輸入流的結尾,則可能小於n)
    /**
    	 * 
    	 * @param file
    	 *            傳入文件
    	 * @return 文件內容
    	 */
    	public static String inputByete(File file) {
    		try {
    			InputStream input = null;
    			try {
    				if (file.exists() && file.isFile()) {
    					input = new FileInputStream(file);//通過打開一個到實際文件的連接來創建一個 FileInputStream
    					byte[] b = new byte[(int) file.length()];
    					input.read(b);//從此輸入流中將最多 file.length 個字節的數據讀入一個 byte 數組中。在某些輸入可用之前,此方法將阻塞。
    					return new String(b);
    				} else {
    					throw new NullPointerException();
    				}
    			} finally {
    				input.close();//關閉數據流
    			}
    
    		} catch (FileNotFoundException | NullPointerException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		return null;
    	}
    

  1. OutputStream中抽象方法:abstract void write(int b),向指定文件輸出字節
  2. 子類FileOutputStream類,寫(byte [] int),輸出指定字節數組

常用輸入字節方法

  1. void write(byte [] b),將指定字節數組輸入到指定文件
  2. void close(),沖刷並關閉輸入流程
  3. void close(),沖刷輸出流,也就是將所有緩衝的數據發送到指定位置
    /**
    	 * 將一個字符串寫入指定文件
    	 * 
    	 * @param file寫入文件文件
    	 * @param str寫入字符串
    	 * @throws FileNotFoundException
    	 */
    	public static void outputByte(File file, String str) {
    		try {
    			if (file.exists() && file.isFile()) { // 判定文件是否存在,或是不是目錄
    				OutputStream output = null;
    				try {
    					output = new FileOutputStream(file);//創建一個向指定 File 對象表示的文件中寫入數據的文件輸出流
    					output.write(str.getBytes());   // 將 b.length 個字節從指定 byte 數組寫入此文件輸出流中,getBytes(),將字符串轉換爲字符數組
    				} finally {
    					output.close();//關閉輸出流
    				}
    			} else {
    				throw new FileNotFoundException();
    			}
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}

讀取字符:

  1. Reader類中的抽象方法:abstract int read() ,這個方法讀入一個字符並返回讀入的字符對應的Unicode,或者在遇到輸入源結尾時,返回-1
  2. 子類必須覆蓋方法abstract int read(),FileReader類中,這個方法將從某個文件中讀取字符
		/**
		 * 字符輸入流的演示,未判定文件
		 */
		try {
			FileReader input = new FileReader(file); // 用來讀取字符文件的便捷類。此類的構造方法假定默認字符編碼和默認字節緩衝區大小都是適當的
			try {
				@SuppressWarnings("resource")
				BufferedReader cache = new BufferedReader(input); // 從字符輸入流讀取文本,緩衝字符,以便字符,數組和行的高效讀取
				String inputTxt = "";
				while ((inputTxt = cache.readLine()) != null) { // 將讀取的字符賦值給inputTxt
					System.out.println(inputTxt);
				}
			} finally {
				input.close();// 關閉輸入流
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();

		} catch (IOException e) {
			e.printStackTrace();
		}

輸入字符:

  1. Writer中抽象方法:abstract void write(int b),向指定文件輸出字符
  2. 子類FileWriter類,write(char [] str),輸出指定字符數組,write(String str)輸出指定的字符串
    	/ **
    		 *字符輸出流
    		 * /
    		String source =“learn how to write to file”;
    		FileWriter output = null;
    		try{
    			try{
    				output = new FileWriter(file); //用來寫入字符文件的便捷類。
    				output.write(source);
    			} finally {
    				output.close(); //關閉輸出流
    			}
    		} catch(IOException e){
    			e.printStackTrace();
    		}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章