bufferedreader

How should I read from a buffered reader?

只愿长相守 提交于 2019-11-26 21:26:04
问题 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

How to use BufferedReader in Java [closed]

﹥>﹥吖頭↗ 提交于 2019-11-26 20:23:51
Sorry if this is an obvious question, but I can't seem to get it. I'm working on an assignment for a Data Structures course. It involves pulling data from a simple .dat file. We had never used any of the file-accessing options in Java before, so the professor just gave us the working code for that piece. A class called FileReadExample creates a new BufferedReader object, opens a file, and then is supposed to kick out a bunch of data about that file. But I cannot access any of the data at all. In a separate testMain file, I created a new FileReadExample object named fr and then attempted to

What is the buffer size in BufferedReader?

末鹿安然 提交于 2019-11-26 19:51:44
问题 What is the sense of buffer size in the constructor? BufferedReader(Reader in, int size) As i have written the program: import java.io.*; class bufferedReaderEx{ public static void main(String args[]){ InputStreamReader isr = null; BufferedReader br = null; try{ isr = new InputStreamReader(System.in); // System.out.println("Write data: "); // int i = isr.read(); // System.out.println("Data read is: " + i); //Thus the InputStreamReader is useful for reading the character from the stream System

Specific difference between bufferedreader and filereader

与世无争的帅哥 提交于 2019-11-26 19:29:31
I would like to know the specific difference between BufferedReader and FileReader . I do know that BufferedReader is much more efficient as opposed to FileReader , but can someone please explain why (specifically and in detail)? Thanks. In simple manner: A FileReader class is a general tool to read in characters from a File. The BufferedReader class can wrap around Readers, like FileReader, to buffer the input and improve efficiency. So you wouldn't use one over the other, but both at the same time by passing the FileReader object to the BufferedReader constructor. Very Detail FileReader is

Closing BufferedReader and System.in

送分小仙女□ 提交于 2019-11-26 19:11:51
Reader rdr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(rdr); String s; s = br.readLine(); br.close(); Scanner sc = new Scanner(System.in); s = sc.nextLine(); System.out.print(s); I've noticed that if I close the BufferedReader , I won't be able to insert input from the keyboard anymore, as System.in is somehow closed. Is there anyway I can keep br.close() (I need that in order to delete a file) and then add more input from the keyboard? Looks like you need: http://commons.apache.org/io/apidocs/org/apache/commons/io/input/CloseShieldInputStream.html Wrap that

Reset buffer with BufferedReader in Java?

余生颓废 提交于 2019-11-26 16:39:18
I am using class BufferedReader to read line by line in the buffer. When reading the last line in the buffer, I want to start reading from the beginning of the buffer again. I have read about the mark() and reset() , I am not sure its usage but I don't think they can help me out of this. Does anyone know how to start reading from the beginning of the buffer after reaching the last line? Like we can use seek(0) of the RandomAccessFile ? mark/reset is what you want, however you can't really use it on the BufferedReader, because it can only reset back a certain number of bytes (the buffer size).

Run .exe file in Java from file location

随声附和 提交于 2019-11-26 16:33:10
I have to open a .exe file from my Java program. So I tried following code First. Process process = runtime.exec("c:\\program files\\test\\test.exe"); But I was getting some error. Then I found out that the exe has to be launched from that location that is c://program files/test/ only then it will open with out errors. So I decided to write a .bat file and execute so that it will cd to that location and execute the .exe file. Following is my code: BufferedWriter fileOut; String itsFileLocation = "c:\\program files\\test\\" System.out.println(itsFileLocation); try { fileOut = new BufferedWriter

Android Reading from an Input stream efficiently

人盡茶涼 提交于 2019-11-26 14:56:42
I am making an HTTP get request to a website for an android application I am making. I am using a DefaultHttpClient and using HttpGet to issue the request. I get the entity response and from this obtain an InputStream object for getting the html of the page. I then cycle through the reply doing as follows: BufferedReader r = new BufferedReader(new InputStreamReader(inputStream)); String x = ""; x = r.readLine(); String total = ""; while(x!= null){ total += x; x = r.readLine(); } However this is horrendously slow. Is this inefficient? I'm not loading a big web page - www.cokezone.co.uk so the

What are mark and reset in BufferedReader?

耗尽温柔 提交于 2019-11-26 14:06:44
问题 I would like to know what are the mark() and reset() methods of BufferedReader ? How do I use them? I read the Javadoc but as a beginner I was unable to understand it. 回答1: The mark and reset methods of streams provide a way to jump backwards in the stream and re-read data. When you call mark() on a BufferedReader it will begin keeping data you read from that point forwards in its internal buffer. When you call reset() it will jump back to the marked position of the stream, so the next read()

Do I need to close() both FileReader and BufferedReader?

陌路散爱 提交于 2019-11-26 11:40:48
I'm reading a local file using a BufferedReader wrapped around a FileReader: BufferedReader reader = new BufferedReader(new FileReader(fileName)); // read the file // (error handling snipped) reader.close(); Do I need to close() the FileReader as well, or will the wrapper handle that? I've seen code where people do something like this: FileReader fReader = new FileReader(fileName); BufferedReader bReader = new BufferedReader(fReader); // read the file // (error handling snipped) bReader.close(); fReader.close(); This method is called from a servlet, and I'd like to make sure I don't leave any