2013-06-18 24 views
23

我試圖複製一個文件,這樣java.nio.file.Files:在Java中複製文件並替換現有的目標

Files.copy(cfgFilePath, strTarget, StandardCopyOption.REPLACE_EXISTING); 

的問題是,Eclipse的說「的方法複製(路徑,路徑,CopyOption ...)在文件中不適用於參數(File,String,StandardCopyOption)「

我在Win7 x64上使用Eclipse和Java 7。我的項目設置爲使用Java 1.6兼容性。

有沒有一個解決方案或做我要創建這樣的事情作爲一種解決方法:

File temp = new File(target); 

if(temp.exists()) 
    temp.delete(); 

感謝。

回答

8

爲補充@assylias'回答:

如果你使用的Java 7,下降File完全。你想要的是Path

並獲得Path對象匹配的道路上你的文件系統,你這樣做:

Paths.get("path/to/file"); // argument may also be absolute 

得到真正的快習慣了。請注意,如果您仍然使用需要File的API,則Path的方法有.toFile()

需要注意的是,如果你是在你使用它返回File對象的API不幸的情況下,你總是可以做:

theFileObject.toPath() 

但在你的代碼,使用Path。系統化。沒有第二個想法。

編輯使用NIO使用1.6複製文件到另一個可以這樣完成;注意:Closer類是由Guava inspited:

public final class Closer 
    implements Closeable 
{ 
    private final List<Closeable> closeables = new ArrayList<Closeable>(); 

    // @Nullable is a JSR 305 annotation 
    public <T extends Closeable> T add(@Nullable final T closeable) 
    { 
     closeables.add(closeable); 
     return closeable; 
    } 

    public void closeQuietly() 
    { 
     try { 
      close(); 
     } catch (IOException ignored) { 
     } 
    } 

    @Override 
    public void close() 
     throws IOException 
    { 
     IOException toThrow = null; 
     final List<Closeable> l = new ArrayList<Closeable>(closeables); 
     Collections.reverse(l); 

     for (final Closeable closeable: l) { 
      if (closeable == null) 
       continue; 
      try { 
       closeable.close(); 
      } catch (IOException e) { 
       if (toThrow == null) 
        toThrow = e; 
      } 
     } 

     if (toThrow != null) 
      throw toThrow; 
    } 
} 

// Copy one file to another using NIO 
public static void doCopy(final File source, final File destination) 
    throws IOException 
{ 
    final Closer closer = new Closer(); 
    final RandomAccessFile src, dst; 
    final FileChannel in, out; 

    try { 
     src = closer.add(new RandomAccessFile(source.getCanonicalFile(), "r"); 
     dst = closer.add(new RandomAccessFile(destination.getCanonicalFile(), "rw"); 
     in = closer.add(src.getChannel()); 
     out = closer.add(dst.getChannel()); 
     in.transferTo(0L, in.size(), out); 
     out.force(false); 
    } finally { 
     closer.close(); 
    } 
} 
+0

+1完全同意 - 我幾乎不再使用文件。 – assylias

+0

我不知道這是否會工作,當我編譯Java 1.6的合規性水平如上所述,但無論如何謝謝你的筆記,將銘記未來。 –

+0

@commander_keen你的意思是你將部署的代碼運行1.6 JVM嗎? – fge

55

您需要通過Path參數由錯誤信息說明:

Path from = cfgFilePath.toPath(); //convert from File to Path 
Path to = Paths.get(strTarget); //convert from String to Path 
Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING); 

這假定您的strTarget是一個有效的路徑。

+0

精氨酸,爲什麼我認爲這是與copyOption一個問題......完全忽略了前兩個參數類型,謝謝。看起來好像我在熱度下編碼時間過長。 ;-) –

+0

UFF,在將導出的jar複製到帶有Java 1.6的PC之後(這就是爲什麼我提到我需要1.6兼容性,雖然我使用Java 7開發並編譯爲1.6),但它呻吟 「線程中的異常」main 「java.lang.NoSuchMethodError:java.io.File.toPath()Lja va/nio/file/Path;」 根據文檔,這是正確的,這種方法是隻有Java 7以後纔可用(我想知道爲什麼這不是在開發/設計時像我以前嘗試從Java 7中使用的其他東西批評,現在我必須檢查一切用手......) –

+0

@commander_keen'Files.copy'也不在Java 6中。 – assylias

1

strTarget是一個 「字符串」 對象,而不是一個 「路徑」 對象

相關問題