天天看點

java 學習總結:輸入/輸出流

1.輸入/輸出流概念

java 學習總結:輸入/輸出流

執行個體

package com.io;

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

public class Demo1 {

	public static void main(String[] args) throws IOException {
		FileInputStream fis = new FileInputStream("xxx.txt");		//建立流對象
		int x = fis.read();				//從硬碟上讀取一個位元組
		System.out.println(x);
		int y = fis.read();
		System.out.println(y);
		
		fis.close();			//關流釋放資源
	}

}

           

2.思考

read()方法讀取的是一個位元組,為什麼傳回的是int ,而不是byte

00000000 00100010 01000001 11111111 00000000

用int會讀取都補上24個零 變成4個位元組

100000001 byte類型-1的原碼

11111110 -1的反碼

11111111 -1的補碼

java 學習總結:輸入/輸出流

3. FileOutputStream追加

FileOutputStream在建立對象的時候是如果沒有這個檔案會自動幫我建立

如果有會先将檔案清空

執行個體

package com.io;

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

public class Demo2 {

	public static void main(String[] args) throws IOException {
		demo1();
		FileOutputStream fos = new FileOutputStream("yyy.txt",true);//建立位元組輸出流,如果沒有就自動建立一個
																	//如果想續寫就在第二個參數傳true
		fos.write(97);			//雖然寫出的是一個int數,但是到檔案上是一個位元組,會自動去除前面三個8位
		fos.write(98);
		fos.write(97);
		
		fos.close();
	}

	private static void demo1() throws FileNotFoundException, IOException {
		FileOutputStream fos = new FileOutputStream("yyy.txt");		//建立位元組輸出流,如果沒有就自動建立一個
		fos.write(97);			//雖然寫出的是一個int數,但是到檔案上是一個位元組,會自動去除前面三個8位
		fos.write(98);
		fos.write(97);
		fos.close();
	}

}

           

4.IO流 核心代碼(重點)

拷貝

package com.io;

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

public class Demo3 {

	public static void main(String[] args) throws IOException {
		FileInputStream fis = new FileInputStream("hello.jpg");		//建立輸入流對象,關聯hello.jpg
		FileOutputStream fos = new FileOutputStream("copy.jpg");		//建立輸出流對象,關聯copy.jpg
		int b ;
		while ((b = fis.read()) != -1) {							//在不斷的讀取每一個位元組
			fos.write(b);  											//将每一個位元組寫出
		}
		fis.close(); 												//關流釋放資源
		fos.close();
	}

}

           

5.BufferedInputStream 類可以對所有InputStream類進行帶緩沖區的包裝以達到性能的優化

java 學習總結:輸入/輸出流

6.思考

java 學習總結:輸入/輸出流

圖解

java 學習總結:輸入/輸出流

7.flush()和close()方法的差別

java 學習總結:輸入/輸出流
java 學習總結:輸入/輸出流

8.圖檔加密:

将寫出的位元組異或上一個數,這個數就是密鑰,解密的時候再次異或就可以了

package com.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo5 {

	public static void main(String[] args) throws IOException {
		Object hello;
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream("hello.jpg"));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("hello.jpg"));
		
		int b;
		while((b = bis.read())!= -1) {
			bos.write(b ^ 123);
	}
		bis.close();
		bos.close();
	}

}

           

9.錄入資料拷貝到檔案

package com.io;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class Demo6 {

	public static void main(String[] args) throws IOException {
		//1.建立鍵盤錄入對象
		Scanner sc = new Scanner(System.in);
		//2.建立輸出流對象,關聯text.txt檔案
		FileOutputStream fos = new FileOutputStream("text.txt");
		System.out.println("請輸入");
		//定義無限循環
		while(true) {
			String line = sc.nextLine();
			//4.遇到quit退出循環
			if("quit".equals(line)) {
				break;
			}
			//5.如果不quit。就将内容寫出
			fos.write(line.getBytes());
			fos.write("\r\n".getBytes());
			
		}
		fos.close();
	}

}

           

10.字元流

java 學習總結:輸入/輸出流

11.思考

java 學習總結:輸入/輸出流

newLine()方法是換行

readLine()方法讀取一個文本行,并将去傳回為字元串。若無資料可讀,則傳回null

newLine()與\r\n的差別:

newLine()是跨平台的

\r\n隻支援windows系統

java 學習總結:輸入/輸出流
java 學習總結:輸入/輸出流

圖解

java 學習總結:輸入/輸出流

擷取文本字元出現的次數

package com.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.TreeMap;

public class Demo1_FileReader {
	/*
	 *分析:
	 *1.建立帶緩沖的輸入流對象
	 *2.建立雙列集合對象TreeMap
	 *3.将讀到的字元存儲在雙列集合中,存儲的時候要判斷,如果不包含這個鍵,就将鍵和1存儲,如果包含這個鍵,就将該鍵的值加1存儲
	 *4,關閉輸入流
	 *5.建立輸出流對象
	 *6.周遊集合将集合中的内容寫到times.txt
	 *7.關閉輸出流
	 */
	public static void main(String[] args) throws IOException {
		//1.建立帶緩沖的輸入流對象
		BufferedReader br = new BufferedReader(new FileReader("zzz.txt"));
		//2.建立雙列集合對象TreeMap
		TreeMap<Character, Integer> tm = new TreeMap<>();
		//3.将讀到的字元存儲在雙列集合中,存儲的時候要判斷,如果不包含這個鍵,就将鍵和1存儲,如果包含這個鍵,就将該鍵的值加1存儲
		int ch;
		while((ch = br.read()) != -1) {
			char c = (char)ch;
			tm.put(c, !tm.containsKey(c) ? 1 : tm.get(c) + 1);
		}
		//4,關閉輸入流
		br.close();
		//5.建立輸出流對象
		BufferedWriter bw = new BufferedWriter(new FileWriter("times.txt"));
		//6.周遊集合将集合中的内容寫到times.txt
		for(Character key : tm.keySet()) {
			switch(key) {
				case '\t':
				bw.write("\\t" + "=" + tm.get(key));
				break;
				case '\n':
					bw.write("\\n" + "=" + tm.get(key));
					break;
				case '\r':
					bw.write("\\r" + "=" + tm.get(key));
					break;
			}
			bw.newLine();
		}
		bw.close();
	}

}