我想將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();
}
請幫助我解決我的錯誤。
我是sory.I編輯了我的問題。 在我的程序中,我從jLablel中獲得它,但只是爲了簡單,我寫它,這就是爲什麼我錯過了:字符。 – 2009-12-25 13:01:50
感謝您的回答,我修復了我的bug。非常感謝 – 2009-12-25 13:03:46