Reputation: 2937
I have a file with 100000 line when am using System.in it takes more than 1 minute to get the input but when I use a file to read the input it doesn't take time. what is the solution to keep using System.in but with more speed?
Upvotes: 2
Views: 320
Reputation: 65
You need to use StringTokenizer if you want to read file very fast. https://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html
Upvotes: 0
Reputation: 28316
System.in
is inherently slow because it's taking the data in line by line (checking for newlines), rather than doing a large block copy from a file mapped in virtual memory.
There's no real way to speed up System.in
, and this sounds like a situation where reading a file would be much more ideal.
Update: You may want to look at this question: What's the fastest way to read from System.in in Java?
Upvotes: 1
Reputation: 11958
Try to change the stream of system.in
System.setIn(new FileInputStream(fileName));
Upvotes: 0
Reputation: 18499
System.in reads line by line not the file contents at a time.So it is slow.
Upvotes: 2