Get real time file size in java - Stack Overflow

There is an existing api which writes the file to particular location and the code snippet is like belo

There is an existing api which writes the file to particular location and the code snippet is like below:

            long lastUpdatedOn = System.currentTimeMillis();
            try(BufferedOutputStream fileOutputStream = new BufferedOutputStream(new FileOutputStream(filePath, true))) {
                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, bytesRead);
                    long now = System.currentTimeMillis();
                    if (now - lastUpdatedOn > 2 * 1000L) {
                        fileOutputStream.flush();
                        lastUpdatedOn = now;
                    }
                }

It is running in background. I want to have api(will hit on certain interval) which should return the real-time size of the file. Suppose filesize is 1GB. If 500MB file content is written at the moment when I hit the api then it should return that file-size. I have code like below:

java.nio.file.Path path = Paths.get(destinationFullFolderPath.toString());
    if (Files.exists(path)) {
                        resultMap.put(fileName, Files.size(path));
                    }

But I am constantly hitting this api and getting empty result and I get the result once total file is written. I am confused that where should I make change. Whether to make change in the api which is writing file or in the api which should return real-time filesize.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743965839a4537521.html

相关推荐

  • Get real time file size in java - Stack Overflow

    There is an existing api which writes the file to particular location and the code snippet is like belo

    2天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信