天天看點

IO操作——字元流

一.Reader,Writer是所有字元輸入流與字元輸出流的父類

二.字元轉換流

1.InputStreamReader(fos)——>字元輸入流

package day05;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/**
 * 轉換流
 * InputStreamReader
 * @author soft01
 *
 */

public class InputStreamReader_read {
	public static void main(String[] args) throws IOException {
		FileInputStream fis = new FileInputStream("osw.txt");
		InputStreamReader isr = new InputStreamReader(fis,"utf-8");
		/*char[] data = new char[100];
		int len = isr.read(data);
		String str = new String(data,0,len);
		System.out.println(str);*/
		StringBuilder builder = new StringBuilder();
		String str = "";
		int d =-1;
		while((d=isr.read())!=-1){
			builder.append((char)d);
		}
		str = builder.toString();
		System.out.println(str);
		isr.close();
	}
}
           

2.OutputStreamWriter(fos,"utf-8");——>字元輸出流

package day05;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

/**
 * java根據讀寫資料的機關劃分了:位元組流,字元流
 * InputStream,OutputStream是所有位元組輸入流與
 * 位元組輸出流的父類,常用實作類:FileInputStream,
 * BufferedInputStream等。
 * 
 * Reader,Writer是所有字元輸入流與字元輸出流的父類
 * 
 * 位元組流的讀寫機關是位元組,而字元流的讀寫機關是字元。
 * 是以字元流有局限性,隻适合讀寫字元資料。
 * 但實際字元流底層本質還是讀寫位元組,隻是字元與位元組
 * 的轉換工作不用我們來做了。
 * 
 * 轉換流
 * OutputStreamWriter,InputStreamReader
 * @author soft01
 *
 */
public class OutputStreamWriter_write {
	public static void main(String[] args) throws IOException {
		FileOutputStream fos = new FileOutputStream("osw.txt");
		/*
		 * 構造方法若隻傳入流,那麼通過目前轉換流寫出的字元
		 * 會按照系統預設的字元集轉化為對應的位元組,不推薦。
		 * 可以使用重載的構造方法,在第二個參數中明确使用的字元集。
		 */
		OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
		String str = "linsa";
		osw.write(str);
		osw.write("hello");
		System.out.println("寫出完畢!");
		osw.close();
	}
}
           
package day05;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
/**
 * 簡易記事本
 * 使用PW将使用者輸入的每行字元串寫入使用者指定的檔案中
 * 構造方法使用流連接配接形式,不使用直接對檔案操作的。
 * @author soft01
 *
 */
public class Note {
	public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException {
		Scanner scan = new Scanner(System.in);
		String fileName,line;
		System.out.println("請輸入一個檔案名:");
		fileName = scan.nextLine();
		FileOutputStream fos = new FileOutputStream(fileName);
		OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
		/*
		 * 當PrintWriter的構造方法第一個參數為流(位元組流,字元流均可)時,
		 * 那麼支援一個重載的構造方法可以傳入一個boolean值,該值若為true,
		 * 則目前PrintWriter具有自動行重新整理功能
		 * 即:每當調用println方法寫出一行字元串後
		 * 會自動調用flush方法将其真實寫出。
		 * 需要注意,調用print方法是不會flush的。
		 */
		PrintWriter pw = new PrintWriter(osw,true);
		System.out.println("請輸入要寫入的資料:");
		do {
			line = scan.nextLine();
			if(line.equals("exit")) {
				break;
			}
			pw.println(line);
		}while(true);
		scan.close();
		pw.close();
	}
}
           

三. 緩沖字元輸出流——>PrintWriter(fileName)

PrintWriter pw = new PrintWriter(osw);

pw.println("How are you?");

package day05;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

/**
 * 緩沖字元流
 * 緩沖字元流由于内部有緩沖區,讀寫字元的效率高。
 * 并且字元流的特點是可以按行讀寫字元串。
 * BufferedWriter,BufferedReader
 * 
 * PrintWriter也是緩沖區字元輸出流,它内部總是連接配接
 * BufferedWriter,除此之外PW還提供了自動重新整理功能,是以更常用。
 * @author soft01
 *
 */
public class PrintWriter_println {
	public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
		/*
		 * PrintWriter提供了直接對檔案進行寫操作的構造方法:
		 * PrintWriter(File file)
		 * PrintWriter(String fileName)
		 * 若希望按照指定的字元集向檔案寫出字元串,
		 * 可以使用對應重載的構造方法:
		 * PrintWriter(File file,String csn)
		 * PrintWriter(String fileName,String csn)
		 * 第二個參數可以指定字元集的名字(charSetName)
		 */
		PrintWriter pw = new PrintWriter("pw.txt","utf-8");
		pw.println("hello");
		pw.println("monky");
		System.out.println("寫出完畢!");
		pw.close();
	}
}
           
package day05;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

/**
 * PrintWriter提供了正常的構造方法,允許傳入
 * 一個位元組流或者字元流完成流連接配接的建立形式
 * @author soft01
 *
 */
public class PrintWriter_println2 {
	public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException {
		FileOutputStream fos = new FileOutputStream("pw2.txt");
		/*
		 * 若希望指定字元集,需要自行連接配接轉換流
		 * 因為轉換流可以将字元按照指定的字元集轉換為位元組
		 */
		OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
		PrintWriter pw = new PrintWriter(osw);
		/*
		 * PW的構造方法允許直接傳入位元組流,
		 * 但實際内部還是會根據流連接配接最終變為PW的。
		 * 隻是這樣做不能指定字元集。
		 */
		pw.println("How are you?");
		pw.println("I'm fine, Thank you.");
		pw.close();
	}
}
           

四. 緩沖字元輸入流——>BufferedReader(isr)

package day05;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * java.io.BufferedReader
 * 緩沖字元輸入流。特點:可以按行讀取字元串
 * 由于有緩沖,讀取字元時效率好
 * @author soft01
 *
 */
public class BufferedReader_readLine {
	public static void main(String[] args) throws IOException {
		/*
		 * ./src/main/java/day05/BufferedReader_readLine.java
		 */
		FileInputStream fis = new FileInputStream("src"+File.separator+"main"+File.separator+"java"+
				File.separator+"day05"+File.separator+"BufferedReader_readLine.java");
		InputStreamReader isr = new InputStreamReader(fis);
		BufferedReader br = new BufferedReader(isr);
		/*
		 * BufferedReader提供了讀取一行字元串的方法:
		 * String readLine()
		 * 該方法會連續讀取若幹字元,直到讀到了換行符為止,
		 * 然後将換行符之間讀取到的所有字元以一個字元串形式傳回。
		 * 需要注意,傳回的字元串中不包含最後的換行符。
		 * 當該方法傳回null時,表示末尾(不會再讀取到任何資料)
		 */
		String line = null;
		while((line=br.readLine())!=null) {
			System.out.println(line);
		}
		br.close();
	}
}
           

繼續閱讀