java - FileOutputStream 打开时发生 FileNotFoundException

标签 java guava

当我尝试创建 FileOutputStream 时遇到 FileNotFoundException。根据 file.exists,该文件确实存在。我已经尝试过诸如 file.mkdir(s) 之类的一切... 我在 Mac 上,使用 gauva。 文件输入为 ''

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;
    }
}

最佳答案

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

这就是问题所在。您正在指定文件处创建文件夹。将其替换为

mcpTarget.getParentFile().mkdirs();

(或者,由于您使用 Guava,请使用: Files.createParentDirs(mcpTarget) )

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

关于java - FileOutputStream 打开时发生 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20938016/

相关文章:

java - 如何解决警告: This AsyncTask class should be static or leaks might occur

java - 具有多种不同 fragment 类型的 Android BaseFragment 类

java - 用于表达测试一个 `Predicate` 是否比另一个 `Predicate` 更严格的操作的 API?

java - Apache 的 StringUtils.isBlank(str) 与 Guava 的 Strings.isNullOrEmpty(str) : Should you routinely check for whitespace?

java - Guava 分离器的图案

java - VBox拖动闪烁

java - Storm Bolt 无法从 Spout 反序列化对象

java - 在 GridPane JavaFx 中选择单元格

java - 迭代 ImmutableSet 和 ImmutableList 并改变对象

grails - spring-security-rest 和 guava 之间的间接 jar 冲突导致 NoSuchMethod 错误