0

I am doing a project in which there are so many files I have to handle. Problem came when I have to provide file in different manner like:

  1. File will contain one string in each line
  2. numbers of char in each line e.g. :

    1st line :  A B 4
    
    2nd line :  6 C A 6 & U #
    
    etc.
    
  3. File will contain no. of Strings e.g.

    1st line : Lion Panther jaguar etc.

I have read how to efficiently handle file but I am so confused when to use Buffered Streams and when Unbuffered. If I am using BufferedStream then BufferInputStream and BufferReader / BufferWriter which should be used.

Similarly I am confuse with I/O Stream, File I/O Stream, ByteArray I/O Stream. There are so many things. Can any one suggest me when to use which one and why? What could be efficient handling according to the different scenarios?

user2672165
  • 2,986
  • 19
  • 27
  • 1
    Have you read their javadoc to understand what they used for? Have you read the IO tutorial? The first two sections would at least have told you that streams are for bytes, and readers/writers are for characters. http://docs.oracle.com/javase/tutorial/essential/io/ – JB Nizet Mar 30 '14 at 07:46
  • Yeah I have read it and I am clear with byte and characters concept and also with the system calls. Problem is only when to use when for efficient handling. – Naushad Ahmad Mar 30 '14 at 19:08
  • and one more problem is how to read one string at a time from file if we have file like lion panther jaguar now i want to read first lion then panther and then jaguar to perform different operation on those data. can anyone help me ? It is similar as we get in any programming competition input file that contain data in same manner. – Naushad Ahmad Mar 30 '14 at 19:12

1 Answers1

1

Well, there might not be a direct answer for this, but you don't have to worry if you feel confused. Discussions about Buffered and Unbufferred have been done many times before.

For example in this link: bufferred vs non-bufferred, gives a good hint (check the answer marked as correct). This comes because while using Bufferred streams, those streams are stored in a small area of memory called (suprisingly) buffer. Same happens to written data (they go into the buffer before being stored into the hard memory). This improves performance because lowers the overhead of I/O operations (which are OS dependent). Check the Java Doc: Bufferred Streams

So, to make it clear, use Bufferred streams when you need to improve the performance of your I/O operations. Use Unbufferred streams when you want to ensure that the output has been written before continuing (because an error might always occur while writing from/into the buffer, an example might be when you want to write a log, it might be opened all the time, so there is no need to access it, no need for a buffer ).

Community
  • 1
  • 1
Endrik
  • 2,238
  • 3
  • 19
  • 33