bufferedreader

Sockets: BufferedReader readLine() blocks

人走茶凉 提交于 2019-11-30 12:24:11
I am using BufferedReader.readLine() method to read a response from a remote server (which is written in C and I have no access to source code). BufferedReader br = new BufferedReader(new InputStreamReader(in)); String line; while((line = br.readLine())!=null){ [...] } But it always blocks at the last line until it times out. So I used the following code: int b; while(true){ b = in.read; [...] } and I found out that the last byte read has an integer value of 13, which I think it is a carriage return, right? So why the readLine method blocks? How does the server usually signal an end of stream

Can I peek on a BufferedReader?

喜你入骨 提交于 2019-11-30 11:11:58
Is there a way to check if in BufferedReader object is something to read? Something like C++ cin.peek() . Thanks. You can try the "boolean ready()" method. From the Java 6 API doc: "A buffered character stream is ready if the buffer is not empty, or if the underlying character stream is ready." BufferedReader r = new BufferedReader(reader); if(r.ready()) { r.read(); } Gavin H You can use a PushbackReader . Using that you can read a character, then unread it. This essentially allows you to push it back. PushbackReader pr = new PushbackReader(reader); char c = (char)pr.read(); // do something to

How to read a text file into jtextarea in Java Swing

时光毁灭记忆、已成空白 提交于 2019-11-30 09:24:58
问题 Here is my code: try { String textLine; FileReader fr = new FileReader("ad.txt"); BufferedReader reader = new BufferedReader(fr); while((textLine=reader.readLine()) != null) { textLine = reader.readLine(); jTextArea1.read(reader, "jTextArea1"); } } catch (IOException ioe) { System.err.println(ioe); System.exit(1); } And my .txt file contains the following: contig00001 length=586 numreads=4 CGGGAAATTATCcGCGCCTTCACCGCCGCCGGTTCCACCGACGAACGGATACTGCGtGaa

Socket closed exception [duplicate]

删除回忆录丶 提交于 2019-11-30 08:30:01
问题 This question already has an answer here : Proper way to close an AutoCloseable (1 answer) Closed 7 months ago . I wrote a simple server and client example as below . Client : Open a connection Get outputstream , write to stream and close the output stream Get inputstream and read from the stream. Getting exception at this point public class DateServer { public static void main(String[] args) throws InterruptedException { ServerSocket serverSocket = null; Socket client = null; try {

BufferedReader not stating 'ready' when it should

大兔子大兔子 提交于 2019-11-30 08:29:56
问题 I am trying to read text from a web document using a BufferedReader over an InputStreamReader on an URL (to the file on some Apache server). String result = ""; URL url = new URL("http://someserver.domain/somefile"); BufferedReader in = null; in = new BufferedReader(new InputStreamReader(url.openStream(), "iso-8859-1")); result += in.readLine(); Now this works just fine. But Obviously I'd like the reader not to just read one line, but as many as there are in the file. Looking at the

Read data from a text file and create an object

為{幸葍}努か 提交于 2019-11-30 07:45:11
I need some help: I'm making a Supermarket simulation on Java, but I've got one problem, I have a text file (Stock.txt) where I have all the supermarket stock on it for example: 0-Bakery-Chocolate Cake-$12.5-250 1-Meat-Premium Steak-$2.6-120 2-Seafood-Tuna - $1.2-14 ... Where the first number is the "id" for the product, next is the department the product belongs, third is the name of the product, the next thing is the price, and the last number is how much pieces of the product the stock has. I have this class: public class Product { protected String name; protected double price; protected

Reading file from assets directory throws FileNotFoundException

主宰稳场 提交于 2019-11-30 06:31:30
I'm trying to read in a text file of a bunch of words that I want to use for a word game I am writing. This list is stored in the assets directory and is a txt file. But, whenever I attempt to open it, it throws an exception. List<String>wordList = new ArrayList<String>(); BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(getAssets().open("wordlist.txt"))); //throwing a FileNotFoundException? String word; while((word=br.readLine()) != null) wordList.add(word); //break txt file into different words, add to wordList } catch(IOException e) { e.printStackTrace(); }

Difference between BufferedReader and BufferedInputStream

谁说胖子不能爱 提交于 2019-11-30 06:13:32
What are the differences between BufferedReader , BufferedInputStream and Scanner in java? BufferedReader reads the text and BufferedInputStream reads byte . Is there any difference other than this? Igor I guess, the difference is the same as between reader and inputstream: one is character-based, another is byte-based. For example, reader normally supports encoding... Edit: Check this question: The difference between InputStream and InputStreamReader when reading multi-byte characters Kumaresh Babu BufferedInputStream reads the data in the buffer as bytes by using InputStream . BufferedReader

BufferedReader, detecting if there is text left to read

拟墨画扇 提交于 2019-11-30 05:28:07
问题 I'm running a thread and everytime it runs, It should be checking to see if there is a new line to read from the BufferedReader although, it gets stuck waiting for a line to exist, thus halting the entire code. if((inputLine = bufferedReader.readLine()) != null){ System.out.println(inputLine); JOptionPane.showMessageDialog(null, inputLine); } Is there a way to better check if there is text in a BufferedReader to be read? 回答1: No, there's no easy way to do that. BufferedReader has a ready call

how to read last line in a text file using java [duplicate]

天涯浪子 提交于 2019-11-30 04:46:18
问题 This question already has answers here : Quickly read the last line of a text file? (9 answers) Closed 6 years ago . I am making a log and I want to read the last line of the log.txt file, but I'm having trouble getting the BufferedReader to stop once the last line is read. Here's my code: try { String sCurrentLine; br = new BufferedReader(new FileReader("C:\\testing.txt")); while ((sCurrentLine = br.readLine()) != null) { System.out.println(sCurrentLine); } } catch (IOException e) { e