Thinking in java 琐碎知识点之 I/O流 、对象序列化(四)

2014-11-23 21:43:47 · 作者: · 浏览: 30
fw1 = new FileWriter("F:\\filewriter.txt"); fw1.write("作品z"); char[] ch={'q','w','e','你'}; fw2 = new FileWriter("E:\\fileWriter.txt"); fw2.write(ch,1,ch.length-1); //第二个参数表示开始写入字符处的偏移量(从第几个字符开始写),第三个参数表示写多少长度的数据 fw2.write(ch,0,ch.length-3); fw2.write(ch,0,ch.length); fw2.write(ch);//Writer能一次写入一个字符数组大小的内容;Reader能一次读出一个字符数组(相当于缓冲)大小的内容 fw1.flush();// 字符流必须手动刷新才能真的写入文件 fw2.flush(); } catch (IOException e) { throw new RuntimeException("文件写入失败"); } finally { try { if (fw1 != null) // 健壮性判断,流有可能为空(流对象没有建立成功) { fw1.close();// 调用close方法时会自动刷新,但是不要都等到这一步才刷 } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //开了几个流关闭几个,单独关闭(否则第一个关的时候抛了异常,第二个就没有机会关了) try { if (fw2 != null) fw2.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
6. 复制文本文件
读取源文件 FileRreader
写入到目的文件 FileWriter

两种文件复制方式,分别计算时间
读一个字符,写一个字符
第一个数组,写一个数组

利用缓冲区复制文件
第一行,写一行的操作
例程:复制文本文件
import java.io.*;
public class CopyText {
	// 读取一个数组,写一个数组
	public static void copyText1() {
		FileReader fr = null;
		FileWriter fw = null;
		try {
			fr = new FileReader("F:\\Thinking.txt");
			fw = new FileWriter("F:\\Thinking2.txt");
			char[] buff = new char[1024];
			int len = 0;
			while ((len = fr.read(buff)) != -1) {
				fw.write(buff, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fr != null)
					fr.close();
			} catch (IOException e) {
				throw new RuntimeException("文件读取关闭失败");
			}
			try {
				if (fw != null)
					fw.close();
			} catch (IOException e) {
				throw new RuntimeException("文件写入关闭失败");
			}


		}
	}

	// 读取一个字符,写一个字符
	public static void copyText2() {
		FileReader fr = null;
		FileWriter fw = null;
		try {
			fw = new FileWriter("F:\\Thinking2.txt");
			fr = new FileReader("F:\\Thinking.txt");
			int len = 0;
			while ((len = fr.read()) != -1) {
				fw.write((char) len);
			}
		} catch (Exception e) {
			throw new RuntimeException("文件复制失败");
		} finally {
			try {
				if (fr != null)
					fr.close();
			} catch (IOException e) {
				throw new RuntimeException("文件读取关闭失败");
			}
			try {
				if (fw != null){
					fw.close();
				}
			} catch (IOException e) {
				throw new RuntimeException("文件写入关闭失败");
			}
		}
	}

	//读取一行写一行的方式,利用的缓冲区对象
	public static void copyText3(){
		FileReader fr=null;
		FileWriter fw=null;
		BufferedReader bfr=null;
		BufferedWriter bfw=null;
		try {
			fr=new FileReader("F:\\Thinking.txt");
			fw=new FileWriter("F:\\Thinking3.txt");
			bfr=new BufferedReader(fr);
			bfw=new BufferedWriter(fw);
			String sLine=null; 
			while((sLine=bfr.readLine())!=null){
				bfw.write(sLine);
				//bfw.write("\r\n");
				bfw.newLine();
			}
			
		} catch (IOException e) {
			throw new RuntimeException("文件复制失败");
		}finally{
			try {
				if(bfr!=null);
				bfr.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if(bfw!=null)
				bfw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	} 
		
	public static void main(String[] args) {