天天看点

Java实现简单文本文件复制

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class TextFileCopy {
	public static void main(String[] args) {
		FileReader reader = null;
		FileWriter writer = null;
		try {
			// 创建文件输入输出流
			reader = new FileReader("F:\\电子文档\\各种JDBC连接.txt");
			writer = new FileWriter("F:\\test.txt");
			int flag = 0;
			// 从输入流读取内容使用输出流输出
			while ((flag = reader.read()) != -1) {
				writer.write(flag);
			}
			System.out.println("复制文本成功");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();		 // 关闭文件输入流
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (writer != null) {
				try {
					writer.close();		// 关闭文件输出流
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}