Java 将文件夹及其文件复制到其他位置
我想将F:\test复制到文件夹F:\123,这样我就有了文件夹F:\123\test 在测试文件夹中,我有两个文件:input和output.java 但是我不能这样做,错误是:F\123\test\output.java(系统找不到指定的路径) 这是我的密码:Java 将文件夹及其文件复制到其他位置,java,Java,我想将F:\test复制到文件夹F:\123,这样我就有了文件夹F:\123\test 在测试文件夹中,我有两个文件:input和output.java 但是我不能这样做,错误是:F\123\test\output.java(系统找不到指定的路径) 这是我的密码: Constant.fromPath = "F:\\test" Constant.toPath = "F\\123\\test"; File source = new File(Constant.fromPath
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();
}
publicstaticvoidcopyfolder(File srcFolder,File destFolder)抛出IOException
{
if(srcFolder.isDirectory())
{
如果(!destFolder.exists())
{
destFolder.mkdir();
}
字符串[]oChildren=srcFolder.list();
for(int i=0;i0)
{
oOutStream.write(对象字节,0,长度);
}
oInStream.close();
oOutStream.close();
}
请帮我修复我的错误。
Constant.toPath=“F\\123\\test”代码>应该是常量。toPath=“F:\\123\\test”代码>您可能想要签出,特别是各种方法。这将为您节省大量类似上述的烦人编码。对不起,我已经编辑了我的问题。在我的程序中,我从jLablel获得了它,但为了简单起见,我编写了它,这就是为什么我遗漏了:character。谢谢你的回答,我已经修复了我的bug。非常感谢