bufferedreader

Convert InputStream to BufferedReader

眉间皱痕 提交于 2019-11-28 03:16:52
I'm trying to read a text file line by line using InputStream from the assets directory in Android. I want to convert the InputStream to a BufferedReader to be able to use the readLine(). I have the following code: InputStream is; is = myContext.getAssets().open ("file.txt"); BufferedReader br = new BufferedReader (is); The third line drops the following error: Multiple markers at this line The constructor BufferedReader (InputStream) is undefinded. What I'm trying to do in C would be something like: StreamReader file; file = File.OpenText ("file.txt"); line = file.ReadLine(); line = file

What exactly does “Stream” and “Buffer” mean in Java I/O?

前提是你 提交于 2019-11-28 02:34:51
I just learned about input/output using BufferedReader . I wanted to know what exactly are the meanings of the term Stream and Buffer ? Also what does this line of code serves us: BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); Java has two kinds of classes for input and output (I/O): streams and readers/writers . Streams ( InputStream , OutputStream and everything that extends these) are for reading and writing binary data from files, the network, or whatever other device. Readers and writers are for reading and writing text (characters). They are a layer on top of

Java variables not initialized error

北战南征 提交于 2019-11-28 02:28:17
I am getting the following error in my Java program: Java variables not initialized error... error : variable nam and r not initialized location class child But nan and r already initialized, yet I am still getting the same error. public class cla { int rn; String name; void get(String a, int x) { name = a; rn = x; } void display() { System.out.println("student name: " + name + "roll no.:" + rn); } } import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class child { public static void main(String args[]) throws IOException { String nam; int r;

How can I read from a BufferedReader in Java without blocking?

独自空忆成欢 提交于 2019-11-28 01:47:54
问题 I want to send a command to a server, and find out if I get a response. Right now i am using BufferedReader 's readline() function, which blocks until there's a response from server, but all I want to do is verify that there's a response from the server in the first place. I tried using ready() or reset() to avoid this block, but it doesn't help. This is causing my program to get stuck waiting for the server to respond, which never happens. InputStreamReader seems to do the same thing, by my

Java-Convert String to int when using BufferedReader

泪湿孤枕 提交于 2019-11-28 00:59:26
问题 How do you convert a String to int when using BufferedReader? as far as i remember,its something like below: System.out.println("input a number"); int n=Integer.parseInt(br.readLine(System.in)); but for some reason,its not working. the error message says: no suitable method found for readLine(java.io.InputStream) it also says br.readLine is not applicable 回答1: An InputStreamReader needs to be specified in the constructor for the BufferedReader . The InputStreamReader turns the byte streams to

How do you set a timeout on BufferedReader and PrintWriter in Java 1.4?

橙三吉。 提交于 2019-11-28 00:45:45
How does one set a timeout on a BufferedReader and a PrintWriter created using a socket connection? Here is the code I have for the server right now, which works until either the server or the client crashes: while(isReceiving){ str = null; BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter pw = new PrintWriter(socket.getOutputStream(), true); while ((str = br.readLine()) != null){ System.out.println("Processing command " + str); pw.println(client.message(str)); } } Outside the scope of this code I have imposed a socket timeout of 1000ms, which

How should I read from a buffered reader?

让人想犯罪 __ 提交于 2019-11-27 23:23:16
I have the following example of reading from a buffered reader: while ((inputLine = input.readLine()) != null) { System.out.println("I got a message from a client: " + inputLine); } The code in the loop println will be executed whenever something appears in the buffered reader ( input in this case). In my case, if a client-application writes something to the socket, the code in the loop (in the server-application) will be executed. But I do not understand how it works. inputLine = input.readLine() waits until something appears in the buffered reader and when something appears there it returns

Reading lines with BufferedReader and checking for end of file

女生的网名这么多〃 提交于 2019-11-27 22:15:42
问题 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

Java: reading strings from a random access file with buffered input

余生长醉 提交于 2019-11-27 20:53:21
问题 I've never had close experiences with Java IO API before and I'm really frustrated now. I find it hard to believe how strange and complex it is and how hard it could be to do a simple task. My task: I have 2 positions (starting byte, ending byte), pos1 and pos2 . I need to read lines between these two bytes (including the starting one, not including the ending one) and use them as UTF8 String objects. For example, in most script languages it would be a very simple 1-2-3-liner like that (in

Socket, BufferedReader hangs at readLine()

倾然丶 夕夏残阳落幕 提交于 2019-11-27 20:27:47
问题 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"