简体   繁体   English

FileOutputStream打开的FileNotFoundException

[英]FileNotFoundException with FileOutputStream open

I'm having a FileNotFoundException when i try to create a FileOutputStream. 我尝试创建FileOutputStream时遇到FileNotFoundException。 The file does exists according to file.exists. 该文件根据file.exists存在。 i've tried everything like file.mkdir(s) ... I'm on a mac and i'm using gauva. 我已经尝试过像file.mkdir(s)这样的东西......我在Mac上,而且我正在使用gauva。 The file input is '' 文件输入是''

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)

the code in the main class. 主类中的代码。

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

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

The execution code The mcpTarget file has to be a directory 执行代码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;
}

Download MCP method 下载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;
}

and the DownloadUtil.DownloadFile method 和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;
    }
}
mcpTarget.mkdirs();
mcpTarget.mkdir();

This is the problem. 这就是问题。 You are creating a folder at the specified file. 您正在指定的文件中创建一个文件夹。 Replace this with 替换为

mcpTarget.getParentFile().mkdirs();

(or, since you use Guava, use this: Files.createParentDirs(mcpTarget) ) (或者,因为你使用Guava,使用它: Files.createParentDirs(mcpTarget)

Also, the latter is a subset of the former, so you never need to call both of the mkdir methods. 此外,后者是前者的子集,因此您永远不需要调用两个mkdir方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM