Java_io体系之FileWriter、FileReader简介、走进源码及示例――18(二)

2014-11-24 07:53:45 · 作者: · 浏览: 1
super(new FileInputStream(file)); } /** * 根据FileDescriptor创建。 */ public FileReader(FileDescriptor fd) { super(new FileInputStream(fd)); } }

4、实例演示:


package com.chy.io.original.test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileReaderAndFileWriterTest {
	
	private static File file = new File("D:" + File.separator + "fw.txt");
	private static File originalFile = new File("D:\\j2se7.chm");

	public static void testFileWriter() throws IOException{
		FileWriter fw = new FileWriter(file);
		
		char[] cbuf = {0x61, 0x62, 0x63, 0x64, 0x65};
		String str = new String(cbuf);
		
		//第一个 “abcde”
		fw.write(cbuf, 0, cbuf.length);
		fw.write("\r\n");//Windows下插入一个换行符、Linux只需"\n".
		//第二个
		fw.write(cbuf);
		fw.write("\r\n");
		//第三个
		for(char c : cbuf){
			fw.write(c);
		}
		fw.write("\r\n");
		//第四个
		fw.write(str);
		fw.write("\r\n");
		//第五个
		fw.write(str, 0, str.length());
		fw.write("\r\n");
		//第六个
		fw.append(str, 0, str.length());
		fw.write("\r\n");
		//第七个
		fw.append(str);
		fw.write("\r\n");
		//第八个
		for(char c : cbuf){
			fw.append(c);
		}
		//调用OutputStreamWriter中的flush和close、OutputStreamWriter调用StreamEncoder的这两个方法。
		fw.flush();
		fw.close();
	}
	
	public static void testFileReader() throws IOException{
		FileReader fr = new FileReader(file);
		char[] cbuf = new char[5];
		fr.read(cbuf, 0, 5);
		System.out.println(new String(cbuf));
		fr.skip(2);
		fr.read(cbuf);
		System.out.println(new String(cbuf));
		if(!fr.markSupported()){
			System.out.println(fr.markSupported());
			return;
		}
	}
	
	/**
	 * 使用文件字符流copy字节形式的文件
	 * 当完成之后去打开copy文件、会提示不能打开此文件、就是因为编码不统一造成了乱码。
	 * 我们可以用下面的字节流来实现
	 */
	public static void testCopyFileByChar()throws IOException{
		BufferedReader br = new BufferedReader(new FileReader(originalFile));
		
		File targetFile = new File("E:\\charCopy" + originalFile.getName());
		BufferedWriter bw = new BufferedWriter(new FileWriter(targetFile));
		
		String str ;
		while((str = br.readLine()) != null){
			bw.write(str);
		}
		
		br.close();
		bw.flush();
		bw.close();
		System.out.println("copy success !");
		
	}
	
	/**
	 * 使用字节流copy文件、可以正常使用
	 * @throws IOException
	 */
	public static void testCopyFileByByte() throws IOException{
		BufferedInputStream bis = new BufferedInputStream(new java.io.FileInputStream(originalFile));
		
		File targetFile = new File("E:\\byteCopy" + originalFile.getName());
		BufferedOutputStream bos = new BufferedOutputStream(new java.io.FileOutputStream(targetFile));
		
		byte[] b = new byte[1024];
		int n = 0;
		while((n = bis.read(b)) != -1){
			bos.write(b, 0, n);
		}
		bis.close();
		bos.flush();
		bos.close();
		System.out.println("copy success !");
	}
	
	
	public static void main(String[] args) throws IOException {
		//testFileWriter();
		//testFileReader();
		//testCopyFileByChar();
		testCopyFileByByte();
	}
}


总结:


FileWriter、FileReader以字符的形式对文件进行操作、本质是通过传入的文件名、文件或者文件描述符来创建用于转码或者解码的InputStreamReader、OutputStreamWriter来将对应的FileInputStream、FileOutp