BufferedReader.readLine() vs FileReader.read(charArray) performance [duplicate] - java

This question already has answers here:
Why does the buffered writer does not write immediately when the write() method is called
(1 answer)
What is buffer? What are buffered reads and writes?
(1 answer)
Closed 4 years ago.
BufferedReader.readLine() method vs FileReader.read(charArray) , if we pass a big size for charArray, performance of FileReader improves exponentially and looks like we can achieve what BufferedReader does, so why do we have BufferedReader and not just use FileReader with a big char array ?
How come BufferedReader is more efficient than FileReader when its just a decorator around FileReader (or any other Reader implementation) and depends upon FileReader(Reader instance) to read data from disk File ?
Does BufferedReader reduces number of I/O journeys to read data from disk as compared to FileReader ?

Because it is buffered. That cuts down the number of system calls by a factor of thousands.

Related

Java OutputStream to Multiple files [duplicate]

This question already has answers here:
How to write data to two java.io.OutputStream objects at once?
(5 answers)
Closed 8 years ago.
I have an OutputStream, and I'd like to (on a conceptual level) broadcast it to multiple files. So for instance, if a byte shows up in the stream, I want that to get written to files A, B, and C.
How can I accomplish this using only one stream? Preferably with a pure Java solution.
You can use Apache Commons IO TeeOutputStream for this purpose.
This OutputStream proxies all bytes written to it to two underlying OutputStreams.
You can use multiple TeeOutputStreams in a chain when you want to write to more than two OutputStreams at once.
OutputStream out = new TeeOutputStream(new FileOutputStream(new File("A")), new TeeOutputStream(new FileOutputStream(new File("B")), new FileOutputStream(new File("C")))))

Why does my thread is freeze if I try to get the ObjectInputStream instead of InputStream in Java? [duplicate]

This question already has answers here:
Java Creating a new ObjectInputStream Blocks
(3 answers)
Closed 8 years ago.
It is about this line
VoIP_InputStream = new ObjectInputStream(VoIP_socket.getInputStream());
If I execute it in debug mode I see that the thread that is running it doesn't go on. It does just freeze at this line and exit the debugger without going on to the next line.
if I try this line instead (without ObjectInputStream)
VoIP_InputStream = VoIP_socket.getInputStream();
If runs normally through. Why does it freeze if I use the ObjectInputStream and how can I fix this?
Because, as it says in the Javadoc, the constructor of ObjectInputStream reads the stream header written by the constructor of ObjectOutputStream at the peer. You need to construct the ObjectOutputStream before the ObjectInputStream, at both ends preferably.

Reading serialized file - java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I am having problems reading from a serialized file.
More specifically, I have serialized an object to a file written in a hexadecimal format. The problem occurs when I want to read one line at a time from this file. For example, the file can look like this:
aced 0005 7372 0005 5465 7374 41f2 13c1
215c 9734 6b02 0000 7870
However, the code underneath reads the whole file (instead of just the first line). Also, it automatically converts the hexadecimal data into something more readable: ¬ísrTestAòÁ
....
try (BufferedReader file = new BufferedReader(new FileReader(fileName))) {
read(file);
} catch ...
....
public static void read(BufferedReader in) throws IOException{
String line = in.readLine();
System.out.println(line); // PROBLEM: This prints every line
}
}
This code works perfectly fine if I have a normal text file with some random words, it only prints the first line. My guess is the problems lies in the serialization format. I read somewhere (probably the API) that the file is supposed to be in binary (even though my file is in hexadecimal??).
What should I do to be able to read one line at a time from this file?
EDIT: I have gotten quite a few of answers, which I am thankful for. I never wanted to deserialize the object - only be able to read every hexadecimal line (one at a time) so I could analyze the serialized object. I am sorry if the question was unclear.
Now I have realized that the file is actually not written in hexadecimal but in binary. Further, it is not even devided into lines. The problem I am facing now is to read every byte and convert it into hexadecimal. Basically, I want the data to look like the hexadecimal data above.
UPDATE:
immibis comments helped me solve this.
"Use FileInputStream (or a BufferedInputStream wrapping one) and call read() repeatedly - each call returns one byte (from 0 to 255) or -1 if there are no more bytes in the file. This is the simplest, but not the most efficient, way (reading an array is usually faster)"
The file does not contain hexadecimal text and is not separated into lines.
Whatever program you are using to edit the file is "helpfully" converting it into hexadecimal for you, since it would be gibberish if displayed directly.
If you are writing the file using ObjectOutputStream and FileOutputStream, then you need to read it using ObjectInputStream and FileInputStream.
Your question doesn't make any sense. Serialized data is binary. It doesn't contain lines. You can't read lines from it. You should either read bytes, with an InputStream, or objects, with an ObjectInputStream.

Efficient Way to Read a File Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What is the most efficient way to read input from a file ?
I have a very very large file which contains a list of words separated by a newline
e.g
computer
science
is
fun
really
I was thinking about using a BufferedReader object however I was confused by this line in the documentation.
"In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,
BufferedReader in = new BufferedReader(new FileReader("foo.in"));
will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient. " <
Can some please explain this to me?
On second read I starting to believe the BufferedReader is my best bet. Is there a better way?
This post may help.
BufferedReader is a good choice, letting you turn a BufferedReader into a java.util.Stream in Java 8.
Parsing a large CSV file for instance with java.util.stream package:
InputStream is = new FileInputStream(new File("persons.csv"));
BufferedReader br = new BufferedReader(new InputStreamReader(is));
List<Person> persons = br.lines()
.substream(1)
.map(mapToPerson)
.filter(person -> person.getAge() > 17)
.limit(50)
.collect(toList());
Unlike Collections which are in-memory data structures which hold elements within it , Streams allow parallel processing and behave like fixed data structures which computes the elements on-demand basis.
Moreover, Streams also support Pipelining and Internal Iterations.

Output stream over a StringBuilder [duplicate]

This question already has answers here:
Get an OutputStream into a String
(6 answers)
Closed 9 years ago.
I think this has been answered but I can't seem to find it.
I have an instance method which writes some contents to an output stream
writeTo(OutputStream){
//class specific logic
}
I want it to get these contents into a StringBuilder. I can do this via a temporary file but that does not seem right. I want to do something like:
Stringbuilder sb = /* */;
OutputStream os = outForStringBuilder(sb);//not sure how to do this
instance.writeTo(os); //This should write the contents to Stringbuilder
Use a ByteArrayOutputStream and then call toString(charSet) - no need for a StringBuilder.
So you are wanting output written to the stream to go to a StringBuffer instead. I am assuming you are doing this because an OutputStream is required somewhere else. You could use ByteArrayOutputStream, but if you want to preserve the StringBuffer behavior, you might simply wrap a StringBuffer in a subclass of OutputStream like the code here:
http://geronimo.apache.org/maven/specs/geronimo-javamail_1.4_spec/1.6/apidocs/src-html/org/apache/geronimo/mail/util/StringBufferOutputStream.html#line.31

Categories

Resources