當前正在開發一個程序設計爲在Windows和OSX上工作,我有這個功能複製一個文件,在Windows中完美的作品,但是當我在osx中嘗試它時,我得到一個IOException:「no such文件或目錄」,我研究了一點,發現可能的目標目錄不存在(雖然它確實存在),所以我說這幾行:file.getParentFile()。mkdirs();在osx中創建重複的文件夾
if(!f2.getParentFile().exists())
{
f2.getParentFile().mkdirs();
}
if(!f2.exists())
{
f2.createNewFile();
}
這似乎解決了問題,但我當尋找複製的文件(位於文件)我一開始無法找到它,但後來我看到該程序實際上創建了一個我想保存文件的文件夾,所以我最終得到了兩個文件夾完全相同的「文件」名稱,這裏是代碼的其餘部分:
public static Boolean copyfile(String srFile, String dtFile)
{
{
try
{
File f1 = new File(srFile);
File f2 = new File(dtFile);
if(!f2.getParentFile().exists())
{
f2.getParentFile().mkdirs();
}
if(!f2.exists())
{
f2.createNewFile();
}
InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File copied.");
}
catch(FileNotFoundException ex){
System.out.println(ex.getMessage() + " in the specified directory.");
JOptionPane.showMessageDialog(new JFrame(), ex.getMessage());
return true;
}
catch(IOException e){
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(new JFrame(), e.getMessage());
return true;
}
}
return false;
}
這顯然不是理想的結果,我該怎麼做才能讓程序識別我想要保存文件的文件夾?
更新:我使用的LS-B命令,並將其顯示在不同的語言的兩個文件夾:「文件」和「Documentos」,即使它們都顯示爲「Documentos」取景
謝謝,但即時通過您提供的代碼得到相同的結果,它的工作原理,但我留下重複的文件夾 –