在d盘下的java.txt文件复制到javacop,需要的代码谢谢
发布网友
发布时间:2022-04-29 13:38
我来回答
共1个回答
热心网友
时间:2022-06-29 01:46
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* 2015年12月10日下午7:52:42
*
* @author hp TODO 文件的copy
*
*/
public class CopyFile {
private static String fromFilePath = "D://java.txt";// 原文件
private static String toFilePath = "D://javadoc.txt";// 拷贝文件
/**
* 拷贝文件
*/
public static void copy() {
try {
File fromFile = new File(fromFilePath);
if (fromFile.exists()) {// 文件存在
FileInputStream fileInputStream = new FileInputStream(fromFile);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
FileOutputStream fileOutputStream = new FileOutputStream(toFilePath);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
byte[] bits = new byte[1024];
while (-1 != (bufferedInputStream.read(bits))) {
bufferedOutputStream.write(bits);
}
bufferedInputStream.close();
fileInputStream.close();
bufferedOutputStream.flush();
bufferedInputStream.close();
fileOutputStream.close();
System.out.println("----- Done ---------");
} else {
System.err.println("文件不存在");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void main(String[] args) {
copy();
}
}