bufferedreader

Closing BufferedReader and InputStreamReader

一笑奈何 提交于 2019-12-17 20:05:34
问题 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){ } 回答1: It would be safer to close your stream using a try..finally block. You might also use a StringBuilder as

Why is the default char buffer size of BufferedReader 8192?

杀马特。学长 韩版系。学妹 提交于 2019-12-17 19:47:15
问题 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 回答1: 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

How to read BufferedReader faster

天大地大妈咪最大 提交于 2019-12-17 15:48:16
问题 I want to optimize this code: InputStream is = rp.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String text = ""; String aux = ""; while ((aux = reader.readLine()) != null) { text += aux; } The thing is that i don't know how to read the content of the bufferedreader and copy it in a String faster than what I have above. I need to spend as little time as possible. Thank you 回答1: Using string concatenation in a loop is the classic performance

How can I make a copy of a BufferedReader?

老子叫甜甜 提交于 2019-12-17 12:05:21
问题 I am using a BufferedReader constructor to make a new copy of an existing BufferedReader . BufferedReader buffReader = new BufferedReader(originalBuffReader); The new buffReader is working fine, but when I do originalBuffReader.readLine() it gives me null . Is there any other way I can make a new bufferReader without affecting my original BufferedReader . FYI: I am getting bufferReader as an input to my method; and I do not have a access to the source. 回答1: Any other way I can make a new

Difference between java.io.PrintWriter and java.io.BufferedWriter?

半世苍凉 提交于 2019-12-17 10:18:22
问题 Please look through code below: // A.class File file = new File("blah.txt"); FileWriter fileWriter = new FileWriter(file); PrintWriter printWriter = new PrintWriter(fileWriter); // B.class File file = new File("blah.txt"); FileWriter fileWriter = new FileWriter(file); BufferedWriter bWriter = new BufferedWriter(fileWriter); What is the difference between these two methods? When should we use PrintWriter over BufferedWriter? 回答1: The API reference for BufferedWriter and PrintWriter detail the

How to use BufferedReader in Java [closed]

回眸只為那壹抹淺笑 提交于 2019-12-17 05:01:13
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . 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

How do I read the last “n” bytes of a file in Java

本小妞迷上赌 提交于 2019-12-14 03:49:00
问题 How do I read the last n number of bytes from a file, without using RandomAccessFile. The last 6 bytes in my files contain crucial information when writing the files back. I need to write my original files, and then append the last 6 bytes elsewhere. Any guidance? Thanks 回答1: You have to do it by using RandomAccessFile. Instances of this class support both reading and writing to a random access file. A random access file behaves like a large array of bytes stored in the file system.

Java: Outputting text file to Console

放肆的年华 提交于 2019-12-14 00:24:21
问题 I'm attempting to output a text file to the console with Java. I was wondering what is the most efficient way of doing so? I've researched several methods however, it's difficult to discern which is the least performance impacted solution. Outputting a text file to the console would involve reading in each line in the file, then writing it to the console. Is it better to use: Buffered Reader with a FileReader, reading in lines and doing a bunch of system.out.println calls? BufferedReader in =

Java BufferedReader

旧巷老猫 提交于 2019-12-13 19:04:40
问题 I'm looking at this tutorial about BufferedReader at youtube https://www.youtube.com/watch?v=yofFVbARIRU I write the code exactly as he does but I can't get it to work. I cant get the BufferedReader code to work even though I imported it with import java.io.*; InputStreamReader stream = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader (stream); I solved it with this code: InputStreamReader stream = new InputStreamReader(System.in); java.io.BufferedReader reader =

BufferedReader: read multiple lines into a single string

痞子三分冷 提交于 2019-12-13 11:36:15
问题 I'm reading numbers from a txt file using BufferedReader for analysis. The way I'm going about this now is- reading a line using .readline, splitting this string into an array of strings using .split public InputFile () { fileIn = null; //stuff here fileIn = new FileReader((filename + ".txt")); buffIn = new BufferedReader(fileIn); return; //stuff here } public String ReadBigStringIn() { String line = null; try { line = buffIn.readLine(); } catch(IOException e){}; return line; } public