发布网友
共5个回答
热心网友
你写的两个程序都不太严谨,我给你写一个复制文件和复制文件夹的标准例子吧。
//复制文件
package com.cdd.file;
import java.io.*;
public class FileText {
public static void main(String[] args) {
String sfpath = "E://word.txt"; // 指定文件地址
String dfpath = "F://word.txt";
File sFile = new File(sfpath); // 创建文件对象
File dFile = new File(dfpath);
try {
dFile.createNewFile(); // 新建文件
FileInputStream fis = new FileInputStream(sFile);
FileOutputStream fout = new FileOutputStream(dFile);
byte[] date = new byte[512]; // 创建字节数组
int rs = -1;
while ((rs = fis.read(date)) > 0) { // 循环读取文件
fout.write(date, 0, rs); // 向文件写数据
}
fout.close();
fis.close(); // 关闭流
System.out.println("文件复制成功");
} catch (Exception e) {
e.printStackTrace();
}
}
}
//复制文件夹
package com.cdd.util;
import java.io.*;
public class FileUtil {
private static void copy(File[] files, File d) {
if (!d.exists()) //如果指定目录不存在
d.mkdir(); //创建目录
for (int i = 0; i < files.length; i++) { //循环遍历要复制的文件夹
if (files[i].isFile()) { //如果文件夹中是文件
try {
FileInputStream fis = new FileInputStream(files[i]); //创建FileInputStream对象
FileOutputStream out = new FileOutputStream(new File(d
.getPath()
+ File.separator + files[i].getName())); //复制后文件的保存路径
int count = fis.available();
byte[] data = new byte[count];
if ((fis.read(data)) != -1) { //读取文件
out.write(data); //将读取的信息写入文件中
}
out.close(); //关闭流
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (files[i].isDirectory()) { //如果文件夹中是一个路径
File des = new File(d.getPath() + File.separator
+ files[i].getName()); //在复制后路径中创建子文件夹
des.mkdir();
copy(files[i].listFiles(), des); //再次调用本方法
}
}
System.out.println("文件夹复制成功");
}
public static void main(String[] args) {
File sourFile = null, desFile = null;
String sourFolder = "E://word"; //指定要进行复制的文件夹
String desFolder = "E://copy"; //指定复制后的文件夹地址
sourFile = new File(sourFolder);
if (!sourFile.isDirectory() || !sourFile.exists()) { //如果原文件夹不存在
System.out.println("原文件夹不存在");
}
desFile = new File(desFolder);
desFile.mkdir();
copy(sourFile.listFiles(), desFile); //调用文件夹复制方法
}
}
热心网友
实现思路:就是循环获取到文件夹中的文件,然后逐一的进行文件复制。
//复制文件夹
package com.cdd.util;
import java.io.*;
public class FileUtil {
private static void copy(File[] files, File d) {
if (!d.exists()) //如果指定目录不存在
d.mkdir(); //创建目录
for (int i = 0; i < files.length; i++) { //循环遍历要复制的文件夹
if (files[i].isFile()) { //如果文件夹中是文件
try {
FileInputStream fis = new FileInputStream(files[i]); //创建FileInputStream对象
FileOutputStream out = new FileOutputStream(new File(d
.getPath()
+ File.separator + files[i].getName())); //复制后文件的保存路径
int count = fis.available();
byte[] data = new byte[count];
while ((str = bre.readLine())!= null) //读取文件通过readline方法可以有效的避免乱码
out.write(str ); //将读取的信息写入文件中
}
out.close(); //关闭流
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (files[i].isDirectory()) { //如果文件夹中是一个路径
File des = new File(d.getPath() + File.separator
+ files[i].getName()); //在复制后路径中创建子文件夹
des.mkdir();
copy(files[i].listFiles(), des); //再次调用本方法
}
}
System.out.println("文件夹复制成功");
}
热心网友
public static void copyFile(String fromPathName, String toFolder)
throws IOException {
File oFile = new File(fromPathName);
if (oFile.isDirectory()) {
File nFile = new File(toFolder + "\\" + oFile.getName());
nFile.mkdirs();
for (File file : oFile.listFiles())
copyFile(file.getPath(), nFile.getPath());
} else {
File source = new File(fromPathName);
FileInputStream in = new FileInputStream(source);
File target = new File(toFolder + "\\" + source.getName());
if (!target.canWrite())
target.setWritable(true);
FileOutputStream out = new FileOutputStream(toFolder + "\\" + source.getName());
byte[] bytes = new byte[4 * 1204];
int length = 0;
while ((length = in.read()) != -1)
out.write(bytes, 0, length);
in.close();
out.close();
}
}
看上去应该没什么大错.我不知道你程序报的错是什么
你把mkdir换成mkdirs试试
这是我上面写的一个,完全可以,复制文件或文件夹都可以,多少层目录都OK ,toFolder存不存在都无所谓追问Stacktrace:] with root cause
java.io.FileNotFoundException: c:\sx\文档\www\w3c\a.gifc:\sx\照片\www\w3c\a.gif (文件名、目录名或卷标语法不正确。)
报错是这个。。我测试是2层目录,但是我复制后只有目录复制了,最后的目录有个文件却没有复制。
追答c:\sx\文档\www\w3c\a.gifc:\sx\照片\www\w3c\a.gif
这是什么?如果你是复制的错误代码那我就应该理解为在你最后一循环时把路径拼了2次
从而出现了这个,这都不是正确的路径了,当然会出错.
你仔细看下错在哪一行吧,反正我上面那个写法我跑过是没问题的你可以参考一下
热心网友
File.renameTo
热心网友
那你再循环文件夹的时候,把每个文件的真实路径打印到控制台,肯定是路径有问题,你看哪里有问题,就改那个路径