top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the Difference between byte stream and Character streams?

0 votes
1,100 views
What is the Difference between byte stream and Character streams?
posted Mar 17, 2016 by Karthick.c

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
 
Best answer

Byte Streams
A byte stream access the file byte by byte. Java programs use byte streams to perform input and output of 8-bit bytes. It is suitable for any kind of file, however not quite appropriate for text files. For example, if the file is using a unicode encoding and a character is represented with two bytes, the byte stream will treat these separately and you will need to do the conversion yourself. Byte oriented streams do not use any encoding scheme while Character oriented streams use character encoding scheme(UNICODE). All byte stream classes are descended from InputStream and OutputStream .

import java.io.*;
public class TestClass{
  public static void main(String[] args) {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
      fis = new FileInputStream("in.txt");
      fos = new FileOutputStream ("out.txt");
      int temp;
      while ((temp = fis.read()) != -1) //read byte by byte
      fos.write((byte)temp);        //write byte by byte
      if (fis != null)
      fis.close();
      if (fos != null)
      fos.close();
    }catch(Exception e){
      System.out.println(e);
    }
  }
}

Character Streams
A character stream will read a file character by character. Character Stream is a higher level concept than Byte Stream . A Character Stream is, effectively, a Byte Stream that has been wrapped with logic that allows it to output characters from a specific encoding. That means, a character stream needs to be given the file's encoding in order to work properly. Character stream can support all types of character sets ASCII, Unicode, UTF-8, UTF-16 etc. All character stream classes are descended from Reader and Writer .

Example

import java.io.*;
public class TestClass{
  public static void main(String[] args) {
    FileReader reader = null;
    try {
      reader = new FileReader("in.txt");
      int fChar;
      while ((fChar = reader.read()) != -1) //read char by char
        System.out.println((char)fChar);          //write char by char
    }catch(Exception e){
      System.out.println(e);
    }
  }
}

Credit: http://net-informations.com/java/cjava/stream.htm

Ling

answer Jun 13, 2017 by Rahul Kumar
0 votes

A stream is a way of sequentially accessing a file. A byte stream access the file byte by byte. A byte stream is suitable for any kind of file, however not quite appropriate for text files. For example, if the file is using a unicode encoding and a character is represented with two bytes, the byte stream will treat these separately and you will need to do the conversion yourself.

A character stream will read a file character by character. A character stream needs to be given the file's encoding in order to work properly.

Although a Microsoft Word Document contains text, it can't be accessed with a character stream (it isn't a text file). You need to use a byte stream to access it.

answer Mar 17, 2016 by Rajan Paswan
Similar Questions
+1 vote

DataStream classes such as DataInputStream and DataOutputStream class. why DataStream classes needed for input data. while we have others stream classes in java through which we can input the data.

0 votes

Explain the main difference between List and Stream in Scala Collection API? How do we prove that difference? When do we choose Stream?

...