filecopier.java

来自「java编写的监控一个文件夹里面有没有新的excel文件放入」· Java 代码 · 共 50 行

JAVA
50
字号
package com.justin.util;

import java.io.*;
import java.nio.*;
import java.nio.channels.*;

public class FileCopier {
	public static void copy(String inFilePath, String outFilePath)
			throws IOException {
		FileInputStream fin = null;
		RandomAccessFile fout = null;
		try {
			fin = new FileInputStream(inFilePath);
			fout = new RandomAccessFile(outFilePath, "rw");

			FileChannel in = fin.getChannel();
			FileChannel out = fout.getChannel();

			MappedByteBuffer input = in.map(FileChannel.MapMode.READ_ONLY, 0,
					in.size());
			MappedByteBuffer output = out.map(FileChannel.MapMode.READ_WRITE,
					0, in.size());

			output.put(input);
		} finally {
			try {
				if (fin != null)
					fin.close();
			} catch (IOException ex) {
			}
			try {
				if (fout != null)
					fout.close();
			} catch (IOException ex) {
			}
		}
	}

	public static void main(String[] args) {
		try {
			copy("D:\\ExcelFile\\test1.xls", "E:\\xu.xls");
			System.out.println("successful");
		}
		catch (IOException ex){
			System.err.println(ex);
		}
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?