Java基礎突擊第八天0020(字符流)

一個字符相當於兩個字節。

-------->Writer

字符輸出流Writer

public abstract class Writer extends Object implements Appendable,Closeable,Flushable

畢竟是抽象類,需要實現它的子類,比如FileWriter

public FileWriter(File file) throws IOException

public FileWriter(File file,boolean appendable) throws IOException

Writer類的方法

public abstract void close() throws IOException 

public void write(String str) throws IOException

public void write(char[] buf) throws IOException

public abstract void flush() throws IOException

Writer與OutputStream不同,可以直接輸出字符串,不用將字符串變爲byte數組在輸出。

import java.io.File;
import java.io.Writer;
import java.io.FileWriter;
public class TestJava{
}
class Demo{
		public static void main(String[] args) throws Exception{
				File fileW = new File("C:"+File.separator+"JavaStudy"+File.separator
								+"JavaIO"+File.separator+"test04.txt");
			  Writer rSwitch = new FileWriter(fileW);
				String strTemp01 = "Hello World!";
				rSwitch.write(strTemp01);
				rSwitch.close();
				Writer rSwitchAppend = new FileWriter(fileW,true);
				String strTemp02 = "\r\nAppen!\r\nHello World!";
				rSwitchAppend.write(strTemp02);
				rSwitchAppend.close();
		}
}//Demo

output:

在test04.txt中存入文字。並追加文字。

-------->Reader

public abstract class Reader extends Object implements Readable,Closeable

FileReader

public FileReader(File file) throws FileNotFoundException

Reader常用方法

public abstract void close() throws IOException

public int read() throws IOException    //讀取單個字符

public int read(char[] cbuf) throws IOException  //讀取字符數組並返回長度

在知道數組長度時,直接輸出所有數據在字符數組,並用read(字符數組)讀取,同時返回數組長度

import java.io.File;
import java.io.Reader;
import java.io.FileReader;
public class TestJava{
}
class Demo{
		public static void main(String[] args) throws Exception{
				File fileR = new File("C:"+File.separator+"JavaStudy"+File.separator+
						"JavaIO"+File.separator+"test04.txt");
				Reader rSwitch = new FileReader(fileR);
				char[] cReady = new char[1024];//put all data to char array
				int lencReady = rSwitch.read(cReady);
				rSwitch.close();
				System.out.println("Content:\n"+new String(cReady,0,lencReady));

				//if you don't know the length of the array you can print the letter
				//one by one until (the end == -1)
		}
}//Demo

output:

Content:
Hello World!
Appen!

Hello World!

不知道數組長度,可循環輸出,InputStream和Reader的int read()一個數字一個數字的讀取,再(char)顯示轉換。

import java.io.File;
import java.io.Reader;
import java.io.FileReader;
public class TestJava{
}
class Demo{
		public static void main(String[] args) throws Exception{
				File fileR = new File("C:"+File.separator+"JavaStudy"+File.separator+
						"JavaIO"+File.separator+"test04.txt");
				Reader rSwitch = new FileReader(fileR);
				int lencReady =0 ;
				char[] cReady= new char[1024];
				int tempRec = 0;
				while((tempRec = rSwitch.read())!=-1){
						cReady[lencReady] = (char)tempRec;
						lencReady++;
				}
				rSwitch.close();
				System.out.println("Content:\n"+new String(cReady,0,lencReady));
		}
}//Demo

output:

Content:
Hello World!
Appen!

Hello World!

字符流和字節流的區別:

字節流不會用到緩衝區(內存),是文件本身直接操作的。字符流用到了緩衝區。

不關閉字節流操作,文件中也能依然輸出內容,證明字節流是直接操作文件本身。

字符流不關閉,緩衝區內容無法輸出。強行輸出緩衝區的數據可以用flush數據(rSwitch.flush())。

字節流還是字符流好?

字節流。所有的文件在硬盤中都是字節方式存儲。字符是只有在內存中才形成。所以字節流較爲廣泛。

使用Java字節流完成文件複製:

import java.io.File;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;

public class TestJava{
}
class Demo{
		public static void main(String[] args){
				if(args.length!=2){
						System.out.println("Input Error!");
						System.exit(1);
				}
				File fS = new File(args[0]);
				File fT = new File(args[1]);
				if(!fS.exists()){
						System.out.println("Source file doesn't exist.");
						System.exit(1);
				}
				InputStream iF = null;
				OutputStream oF = null;
				try{
						iF = new FileInputStream(fS);
				}catch(FileNotFoundException e){
						e.printStackTrace();
				}
				try{
						oF = new FileOutputStream(fT);
				}catch(FileNotFoundException e){
						e.printStackTrace();
				}
				if(iF!=null && oF!=null){
						int recTemp=0;
						try{
								while((recTemp = iF.read())!=-1){
										oF.write(recTemp);
								}
								System.out.println("Copy over!");
						}catch(IOException e){
								e.printStackTrace();
						}
						try{
								iF.close();
								oF.close();
						}catch(IOException e){
								e.printStackTrace();
						}
				}
		}
}//Demo

在控制檯輸入:java Demo C:\JavaStudy\JavaIO\test04.txt C:\JavaStudy\JavaIO\test03.txt

output: Copy over

同時在文件夾複製test04.txt到test03.txt。


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