public static void CopyDirecotry(File fromDir, File toDir) throws Exception {
// File f = new File(fromDir.getName());
toDir.mkdir();
if (fromDir.isDirectory()) {
File[] Dirname = fromDir.listFiles(); for (int i = 0; i < Dirname.length; i++) {
if (Dirname[i].isDirectory()) {
CopyDirecotry(Dirname[i], new File(toDir.getAbsoluteFile()
+ "\\" + Dirname[i].getName()));
} else {
copyFile(Dirname[i], Dirname[i].getName()); }
}
}
}
public static void copyFile(File fromFile, String toFile)
throws IOException {
FileReader from = new FileReader(fromFile);
File ff = new File(toFile);
if (!ff.exists()) {
ff.createNewFile();
}
FileWriter to = new FileWriter(ff);
char[] ch = new char[(int) fromFile.length()];
while (from.read(ch) != -1);
from.close();
to.write(ch);
to.flush();
to.close();
System.out.println("文件成功复制!!!");
}public static void main(String[] args) throws IOException {
try {
File fromDir = new File(getFile + File.separator + "bin");
File toDir = new File(getFile + File.separator + "bin1");
CopyDirecotry(fromDir, toDir);
} catch (Exception e) {
e.printStackTrace();
}
}
// File f = new File(fromDir.getName());
toDir.mkdir();
if (fromDir.isDirectory()) {
File[] Dirname = fromDir.listFiles(); for (int i = 0; i < Dirname.length; i++) {
if (Dirname[i].isDirectory()) {
CopyDirecotry(Dirname[i], new File(toDir.getAbsoluteFile()
+ "\\" + Dirname[i].getName()));
} else {
copyFile(Dirname[i], Dirname[i].getName()); }
}
}
}
public static void copyFile(File fromFile, String toFile)
throws IOException {
FileReader from = new FileReader(fromFile);
File ff = new File(toFile);
if (!ff.exists()) {
ff.createNewFile();
}
FileWriter to = new FileWriter(ff);
char[] ch = new char[(int) fromFile.length()];
while (from.read(ch) != -1);
from.close();
to.write(ch);
to.flush();
to.close();
System.out.println("文件成功复制!!!");
}public static void main(String[] args) throws IOException {
try {
File fromDir = new File(getFile + File.separator + "bin");
File toDir = new File(getFile + File.separator + "bin1");
CopyDirecotry(fromDir, toDir);
} catch (Exception e) {
e.printStackTrace();
}
}
解决方案 »
- 一个小发现,大家进来看看。。。。。。
- 千千静听,这样的软件可以用什么开发?
- 简单文件读写问题,为何部分汉字会变成??
- Socket传送数据,回包字节数较大时,如何分次读取回包?在线等
- 在一个界面用socket同时进行 双机通信 和 文件传输 怎么解决?
- Iterator的数据结构是什么呢?
- 9个正则表达式的练习,看看你能答出多少:)
- 4CPU服务器的JAVA运行问题
- 近一个星期无人答出的问题,快来看
- 连接MySql时,总是提示ClassNotFoundException,我设环境变量了
- Java中输入两个数,比较大小,在比较时出现异常,求解!
- JDBC 在createStatement时设置游标是TYPE_FORWARD_ONLY,但是结果集中还是可以用previous()方法
import java.io.*;
public class TestCopyFile {
public static void copyDirecotry(File fromDir, File toDir) throws Exception {
// File f = new File(fromDir.getName());
toDir.mkdir();
if (fromDir.isDirectory()) {
File[] Dirname = fromDir.listFiles(); for (int i = 0; i < Dirname.length; i++) {
if (Dirname[i].isDirectory()) {
copyDirecotry(Dirname[i], new File(toDir.getAbsoluteFile()
+ "\\" + Dirname[i].getName()));
} else {
copyFile(Dirname[i], Dirname[i].getName());
}
}
}
} public static void copyFile(File fromFile, String toFile)
throws IOException {
FileReader from = new FileReader(fromFile);
File ff = new File(toFile);
if (!ff.exists()) {
ff.createNewFile();
}
FileWriter to = new FileWriter(ff);
char[] ch = new char[(int) fromFile.length()];
while (from.read(ch) != -1)
;
from.close();
to.write(ch);
to.flush();
to.close();
//System.out.println("文件成功复制!!!");
} public static void main(String[] args) throws Exception {
String getFile = System.getProperty("user.dir");
File fromDir = new File(getFile + File.separator + "bin");
File toDir = new File(getFile + File.separator + "bin1");
if(toDir!=null) System.out.println(toDir);//D:\Program Files\MyEclipseProject\Exercise\bin1
if(!toDir.isDirectory()) toDir.mkdirs(); //记得要写这一句
copyDirecotry(fromDir, toDir);//函数名首字母小写
}}
public static void copyDirecotry(String fromDirName, String toDirName)
throws Exception { File fromDir = new File(fromDirName);
if (!fromDir.isDirectory())
return;
File toDir = new File(toDirName);
if (!toDir.isDirectory())
toDir.mkdirs(); // 记得要写这一句
File[] Dirname = fromDir.listFiles(); for (int i = 0; i < Dirname.length; i++) {
if (Dirname[i].isDirectory()) {
copyDirecotry(Dirname[i].getAbsolutePath(), toDir
.getAbsoluteFile()
+ "\\" + Dirname[i].getName());
} else {
copyFile(Dirname[i].getAbsolutePath(), toDir.getAbsoluteFile()
+ "\\" + Dirname[i].getName());
}
} } public static void copyFile(String fromFileName, String toFileName)
throws IOException { BufferedReader br = new BufferedReader(new FileReader(fromFileName));
BufferedWriter bw = new BufferedWriter(new FileWriter(toFileName)); String s;
StringBuffer sb = new StringBuffer();
while ((s = br.readLine()) != null) {
bw.write(s);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
// System.out.println("文件成功复制!!!");
} public static void main(String[] args) throws Exception {
String getFile = System.getProperty("user.dir");
File fromDir = new File(getFile + File.separator + "bin");
File toDir = new File(getFile + File.separator + "bin1");
// if(toDir!=null) System.out.println(toDir);//D:\Program
// Files\MyEclipseProject\Exercise\bin1
copyDirecotry(fromDir.getAbsolutePath(),
toDir.getAbsolutePath());//函数名首字母小写
}}
原先的bin目录大小不一样,我的机器是bin1要大于bin
你只要把bin和bin1改为其他目录,就能复制相同大小的目录,比如src 和 src1,
你的代码运行的话:复制的文件都在bin那个目录的父目录下要实现对应目录的文件复制,只需要改一下这个地方的代码就可以:copyFile(Dirname[i], toDir.getCanonicalPath()+File.separator+Dirname[i].getName());
不可能啊,我把你的代码直接COPY过去的,然后只把copyFile方法传的参数改了一下,就好了:
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class CopyDirecotries { public static void CopyDirecotry(File fromDir, File toDir) throws Exception {
// File f = new File(fromDir.getName());
toDir.mkdir();
if (fromDir.isDirectory()) {
File[] Dirname = fromDir.listFiles(); for (int i = 0; i < Dirname.length; i++) {
if (Dirname[i].isDirectory()) {
CopyDirecotry(Dirname[i], new File(toDir.getAbsoluteFile() + "\\" + Dirname[i].getName()));
} else {
copyFile(Dirname[i], toDir.getCanonicalPath()+File.separator+Dirname[i].getName());//此处代码就行了修改 }
}
}
} public static void copyFile(File fromFile, String toFile) throws IOException {
FileReader from = new FileReader(fromFile);
File ff = new File(toFile);
if (!ff.exists()) {
ff.createNewFile();
}
FileWriter to = new FileWriter(ff);
char[] ch = new char[(int) fromFile.length()];
while (from.read(ch) != -1)
;
from.close();
to.write(ch);
to.flush();
to.close();
System.out.println("文件成功复制!!!");
} /**
* @param args
*/
public static void main(String[] args) {
String getFile = System.getProperty("user.dir");
try {
System.out.println("filepath: " + getFile + File.separator + "bin");
File fromDir = new File(getFile + File.separator + "bin");
File toDir = new File(getFile + File.separator + "bin1"); if (fromDir.exists() && toDir.exists()) {
System.out.println("YES");
} CopyDirecotry(fromDir, toDir); } catch (Exception e) {
e.printStackTrace();
} }}