Java FileNotFoundException,FileOutputStream打开

Java FileNotFoundException,FileOutputStream打开,java,guava,Java,Guava,当我尝试创建FileOutputStream时,我遇到了FileNotFoundException。根据file.exists,文件不存在。我已经尝试了一切,比如file.mkdir。。。 我在mac电脑上,用的是gauva。 文件输入为“” 执行代码目标文件必须是目录 public boolean install(File mcpTarget) throws IOException { mcpTarget.mkdirs(); if (isMCPInstalled(mcpTar

当我尝试创建FileOutputStream时,我遇到了FileNotFoundException。根据file.exists,文件不存在。我已经尝试了一切,比如file.mkdir。。。 我在mac电脑上,用的是gauva。 文件输入为“”

执行代码目标文件必须是目录

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;
    }
}
公共静态布尔下载文件(字符串名称、文件路径、字符串下载URL)
{
System.out.println(String.format(“尝试下载文件:%s”,名称));
尝试
{
URL=新URL(下载URL);
final URLConnection connection=url.openConnection();
连接。设置连接超时(6000);
connection.setReadTimeout(6000);
InputSupplier urlSupplier=新的InputSupplier()
{
@凌驾
公共InputStream getInput()引发IOException
{
返回连接。getInputStream();
}
};
文件。副本(urlSupplier,路径);
返回true;
}
捕获(例外e)
{
e、 printStackTrace();
返回false;
}
}
这就是问题所在。您正在指定的文件上创建文件夹。替换为

mcpTarget.getParentFile().mkdirs();
(或者,由于您使用番石榴,请使用以下内容:)

另外,后者是前者的子集,因此您永远不需要同时调用这两个mkdir方法

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();
mcpTarget.getParentFile().mkdirs();