bufferedreader

Closing BufferedReader and InputStreamReader

心不动则不痛 提交于 2019-11-28 12:09:57
This piece of code is creating memory leak issues cause of BufferedReader and InputStreamReader which I think might be happening cause of some exceptions. How should I change it? try{ URL url = new URL(sMyUrl); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); while ((str = in.readLine()) != null) { jsonString += str; } in.close(); }catch(Exception e){ } It would be safer to close your stream using a try..finally block. You might also use a StringBuilder as it is designed for concatenating strings. You should also avoid catching Exception and doing nothing with

Why is Scanner slower than BufferedReader when reading from input?

北慕城南 提交于 2019-11-28 12:05:39
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 java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main

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

人盡茶涼 提交于 2019-11-28 12:01:44
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 open the filestream twice. Thanks, Note: the current line separator (returned by System.getProperty("line

Why is the default char buffer size of BufferedReader 8192?

不羁的心 提交于 2019-11-28 11:33:36
When I construct a new BufferedReader it is providing me a buffer of 8192 characters. What is the logic/reason behind this? 8192 = 2 to the power of 13 Traditionally, memory managers and paging files in the operating system work on pages that are sized in powers of 2. This allows very efficient multiply/divide operations to be performed with left/right shift operations. When working with a buffer, the worst case scenario is to have a buffer with size 1 byte longer than the page size (that would result in an extra page swap with very low benefit). So the default buffer sizes will also tend to

BufferedReader readLine() blocks

蓝咒 提交于 2019-11-28 11:07:32
When receiving data using readLine(), even though I put a "\n" at the end of the message using the .flush when sending the message, the while loop that reads my message still blocks. Only when closing the socket connection, it leaves the loop. Here's the client code : bos = new BufferedOutputStream(socket. getOutputStream()); bis = new BufferedInputStream(socket. getInputStream()); osw = new OutputStreamWriter(bos, "UTF-8"); osw.write(REG_CMD + "\n"); osw.flush(); isr = new InputStreamReader(bis, "UTF-8"); BufferedReader br = new BufferedReader(isr); String response = ""; String line; while(

error message : stream closed

大兔子大兔子 提交于 2019-11-28 10:45:22
问题 Upon running following code under class FlightSearch String moreSearch = "y"; List<Flight> resultList; // load initial flight data into DB if (!init()) { return; } // A background thread to monitor changes in csv repository FileListner fl = new FileListner(); fl.start(); do { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); // main thread gets input Input inputQuery = new Input(); try { inputQuery.getQuery(); } catch (InvalidException e1)

Maximum line length for BufferedReader.readLine() in Java?

此生再无相见时 提交于 2019-11-28 07:25:50
I use BufferedReader's readLine() method to read lines of text from a socket. There is no obvious way to limit the length of the line read. I am worried that the source of the data can (maliciously or by mistake) write a lot of data without any line feed character, and this will cause BufferedReader to allocate an unbounded amount of memory. Is there a way to avoid that? Or do I have to implement a bounded version of readLine() myself? The simplest way to do this will be to implement your own bounded line reader. Or even simpler, reuse the code from this BoundedBufferedReader class . Actually,

Java - Read all .txt files in folder

旧巷老猫 提交于 2019-11-28 06:25:43
Let's say, I have a folder called maps and inside maps I have map1.txt , map2.txt, and map3.txt . How can I use Java and the BufferReader to read all of the .txt files in folder maps (if it is at all possible)? Andrew White Something like the following should get you going, note that I use apache commons FileUtils instead of messing with buffers and streams for simplicity... File folder = new File("/path/to/files"); File[] listOfFiles = folder.listFiles(); for (int i = 0; i < listOfFiles.length; i++) { File file = listOfFiles[i]; if (file.isFile() && file.getName().endsWith(".txt")) { String

.NET equivalent of Java's BufferedReader

感情迁移 提交于 2019-11-28 05:52:42
问题 I have that code in Java public void read() throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(this.socket.getInputStream(), "UTF8")); String requestURL = null; Vector property = new Vector(); String line; //MORE OF CODE } If You need full code here is paste. I want rewrite that to C# But i don't know which is equivalent to BufferReader. I have socket, and i want read from socket InputStream (with UTF8) Thanks. 回答1: Something like this ought to do you, though I

Why is my String returning “\ufffd\ufffdN a m e”

筅森魡賤 提交于 2019-11-28 04:37:32
问题 This is my method public void readFile3()throws IOException { try { FileReader fr = new FileReader(Path3); BufferedReader br = new BufferedReader(fr); String s = br.readLine(); int a =1; while( a != 2) { s = br.readLine(); a ++; } Storage.add(s); br.close(); } catch(IOException e) { System.out.println(e.getMessage()); } } For some reason I am unable to read the file which only contains this " Name Intel(R) Core(TM) i5-2500 CPU @ 3.30GHz " When i debug the code the String s is being returned