新io與舊io文件複製

package file;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;

public class NewIo {

	public static void main(String[] args) throws IOException {
		List<String> fileList = listFiles("C:\\業務數據");
		String destFile = "C:\\業務數據\\data.txt";
		for (String file : fileList) {
			copyFile(file, destFile);
		}

	}

	public static List<String> listFiles(String path) {
		File file = new File(path);
		List<String> list = new ArrayList<String>();
		File[] files = file.listFiles();
		for (File f : files) {
			String absolutePath = f.getAbsolutePath();
			if (absolutePath.endsWith(".xml")) {
				list.add(f.getAbsolutePath());
			}
		}
		return list;
	}

	/**
	 * 新IO
	 * 
	 * @param sourceFile
	 * @param destFile
	 * @throws IOException
	 */
	public static void copyFileNew(File sourceFile, File destFile) throws IOException {
		if (!destFile.exists()) {
			destFile.createNewFile();
		}

		FileChannel source = null;
		FileChannel destination = null;
		try {
			source = new FileInputStream(sourceFile).getChannel();
			destination = new FileOutputStream(destFile).getChannel();
			destination.transferFrom(source, 0, source.size());
			System.out.println(source.size());
		} finally {
			if (source != null) {
				source.close();
			}
			if (destination != null) {
				destination.close();
			}
		}
	}

	public static void copyFileOld(File source, File dest) throws IOException {
		if (!dest.exists()) {
			dest.createNewFile();
		}
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new FileInputStream(source);
			out = new FileOutputStream(dest, true);

			// Transfer bytes from in to out
			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
		} finally {
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.close();
			}
		}
	}

	public static void copyFile(String fileName, String dest) throws IOException {
		BufferedReader bufferedReader = null;
		BufferedWriter writer = null;
		try {
			bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
			writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest, true)));
			writer.write(fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.lastIndexOf("-")) + ":");
			writer.newLine();
			String content = "";
			while ((content = bufferedReader.readLine()) != null) {
				if (content.trim().matches("<!-- [a-zA-Z]+ -->")) {//正則表達式過濾
					System.out.println(content.trim());
					writer.write(content.trim());
					writer.newLine();
				}
			}
		} catch (FileNotFoundException e) {
			throw new RuntimeException("文件路徑不正確", e);
		} catch (IOException e) {
			throw new RuntimeException("讀取文件出錯", e);
		} finally {
			if (writer != null) {
				writer.close();
			}
			if (bufferedReader != null) {
				bufferedReader.close();
			}
		}
	}

}

 

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