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

Sunday, 17 November 2013

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

In our previous discussion we came across, the foundation of Java I/O and a little deep about InputStreams and OutputStreams. In this particular blog we will see Byte Stream and how to Read and Write a file in Java using ‘Byte Streams’

Byte Streams in Java

As we already know, streams help us to sequentially accessing a file or data. A byte stream accesses a file ‘byte by byte’ that is suitable for any type of data but in some cases it’s not recommended to use byte stream. For example, if the file is using a unicode encoding and a character in the file is represented with two bytes, the byte stream will treat these separately and we will need to do the conversion our-self.

Does ‘Byte Streams’ support Unicode characters ?

Yes, byte streams supports bytes thus supports anything that can be represented using bytes..i.e. text, photos, videos ..etc. The thing to be considered here is that everything that is to be manipulated using byte streams needs to follow some rules of coding and decoding to a combination of 0 and 1’s.

Reading and Writing a file in Java using ‘Byte Streams’

Java provides a number of byte streams classes, all these classes are decedents from either ‘InputStream’ or ‘OutputStream’. In the example below we have used ‘FileInputStream’ and ‘FileOutputStream’ class. All other classes provided for Byte Streams are much similar to these two classes in their usage.
package com.beingjavaguys.core;

 * @author Nagesh Chauhan
public class ByteStreamDemo {

 public static void main(String args[]) throws IOException {
  FileInputStream fileInputStream = null;
  FileOutputStream fileOutputStream = null;

  try {
   int data;
   fileInputStream = new FileInputStream("files/source.txt");
   fileOutputStream = new FileOutputStream("files/destination.txt");
   while ((data = != -1) {
    System.out.print(" " + data);
  } catch (FileNotFoundException e) {
  } finally {
   if (fileInputStream != null)
   if (fileOutputStream != null)

We have used read() and write() functions here, read() returns a integer value equals to next byte in the file and -1 if no more byte is left to read. The write() methods accepts a byte as parameter to write and returns a void.

Byte stream reads and write a byte at a time, this needs a lower level of machine resources. Thus we must avoid the usage of byte stream while dealing with more sophisticated data. In that case Character Stream and other available streams should be used accordingly.

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

Thanks for reading !
Being Java Guys Team


Post a Comment

Like Us on Facebook

Like Us On Google+


Skype: neel4soft