简体   繁体   English

通过扫描仪读取大量输入

[英]Read large input through Scanner

I am trying to take some user input and process them in my program. 我正在尝试接受一些用户输入并在我的程序中处理它们。 For that using the Scanner class as below. 对于那些使用Scanner类如下。

Scanner scan = new Scanner(System.in);  
System.out.print("Input : ");
String input = scan.nextLine();

// Process the Data

scan.close();

The program works fine with small amount of data. 该程序适用于少量数据。 But if the input string length is huge (~100K characters), Scanner stops responding and fails to read any data. 但是如果输入字符串长度很大(~100K字符),则扫描程序停止响应并且无法读取任何数据。

Is there any different way or workaround to this problem ? 这个问题有什么不同的方式或解决方法吗?

Note : These is no possible way to store the data in a file and read from there. 注意:这些是将数据存储在文件中并从那里读取的可能方法。 It would be a nice option to read chunks of data from a file. 从文件中读取数据块是一个不错的选择。 But unfortunately there is no such implementation in my application. 但不幸的是,我的申请中没有这样的实施。

EDIT : As mentioned earlier, I need to read data directly from user (not from a file). 编辑:如前所述,我需要直接从用户(而不是从文件)读取数据。 Anyway, already tried to use the BufferReader. 无论如何,已经尝试使用BufferReader。 But does not seem like can read some data of around 100K characters. 但似乎不能读取大约100K字符的一些数据。

Here is the code below. 这是下面的代码。

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Input : ");

String input = br.readLine();

// Remaining Code

Well this behavior might be normal, you'll have to wait for a moment before the Scanner code does anything. 那么这种行为可能是正常的,你必须等待一段时间才能扫描代码做任何事情。 It's a big input and I guess the OS has to put it somewhere, so there is certain amount of time between the moment you press enter and the moment the JVM can read it. 这是一个很大的输入,我想操作系统必须把它放在某个地方,所以在你按Enter键和JVM读取它的那一刻之间有一定的时间。

You should benchmark it to see how much reading time evolves depending on you input. 你应该对它进行基准测试,看看你输入的阅读时间有多大。

I've made a code that read character per character from input. 我已经制作了一个代码,用于从输入中读取每个字符的字符。 It is not a good solution as it surely is slower (and dirtier) than using nextLine(), but it will show how fast is the buffering and reading on your computer: 这不是一个好的解决方案,因为它肯定比使用nextLine()更慢(和更脏),但它会显示您的计算机上的缓冲和读取速度有多快:

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);  
    Log.d("Input : ");

    StringBuilder test = new StringBuilder();

    String input = "";
    long measure = 0;
    while(input != null){
        input = scan.findInLine("."); // Should consume input one char per one char.

        measure++;
        if(input != null)
            test.append(input.charAt(0));

        if(measure == 1 || measure % 1000 == 0) // displays the reading time of first reads and the next thousand ones
            Log.d("Reading at:"+measure);

    }
    Log.d("End");
    scan.close();

    Log.d("size: "+test.toString().length());
    }
}

(note: you can replace Log.d(yourString) by System.out.println( new Date() +":" +yourString ) ) (注意:你可以用System.out.println( new Date() +":" +yourString )替换Log.d(yourString) System.out.println( new Date() +":" +yourString )

Test with this on your computer and gradually increase the input. 在计算机上测试并逐渐增加输入。 I'm not sure the response is linear. 我不确定响应是否是线性的。 It works well with 50000 char but it's in fact quite long with 100.000 (still, only a few minutes before I can read the first character). 它可以很好地使用50000个字符,但它实际上很长,只有100.000(仍然只有几分钟我才能阅读第一个字符)。

edit: the time between reads also increases with input size ... it takes 80 seconds for 1000 char reads with an 100.000 chars input (and a few seconds with 50.000). 编辑:读取之间的时间也随着输入大小而增加... 1000个字符读取需要80秒,输入100.000个字符(以及50.000几秒钟)。 At this rate, it will take more than 2 hours to read everything. 按照这个速度,阅读所有内容需要2个多小时。

I'm not sure, looping on System.in.read(); 我不确定,循环使用System.in.read(); might have better results (I don't know what the line end character is however ... \\EOF maybe ?), but you can still try that too. 可能会有更好的结果(我不知道线端字符是什么... \\EOF可能?),但你仍然可以尝试。 Again, I don't think it's a good solution, but reading from the comment, it's all you got left. 再说一遍,我认为这不是一个好的解决方案,但从评论中读到,这就是你所剩下的一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM