天天看點

java基礎—java的Io操作學習(2)[通俗易懂]

大家好,又見面了,我是全棧君。

學習java的Io操作(2),往檔案中寫入内容,讀取檔案中的内容!

package com.dufy.io;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Writer;

/**
 * 
 * 練習IO操作 second <br/>
 * 代碼是寫出來的,不是看出來的,我是阿飛-aflyun <br/>
 * 在路上! 
 * @author aflyun
 * @email [email protected]
 *
 */
public class TestSecondIo {
	
	public static void main(String[] args) throws IOException {
		writeByteFile();//位元組流寫檔案
		readByteFile();//位元組流讀檔案
		changeReadByteFile();//修改位元組流讀檔案
		
		writeStringFile();//字元流寫檔案
		readStringFile();//字元流寫檔案
	}
	/**
	 * 位元組流
	 * 1:向檔案中寫入字元串
	 * 2:向檔案中一個位元組一個位元組的寫入字元串
	 * 3:向檔案中追加新内容
	 * @throws IOException 
	 */
	public static void writeByteFile() throws IOException{
		String path = "E:"+ File.separator +"aflyun.txt";
		File file = new File(path);
		OutputStream os = new FileOutputStream(file);
		String str = "Hello World!";
		byte[] b = str.getBytes();
		//1:向檔案中寫入字元串
		os.write(b);
		//2:向檔案中一個位元組一個位元組的寫入字元串
		for (int i = 0; i < b.length; i++) {
			os.write(b[i]);
		}
		//3:向檔案中追加新内容
		String appendStr = "\nI am aflyun!"; // \n 表示換行
		byte[] b1 = appendStr.getBytes();
		os.write(b1);//輸出 Hello World! I am aflyun!
		os.flush();
		os.close();
	}
	/**
	 * 位元組流
	 * 1:讀取内容
	 * @throws IOException
	 */
	public static void readByteFile() throws IOException{
		String path = "E:"+ File.separator +"aflyun.txt";
		File file = new File(path);
		InputStream is = new FileInputStream(file);
		byte b[] = new byte[1024]; //設定最多存1024個位元組
		int len = is.read(b);
		is.close();
		System.out.println(new String(b));
		System.out.println("檔案讀入長度: " + len);
	}
	/**
	 * 修改上面那種方式
	 * 位元組流
	 * 上面那種方式	預先申請了一個指定大小的空間,但是有時候這個空間可能太小,有時候可能太大,我們需要準确的大小,這樣子可以更好的利用空間
	 * @throws IOException
	 */
	public static void 	changeReadByteFile() throws IOException{
		String path = "E:"+ File.separator +"aflyun.txt";
		File file = new File(path);
		InputStream is = new FileInputStream(file);
		byte b[] = new byte[(int) file.length()]; //設定讀取的該檔案的長度,這樣子可以很好的設定使用的空間
		//附1:一個位元組一個位元組的讀
		/*for (int i = 0; i < b.length; i++) {
			b[i] = (byte) is.read();
		}*/
		//附2:判斷檔案是否讀完
		int tmp = 0;
		int count = 0;
		while((tmp = is.read())!= -1){
			b[count++] = (byte) tmp;
		}
		is.close();
		System.out.println(new String(b));
		System.out.println(count);
	
	}
	/**
	 * 字元流
	 * 檔案寫人字元資料
	 * @throws IOException
	 */
	public static void 	writeStringFile() throws IOException{
		String path = "E:"+ File.separator +"aflyunGood.txt";
		File file = new File(path);
		Writer out = new FileWriter(file);
		String str = "你好,依依!";
		out.write(str);
		out.close();
	}
	/**
	 * 字元流
	 * 檔案讀出
	 * @throws IOException 
	 */
	public static void readStringFile() throws IOException{
		String path = "E:"+ File.separator +"aflyunGood.txt";
		File file = new File(path);
		BufferedReader bf = new BufferedReader(new FileReader(file));
		String str = "";
		while((str = bf.readLine()) != null){ //按行讀
			System.out.println(str);
		}
		bf.close();
	}
	
	/**
	 * 總結 位元組流和字元流的差別
	 * 	位元組流在操作的時候本身是不會用到緩沖區的,是檔案本身的直接操作的,但是字元流在操作的 時候下後是會用到緩沖區的,是通過緩沖區來操作檔案的。
	 * 	試着将上面的位元組流和字元流的程式的最後一行關閉檔案的代碼注釋掉,然後運作程式看看。你就會發現使用位元組流的話,檔案中已經存在内容,但是使用字元流的時候,檔案中還是沒有内容的,這個時候就要重新整理緩沖區。
	 * 	那到底那個好一些呢?
	 * 	答案是位元組流。首先因為硬碟上的所有檔案都是以位元組的形式進行傳輸或者儲存的,包括圖檔等内容。但是字元隻是在記憶體中才會形成的,是以在開發中,位元組流使用廣泛。
	 */
}           

複制

釋出者:全棧程式員棧長,轉載請注明出處:https://javaforall.cn/121268.html原文連結:https://javaforall.cn