本文目录一览:
java如何拷贝文件到另一个目录下
/**
* 复制单个文件
*
* @param oldPath String 原文件路径 如:c:/fqf.txt
* @param newPath String 复制后路径 如:f:/fqf.txt
* @return boolean
*/
public void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { // 文件存在时
InputStream inStream = new FileInputStream(oldPath); // 读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; // 字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
} catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
/**
* 复制整个文件夹内容
*
* @param oldPath String 原文件路径 如:c:/fqf
* @param newPath String 复制后路径 如:f:/fqf/ff
* @return boolean
*/
public void copyFolder(String oldPath, String newPath) {
try {
(new File(newPath)).mkdirs(); // 如果文件夹不存在则建立新文件夹
File a = new File(oldPath);
String[] file = a.list();
File temp = null;
for (int i = 0; i < file.length; i++) {
if (oldPath.endsWith(File.separator)) {
temp = new File(oldPath + file[i]);
} else {
temp = new File(oldPath + File.separator + file[i]);
}
if (temp.isFile()) {
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newPath + "/" + (temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if (temp.isDirectory()) { // 如果是子文件夹
copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
}
}
} catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
}
利用JAVA语言编写一个 名为copy的程序 实现文件的拷贝功能,应该怎样做?
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if (args.length != 2) {
System.out.print("没有输入正确数目的参数,程序退出!");
System.exit(0);
}
File fileS = new File("./" + args[0]);
File fileD = new File("./" + args[1]);
if (fileD.exists())
System.out.println("目标文件 " + args[1] + " 已存在!");
byte[] temp = new byte[50];
int totalSize = 0;
try {
FileInputStream fr = new FileInputStream(fileS);
FileOutputStream fo = new FileOutputStream(fileD);
int length = 0;
while ((length = fr.read(temp, 0, temp.length)) != -1) {
totalSize += length;
fo.write(temp, 0, length);
}
System.out.println("文件 " + args[0] + " 有 " + totalSize + " 个字节");
System.out.println("复制完成!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("源文件 " + args[0] + " 不存在!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
java按比特位进行COPY
System.arrayCopy
可以实现 byte
数组的拷贝,可以基本解决你的问题,不过如果 bit
不是 8 的倍数可能会多拷贝几个,做个掩码,把最后一个字节与(and)一下,就行了。
Java怎么实现文件拷贝
工具/原料
- 一台配置了 Java 环境的电脑
- 一款适合自己的开发集成环境,这里用的是 Eclipse Kepler
文件拷贝 DEMO
- 首先,理清思路,然后我们再动手操作。
- 拷贝,有源文件,和目的文件。
- 如果原文件不存在,提示,报错。
- 如果目的文件不存在,创建空文件并被覆盖。
- 如果目的地址,也即目的路径不存在,创建路径。
- 拷贝,输入流,输出流,关闭流。
- 拷贝前输出文件大小,计算拷贝大小,比较并核实。输出。
- 首先呢,先判断传参是否完整。
- 如果不够两个参数,或者多于两个参数,提示错误。
- 如果目标文件不存在,创建空文件继续复制。
- 在开始前,输出被拷贝的源文件的大小。
- 获得文件名称,即短名。也即路径下的文件全名(包括文件扩展名)。
- 拷贝的关键,这里用的简单的缓冲流。从源文件到目的文件。
number of bytes copied
即是对拷贝长度的累计,直到拷贝完成,输出。 - 将步骤二中的判断并拷贝文件的代码写在一个
main
函数中,执行拷贝,拷贝完成。结果拷贝大小和源文件大小一致,成功。 - 在执行前,记得输入参数。
- 如果是使用命令提示符,执行
javac CopyFile.java
之后, - 执行
java CopyFile [源文件长名] [目的文件长名]
- 如果是使用的 Eclipse,在运行前设置一下运行参数,完成后点击运行,如下图。
- 如果是使用命令提示符,执行
P.S. 这里面的所谓“长名”是指完整绝对路径+文件名+文件类型扩展名
这里的源文件及目的文件的名称分别为:
E:/IP_Data.rar
和D:/testFiles/IP_Data.rar