Java IO 传输问题

标签 java io

创建 FileUtils 类以重用所有 IO。 我们通过共享将文件(随机数量)从一台 Unix 服务器传输到另一台。

我的问题: 随机有一个文件会以 0kb 文件的形式出现,但无一异常(exception),并表示传输成功。

我们使用的基本方法:

resultCode = 0;
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        fis = new FileInputStream(fileLocation + orginalFile);
        fos = new FileOutputStream(toFolder + destinationFile);
        byte[] buf = new byte[1024];
        int i = 0;
        while ((i = fis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }

        resultCode = 1;
    } catch (Exception e) {
        resultCode = 2;
        //our logging

    } finally {
        try {
            fis.close();
            fos.close();
        } catch (Exception e) {
//our logging
        }
    }

    return resultCode;
}

对于造成这种情况的原因有什么想法吗?每次传输单个文件时都会调用 FileUtils 类。

谢谢

最佳答案

关闭之前刷新文件输出流 fos.flush() ,我不确定它是否能解决您的问题,但值得一试:

public void flush() throws IOException

Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

关于Java IO 传输问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16100384/

相关文章:

java - 从 Java 客户端应用程序使用 JSON Web 服务

io - 你如何附加到 Perl 6 中的文件?

c# - 错误 "An unhandled exception of type ' System.IO.IOException' 出现在 mscorlib.dll 中”

C - 对具有可变长度元素的大二进制文件进行排序

java - 句号到字符串

java - 用于 GWT 的 SQLite 数据库?

java - 在 Java 中存储 15,000 个项目

java - 如何使用多个 bean 配置文件配置 Spring BeanFactory?

node.js - 触摸文件,然后在 Node 中写入它

c# - 写入文件 C# 的性能