bufferedreader

Auto-Detect Character Encoding in Java

你说的曾经没有我的故事 提交于 2019-11-27 12:59:14
问题 Seems to be a fairly hit issue, but I've not yet been able to find a solution; perhaps because it comes in so many flavors. Here it is though. I'm trying to read some comma delimited files (occasionally the delimiters can be a little bit more unique than commas, but commas will suffice for now). The files are supposed to be standardized across the industry, but lately we've seen many different types of character set files coming in. I'd like to be able to set up a BufferedReader to compensate

Read all lines with BufferedReader

余生长醉 提交于 2019-11-27 12:56:10
问题 I want to type a multiple line text into the console using a BufferedReader and when I hit "Enter" to find the sum of the length of the whole text. The problem is that it seems I'm getting into an infinite loop and when I press "Enter" the program does not come to an end. My code is below: InputStreamReader instream = new InputStreamReader(System.in); BufferedReader buffer = new BufferedReader(instream); line= buffer.readLine(); while (line!=null){ length = length + line.length(); line=

Fastest Way To Read and Write Large Files Line By Line in Java

冷暖自知 提交于 2019-11-27 09:47:18
问题 I have been searching a lot for the fastest way to read and write again a large files (0.5 - 1 GB) in java with limited memory (about 64MB). Each line in the file represents a record, so I need to get them line by line. The file is a normal text file. I tried BufferedReader and BufferedWriter but it doesn't seem to be the best option. It takes about 35 seconds to read and write a file of size 0.5 GB, only read write with no processing. I think the bottleneck here is writing as reading alone

Do any Java stream-input libraries preserve line ending characters?

无人久伴 提交于 2019-11-27 08:13:34
问题 I'd like to iterate through a text file one line at a time, operate on the contents, and stream the result to a separate file. Textbook case for BufferedReader.readLine() . But: I need to glue my lines together with newlines, and what if the original file didn't have the "right" newlines for my platform (DOS files on Linux or vice versa)? I guess I could read ahead a bit in the stream and see what kind of line endings I find, even though that's really hacky. But: suppose my input file doesn't

Java: How do you read each line of a text file and setting each line as an array element?

坚强是说给别人听的谎言 提交于 2019-11-27 07:26:56
问题 I am trying to read questions that are listed in each line of a text file and then adding each line to an array, so that they can be called individually later on. I'm almost positive it is possible to do in Java but I am unsure how to do it. I did figure out how to read a whole text file and setting it all to a string: private static String readFile(String pathname) { String line = null; try { BufferedReader reader = new BufferedReader(new FileReader(pathname)); while((line = reader.readLine(

Why is Scanner slower than BufferedReader when reading from input?

三世轮回 提交于 2019-11-27 06:45:09
问题 I understand what is Scanner good for, and also when to use Scanner and when BufferedReader. I read a different, yet in some therm similar question Scanner vs. BufferedReader Why is Scanner so slow when I read from the input? I assume it has to do with that there is a small buffer in Scanner, but here I am lost. The original problem is from, Codechef , but I am not interested in that solution. Here is a code example with a given input: Input: 7 3 1 51 966369 7 9 999996 1 And the code import

How to find out which line separator BufferedReader#readLine() used to split the line?

梦想与她 提交于 2019-11-27 06:42:42
问题 I am reading a file via the BufferedReader String filename = ... br = new BufferedReader( new FileInputStream(filename)); while (true) { String s = br.readLine(); if (s == null) break; ... } I need to know if the lines are separated by '\n' or '\r\n' is there way I can find out ? I don't want to open the FileInputStream so to scan it initially. Ideally I would like to ask the BufferedReader since it must know. I am happy to override the BufferedReader to hack it but I really don't want to

Difference between java.io.PrintWriter and java.io.BufferedWriter?

限于喜欢 提交于 2019-11-27 06:15:35
Please look through code below: // A.class File file = new File("blah.txt"); FileWriter fileWriter = new FileWriter(file); PrintWriter printWriter = new PrintWriter(fileWriter); // B.class File file = new File("blah.txt"); FileWriter fileWriter = new FileWriter(file); BufferedWriter bWriter = new BufferedWriter(fileWriter); What is the difference between these two methods? When should we use PrintWriter over BufferedWriter? TofuBeer The API reference for BufferedWriter and PrintWriter detail the differences. The main reason to use the PrintWriter is to get access to the printXXX methods like

Java using scanner enter key pressed

你离开我真会死。 提交于 2019-11-27 05:33:59
I am programming using Java. I am trying write code which can recognize if the user presses the enter key in a console based program. How can I do this using java. I have been told that this can be done using either Scanner or, buffered input reader. I do not understand(or know how to use) buffered input reader. I tried to do do this using scanner but after pressing enter twice the program terminates, and it doesn't work Scanner readinput = new Scanner(System.in); String enterkey = "Hola"; System.out.print(enterkey); enterkey = readinput.nextLine(); System.out.print(enterkey); if(enterkey == "

Read the 30Million user id's one by one from the big file

拟墨画扇 提交于 2019-11-27 04:27:07
问题 I am trying to read a very big file using Java. That big file will have data like this, meaning each line will have an user id. 149905320 1165665384 66969324 886633368 1145241312 286585320 1008665352 And in that big file there will be around 30Million user id's. Now I am trying to read all the user id's one by one from that big file only once. Meaning each user id should be selected only once from that big file. For example, if I have 30Million user id's then it should print 30 Million user