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();
				}
			}
		}
	}
}

發佈了35 篇原創文章 · 獲贊 96 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章