将文件夹及其文件复制到其他位置
问题描述:
我想将F:\ test复制到文件夹F:\ 123,以便我具有文件夹F:\ 123 \ test。将文件夹及其文件复制到其他位置
在测试文件夹我哈瓦2个文件:输入和output.java 但我不能这样做,埃罗是:˚F\ 123 \测试\ output.java(系统找不到指定的路径)
这里是我的代码:
Constant.fromPath = "F:\\test"
Constant.toPath = "F\\123\\test";
File source = new File(Constant.fromPath);
File destination = new File(Constant.toPath);
try
{
copyFolder(source, destination);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
这里是copyFolder功能
public static void copyFolder(File srcFolder, File destFolder) throws IOException
{
if (srcFolder.isDirectory())
{
if (! destFolder.exists())
{
destFolder.mkdir();
}
String[] oChildren = srcFolder.list();
for (int i=0; i < oChildren.length; i++)
{
copyFolder(new File(srcFolder, oChildren[i]), new File(destFolder, oChildren[i]));
}
}
else
{
if(destFolder.isDirectory())
{
copyFile(srcFolder, new File(destFolder, srcFolder.getName()));
}
else
{
copyFile(srcFolder, destFolder);
}
}
}
public static void copyFile(File srcFile, File destFile) throws IOException
{
InputStream oInStream = new FileInputStream(srcFile);
OutputStream oOutStream = new FileOutputStream(destFile);
// Transfer bytes from in to out
byte[] oBytes = new byte[1024];
int nLength;
BufferedInputStream oBuffInputStream = new BufferedInputStream(oInStream);
while ((nLength = oBuffInputStream.read(oBytes)) > 0)
{
oOutStream.write(oBytes, 0, nLength);
}
oInStream.close();
oOutStream.close();
}
请帮助我解决我的错误。
答
Constant.toPath = "F\\123\\test";
应该Constant.toPath = "F:\\123\\test";
我是sory.I编辑了我的问题。 在我的程序中,我从jLablel中获得它,但只是为了简单,我写它,这就是为什么我错过了:字符。 – 2009-12-25 13:01:50
感谢您的回答,我修复了我的bug。非常感谢 – 2009-12-25 13:03:46