在尚未完成复制/上传的同时读取文件内容 [英] Reading a file content while it hasn't finished being copied/uploaded

查看:133
本文介绍了在尚未完成复制/上传的同时读取文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每5秒(例如),服务器检查文件是否已添加到特定目录。如果是,它会读取并处理它们。相关文件可能相当大(例如100+ Mo),因此将它们复制/上传到所述目录可能很长。

Every 5 seconds (for example), a server checks if files have been added to a specific directory. If yes, it reads and processes them. The concerned files can be quite big (100+ Mo for example), so copying/uploading them to the said directory can be quite long.

如果服务器尝试访问尚未完成复制/上传的文件? JAVA如何管理这些并发访问?

What if the server tries to access a file that hasn't finished being copied/uploaded? How does JAVA manage these concurrent accesses? Does it depend on the OS of the server?

我试过,复制一个约1300000行的TXT文件即约200Mo)从远程服务器到我的本地计算机:大约需要5秒钟。在此期间,我运行以下JAVA类:

I made a try, copying a ~1300000-line TXT file (i.e. about 200 Mo) from a remote server to my local computer: it takes about 5 seconds. During this lapse, I run the following JAVA class:

public static void main(String[] args) throws Exception {

    String local = "C:\\large.txt";

    BufferedReader reader = new BufferedReader(new FileReader(local));
    int lines = 0;
    while (reader.readLine() != null)
        lines++;
    reader.close();

    System.out.println(lines + " lines");

}

我得到以下异常:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2882)
    at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)
    at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:515)
    at java.lang.StringBuffer.append(StringBuffer.java:306)
    at java.io.BufferedReader.readLine(BufferedReader.java:345)
    at java.io.BufferedReader.readLine(BufferedReader.java:362)
    at main.Main.main(Main.java:15)

当文件完成复制后运行类时,我得到预期的输出(即 1229761行),因此异常不是由于文件的大小因为我们可以想到在第一位)。什么是JAVA在后台执行,抛出此 OutOfMemoryError 异常?

When running the class once the file has finished being copied, I get the expected output (i.e. 1229761 lines), so the exception isn't due to the size of the file (as we could think in the first place). What is JAVA doing in background, that threw this OutOfMemoryError exception?

推荐答案



JAVA如何管理这些并发访问?是否依赖于服务器的操作系统?

How does JAVA manage these concurrent accesses? Does it depend on the OS of the server?


这取决于具体的操作系统。如果您在单个JVM中运行副本和服务器 AsynchronousFileChannel(新的1.7)类可以是一个很大的帮助。但是,如果客户端和服务器由不同的JVM表示(或者甚至更多,在不同的机器上启动),这一切都变成一个特定的平台。

It depends on the specific OS. If you run a copy and server in a single JVM AsynchronousFileChannel (new in 1.7) class could be of a great help. However, if client and server are represented by different JVMs (or even more, are started on a different machines) it all turns to be a platform specific.

JavaDoc for AsynchronousFileChannel:



与FileChannel一样,由该类的实例保证与同一程序中其他实例提供的同一文件的其他视图一致。然而,由于该类的实例提供的视图可能或可能不与由于由底层操作系统执行的高速缓存和由网络文件系统协议引起的延迟的其他并发运行的程序所看到的视图一致。强>。无论这些其他程序的编写语言,以及它们是在同一台机器上运行还是在其他机器上运行,都是如此。任何此类不一致的确切性质是系统依赖的,因此未指定。

As with FileChannel, the view of a file provided by an instance of this class is guaranteed to be consistent with other views of the same file provided by other instances in the same program. The view provided by an instance of this class may or may not, however, be consistent with the views seen by other concurrently-running programs due to caching performed by the underlying operating system and delays induced by network-filesystem protocols. This is true regardless of the language in which these other programs are written, and whether they are running on the same machine or on some other machine. The exact nature of any such inconsistencies are system-dependent and are therefore unspecified.


这篇关于在尚未完成复制/上传的同时读取文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆