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?
Asked
Active
Viewed 251 times
2
-
i am a new user i accept questions but u know sometimes you cant what can i do!!!? – Hussein Zawawi Nov 03 '11 at 12:19
-
You can always accept an answer, just click the tick below the vote count on the left. – Polynomial Nov 03 '11 at 12:22
-
even if does not answer the subject how it comes? – Hussein Zawawi Nov 03 '11 at 18:45
4 Answers
2
System.in reads line by line not the file contents at a time.So it is slow.

Android Killer
- 18,174
- 13
- 67
- 90
1
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?

Community
- 1
- 1

Polynomial
- 27,674
- 12
- 80
- 107
-
-
Could you not change it so the string that goes to `System.in` is a path to the file, then parse it out from there? – Polynomial Nov 03 '11 at 12:15
-
i wish :S it doesnt make any sense http://livearchive.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=378&page=show_problem&problem=2968 – Hussein Zawawi Nov 03 '11 at 12:17
-
0
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

Aleksandr Lovkov
- 65
- 9
0
Try to change the stream of system.in
System.setIn(new FileInputStream(fileName));

shift66
- 11,760
- 13
- 50
- 83