如何处理创建的损坏文件但发生 IOException?

发布于 2024-11-25 11:43:12 字数 2079 浏览 1 评论 0原文

您能否建议如何处理这些情况?我知道在第二个例子中,在unix上很少发生这种情况,是吗?如果访问权限没问题的话。该文件甚至不会被创建。我不明白为什么 IOException 在那里,无论它是否被创建,为什么我们必须为 IOException 烦恼?

但在第一个示例中,将会有一个损坏的僵尸文件。现在如果你告诉用户重新上传,同样的事情可能会发生。如果你不能这样做,并且输入流没有标记。您丢失了数据吗?我真的不喜欢Java中的这种做法,我希望Java 7中的新IO更好,

通常会删除它吗

public void inputStreamToFile(InputStream in, File file) throws SystemException {

    OutputStream out;
    try {
        out = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        throw new SystemException("Temporary file created : " + file.getAbsolutePath() + " but not found to be populated", e);
    }

    boolean fileCorrupted = false;
    int read = 0;
    byte[] bytes = new byte[1024];

    try {
        while ((read = in.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
    } catch (IOException e) {
        fileCorrupted = true;
        logger.fatal("IO went wrong for file : " + file.getAbsolutePath(), e);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);

                    if(fileCorrupted) {
        ???
                    }
    }
}


public File createTempFile(String fileId, String ext, String root) throws SystemException {

    String fileName = fileId + "." + ext;

    File dir = new File(root);

    if (!dir.exists()) {
        if (!dir.mkdirs())
            throw new SystemException("Directory " + dir.getAbsolutePath() + " already exists most probably");
    }

    File file = new File(dir, fileName);

    boolean fileCreated = false;
    boolean fileCorrupted = false;
    try {
        fileCreated = file.createNewFile();
    } catch (IOException e) {
        fileCorrupted = true;
        logger.error("Temp file " + file.getAbsolutePath() + " creation fail", e);
    } finally {

        if (fileCreated)
            return file;
        else if (!fileCreated && !fileCorrupted)
            throw new SystemException("File " + file.getAbsolutePath() + " already exists most probably");
        else if (!fileCreated && fileCorrupted) {

        }
    }

}

Could you please suggest how to deal with these situations ? I understand that in the second example, it is very rare that it would happen on unix, is it ? If access rights are alright. Also the file wouldn't be even created. I don't understand why the IOException is there, either it is created or not, why do we have to bother with IOException ?

But in the first example, there will be a corrupted zombie file. Now if you tell the user to upload it again, the same thing may happen. If you can't do that, and the inputstream has no marker. You loose your data ? I really don't like how this is done in Java, I hope the new IO in Java 7 is better

Is it usual to delete it

public void inputStreamToFile(InputStream in, File file) throws SystemException {

    OutputStream out;
    try {
        out = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        throw new SystemException("Temporary file created : " + file.getAbsolutePath() + " but not found to be populated", e);
    }

    boolean fileCorrupted = false;
    int read = 0;
    byte[] bytes = new byte[1024];

    try {
        while ((read = in.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
    } catch (IOException e) {
        fileCorrupted = true;
        logger.fatal("IO went wrong for file : " + file.getAbsolutePath(), e);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);

                    if(fileCorrupted) {
        ???
                    }
    }
}


public File createTempFile(String fileId, String ext, String root) throws SystemException {

    String fileName = fileId + "." + ext;

    File dir = new File(root);

    if (!dir.exists()) {
        if (!dir.mkdirs())
            throw new SystemException("Directory " + dir.getAbsolutePath() + " already exists most probably");
    }

    File file = new File(dir, fileName);

    boolean fileCreated = false;
    boolean fileCorrupted = false;
    try {
        fileCreated = file.createNewFile();
    } catch (IOException e) {
        fileCorrupted = true;
        logger.error("Temp file " + file.getAbsolutePath() + " creation fail", e);
    } finally {

        if (fileCreated)
            return file;
        else if (!fileCreated && !fileCorrupted)
            throw new SystemException("File " + file.getAbsolutePath() + " already exists most probably");
        else if (!fileCreated && fileCorrupted) {

        }
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉怎暖 2024-12-02 11:43:12

我真的不喜欢 Java 中的实现方式,我希望 Java 7 中的新 IO 更好

我不确定 Java 与您使用它的任何其他编程语言/环境有何不同:

  • 客户端发送 当您读取数据时,通过网络将一些数据
  • 写入本地文件

无论语言/工具/环境如何,连接都可能中断或丢失,客户端消失,磁盘损坏死,或发生任何其他错误。 I/O 错误可能发生在任何环境中。

在这种情况下您可以执行的操作很大程度上取决于具体情况和发生的错误。例如,数据是否以某种方式构建,您可以要求用户从记录 1000 继续上传?然而,没有一个解决方案可以满足所有情况。

I really don't like how this is done in Java, I hope the new IO in Java 7 is better

I'm not sure how Java is different than any other programming language/environment in the way you are using it:

  • a client sends some data to your over the wire
  • as you read it, you write it to a local file

Regardless of the language/tools/environment, it's possible for the connection to be interrupted or lost, for the client to go away, for the disk to die, or for any other error to occur. I/O errors can occur in any and all environments.

What you can do in this situation is highly dependent on the situation and the error that occured. For example, is the data structured in some way where you could ask the user to resume uploading from record 1000, for example? However, there is no single solution that fits all here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文