Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Sunday, 24 November 2013

Read and Write a file in Java using Buffered Streams - Java IO (Buffered Streams)

In our previous discussions we came across ‘Byte Streams’ and ‘Character Streams’ in Java. In this particular blog we will dive into ‘Buffered Streams’. We will come to know how to read from and write to a file in java using ‘Bufferd Streams’ and why this is a good practice to use buffered streams over byte and character streams.


In case of ‘byte’ and ‘character’ streams every byte or piece of data that s being read or write requires an direct support from underlying OS because of not having an intermediate buffer included. This makes a extensive use of memory and resources. On the other hand Buffered streams works as a wrapper to hold unbuffered streams and make it possible to store bunch of data or bytes in buffers (memory). The underlying OS resource are needed only when the buffer is full or empty and not on every instance of read or write.

The native input API is called only when the buffer is empty. Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full. Unbuffered streams can be converted to buffered streams by wrapping them in a constructor of Bufferd Stream class as shown below.


Convert ‘byte Stream’ to ‘Buffered byte Stream’

Read and Write file using ‘BufferedInputStream’ and

‘BufferedOutputStream’ classes. ‘BufferedInputStream’ and BufferedOutputStream’ clases are used to make buffered byte streams by wrapping InputStreamReader and OutputStreamWriter classes.
package com.beingjavaguys.core;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
 * @author Nagesh Chauhan
 * 
 */
public class BufferedIOStream {
 public static void main(String args[]) throws IOException {
  BufferedInputStream bis = null;
  BufferedOutputStream bos = null;

  try {
   bis = new BufferedInputStream(new FileInputStream(
     "files/source.txt"));
   bos = new BufferedOutputStream(new FileOutputStream(
     "files/destination.txt"));
   int data;
   while ((data = bis.read()) != -1) {
    System.out.println(data);
    bos.write(data);
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } finally {
   if (bis != null)
    bis.close();
   if (bos != null)
    bos.close();
  }
 }
}




Convert ‘character Stream’ to ‘Buffered character Stream’

Read and Write file using ‘BufferedReader’ and ‘BufferedWriter’ classes.

‘BufferedReader’ and BufferedWriter’ clases are used to make buffered character streams by wrapping ‘FileReader’ and ‘FileWriter’ classes.
package com.beingjavaguys.core;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
 * @author Nagesh Chauhan
 * 
 */
public class BufferedReadWriteStream {
 public static void main(String srgs[]) throws IOException {
  BufferedReader bufReader = null;
  BufferedWriter bufWriter = null;

  try {
   bufReader = new BufferedReader(new FileReader("files/source.txt"));
   bufWriter = new BufferedWriter(new FileWriter(
     "files/destination.txt"));
   String data;
   while ((data = bufReader.readLine()) != null) {
    System.out.println(data);
    bufWriter.write(data);
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } finally {
   if (bufReader != null)
    bufReader.close();
   if (bufWriter != null)
    bufWriter.close();
  }
 }
}



Here we are done with 'Read and Write a file in Java using 'Byte Buffered Streams' and 'Character Buffered Streams'. In our upcoming blogs we will see more about Java Programming and other opensource technologies.








Thanks for reading !
Being Java Guys Team




0 comments:

Post a Comment

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft