2014-01-05 68 views
0

我有一個FileNotFoundException當我嘗試創建一個FileOutputStream。該文件根據file.exists確實存在。我試過一切像file.mkdir(s)... 我在一個mac和我使用gauva。 文件輸入爲''FileNotFoundException與FileOutputStream打開

java.io.FileNotFoundException: /Users/big_Xplosion/mods/Blaze-Installer/installer/test 
at java.io.FileOutputStream.open(Native Method) 
at java.io.FileOutputStream.<init>(FileOutputStream.java:194) 
at com.google.common.io.Files$FileByteSink.openStream(Files.java:223) 
at com.google.common.io.Files$FileByteSink.openStream(Files.java:211) 
at com.google.common.io.ByteSource.copyTo(ByteSource.java:203) 
at com.google.common.io.Files.copy(Files.java:382) 
at com.big_Xplosion.blazeInstaller.util.DownloadUtil.downloadFile(DownloadUtil.java:80) 
at com.big_Xplosion.blazeInstaller.action.MCPInstall.downloadMCP(MCPInstall.java:78) 
at com.big_Xplosion.blazeInstaller.action.MCPInstall.install(MCPInstall.java:30) 
at com.big_Xplosion.blazeInstaller.util.InstallType.install(InstallType.java:37) 
at com.big_Xplosion.blazeInstaller.BlazeInstaller.handleOptions(BlazeInstaller.java:51) 
at com.big_Xplosion.blazeInstaller.BlazeInstaller.main(BlazeInstaller.java:26) 

主類中的代碼。

File file = mcpSpec.value(options); //the file input given is 'test' 

     try 
     { 
      InstallType.MCP.install(file.getAbsoluteFile()); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 

執行代碼的mcpTarget文件必須是一個目錄

public boolean install(File mcpTarget) throws IOException 
{ 
    mcpTarget.mkdirs(); 

    if (isMCPInstalled(mcpTarget)) 
     System.out.println(String.format("MCP is already installed in %s, skipped download and extraction.", mcpTarget)); 
    else if (isMCPDownloaded(mcpTarget)) 
    { 
     if (!unpackMCPZip(mcpTarget)) 
      return false; 
    } 
    else 
    { 
     if (!downloadMCP(mcpTarget)) 
      return false; 

     if (!unpackMCPZip(mcpTarget)) 
      return false; 
    } 

    System.out.println("Successfully downloaded and unpacked MCP"); 

    return false; 
} 

下載MCP方法

public boolean downloadMCP(File targetFile) 
{ 
    String mcpURL = new UnresolvedString(LibURL.MCP_DOWNLOAD_URL, new VersionResolver()).call(); 

    if (!DownloadUtil.downloadFile("MCP", targetFile, mcpURL)) 
    { 
     System.out.println("Failed to download MCP, please try again and if it still doesn't work contact a dev."); 
     return false; 
    } 

    return true; 
} 

和DownloadUtil.DownloadFile方法

public static boolean downloadFile(String name, File path, String downloadUrl) 
{ 
    System.out.println(String.format("Attempt at downloading file: %s", name)); 

    try 
    { 
     URL url = new URL(downloadUrl); 
     final URLConnection connection = url.openConnection(); 
     connection.setConnectTimeout(6000); 
     connection.setReadTimeout(6000); 

     InputSupplier<InputStream> urlSupplier = new InputSupplier<InputStream>() 
     { 
      @Override 
      public InputStream getInput() throws IOException 
      { 
       return connection.getInputStream(); 
      } 
     }; 

     Files.copy(urlSupplier, path); 

     return true; 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 

     return false; 
    } 
} 

回答

2
mcpTarget.mkdirs(); 
mcpTarget.mkdir(); 

這是問題所在。您正在指定的文件中創建一個文件夾。與

mcpTarget.getParentFile().mkdirs(); 

更換這(因爲你使用芭樂或者,使用此:Files.createParentDirs(mcpTarget)

而且,後者是前者的一個子集,所以你永遠需要通話雙方的MKDIR方法。

+0

仍然收到相同的錯誤:( –

相關問題