bufferedreader

Java BufferedReader readline blocking?

让人想犯罪 __ 提交于 2019-11-29 04:23:51
I want to make an HTTP request and then get the response as sketched here: URLConnection c = new URL("http://foo.com").openConnection(); c.setDoOutput(true); /* write an http request here using a new OutputStreamWriter(c.getOutputStream) */ BufferedReader reader = new BufferedReader(new InputStreamReader(c.getInputStream)); reader.readLine(); But my question is, if the request I send takes a long time before a response is received, what happens in the call reader.readLine() above? Will this process stay running/runnable on the CPU or will it get taken off the CPU and be notified to wake up and

Reading lines with BufferedReader and checking for end of file

女生的网名这么多〃 提交于 2019-11-29 03:54:40
If I have something like this in my code: String line = r.readLine(); //Where r is a bufferedReader How can I avoid a crash if the next line is the end of the file? (i.e. null) I need to read the next line because there may be something there that I need to deal with but if there isn't the code just crashes. If there is something there then all is OK, but I can't be guaranteed that there will be something there. So if I do something like: (pseudo code): if (r.readLine is null) //End code else {check line again and excecute code depending on what the next line is} The issue I have with

Making io.BufferedReader from sys.stdin in Python2

南楼画角 提交于 2019-11-29 03:02:47
How can I make a BufferedReader object from a standard file object, like sys.stdin or what you get from 'open'? (Background: I need a peek() method, which the standard file objects fail at having. Any suggestions to solve this issue are also welcome.) I'd have sort of expected this to work, but it doesn't: >>> import sys >>> import io >>> io.BufferedReader(sys.stdin) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'file' object has no attribute 'readable' (This is Python 2.7) Hah, got it, at least for anything that has a file descriptor. stream = sys

java: how to use bufferedreader to read specific line

ぐ巨炮叔叔 提交于 2019-11-29 02:32:50
Lets say I have a text file called: data.txt (contains 2000 lines) How do I read given specific line from: 500-1500 and then 1500-2000 and display the output of specific line? this code will read whole files (2000 line) public static String getContents(File aFile) { StringBuffer contents = new StringBuffer(); try { BufferedReader input = new BufferedReader(new FileReader(aFile)); try { String line = null; while (( line = input.readLine()) != null){ contents.append(line); contents.append(System.getProperty("line.separator")); } } finally { input.close(); } } catch (IOException ex){ ex

BufferedReader to skip first line

戏子无情 提交于 2019-11-28 23:30:18
问题 I am using the following bufferedreader to read the lines of a file, BufferedReader reader = new BufferedReader(new FileReader(somepath)); while ((line1 = reader.readLine()) != null) { //some code } Now, I want to skip reading the first line of the file and I don't want to use a counter line int lineno to keep a count of the lines. How to do this? 回答1: You can try this BufferedReader reader = new BufferedReader(new FileReader(somepath)); reader.readLine(); // this will read the first line

Why is the performance of BufferedReader so much worse than BufferedInputStream?

让人想犯罪 __ 提交于 2019-11-28 23:22:38
I understand that using a BufferedReader (wrapping a FileReader) is going to be significantly slower than using a BufferedInputStream (wrapping a FileInputStream), because the raw bytes have to be converted to characters. But I don't understand why it is so much slower! Here are the two code samples that I'm using: BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(filename)); try { byte[] byteBuffer = new byte[bufferSize]; int numberOfBytes; do { numberOfBytes = inputStream.read(byteBuffer, 0, bufferSize); } while (numberOfBytes >= 0); } finally { inputStream.close(

Auto-Detect Character Encoding in Java

删除回忆录丶 提交于 2019-11-28 20:32:46
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 for this. What is a pretty standard way of doing this and detecting whether it was successful or not?

Socket, BufferedReader hangs at readLine()

左心房为你撑大大i 提交于 2019-11-28 18:54:21
I have a server which initially does this:- BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); for (;;) { String cmdLine = br.readLine(); if (cmdLine == null || cmdLine.length() == 0) break; ... } later it passes the socket to another class "foo" This class wait for application specific messages. BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); appCmd=br.readLine(); My client sends this sequence: "bar\n" "how are u?\n" "\n" "passing it to foo\n" "\n" The problem is that sometimes "foo" does not get its response. It hangs in the

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

[亡魂溺海] 提交于 2019-11-28 16:44:38
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 takes about 10 seconds. I tried to read array of bytes, but then searching for lines in each array that

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

允我心安 提交于 2019-11-28 14:15:10
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 have a trailing newline. I'd like to keep things how they were. Now I need to peek ahead to the next