0

What is the difference between using byte stream and character stream when we can use either of them interchangeably in reading/writing text file (.txt) and character file(.doc,.pdf) along with some manipulation to get the desired results ?

2 Answers2

0

Related answer on Stackoverflow

TL;DR version: A byte stream reads a file in fixed unit of 8 bits (1 byte). A character file reads the file based on the encoding specified when you open the file.

Community
  • 1
  • 1
Seth
  • 351
  • 1
  • 3
  • 8
  • -basically my confusion is , that if i have 2 files one is .txt(byte file) and other is .doc (character file like word document, well i suppose) file and i have to perform both read/write operations on both the files; then what mechanism(type of streams i.e. reader/writer or inputtream/outputstream) should i use for each of the file. Please help , i have already got less marks in 1 exam. – GARIMA JAIN May 04 '17 at 09:28
-1

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.

Manu
  • 17
  • 5
  • @Sreenath-basically my confusion is , that if i have 2 files one is .txt(byte file) and other is .doc (character file like word document, well i suppose) file and i have to perform both read/write operations on both the files; then what mechanism(type of streams i.e. reader/writer or inputtream/outputstream) should i use for each of the file. Please help , i have already got less marks in 1 exam. – GARIMA JAIN May 04 '17 at 09:31
  • Do both actions seperately, Use FileReader/FileWriter in the case of txt file and FileInputStream/FileOutputStream in doc file case. – Manu May 04 '17 at 09:42
  • @Sreenath- which means that .doc files are byte files and .txt files are character files. just tell me if i am correct ??? – GARIMA JAIN May 04 '17 at 11:24
  • In general every file is a binary file, but if the data in it contains only text (letter, numbers and other symbols one would use in writing, and if it consists of lines, then we consider it a text file(Character file). A Microsoft word file(.doc) is a binary file(Byte file) as besides the actual text, it also contains various characters representing font size and color. – Manu May 04 '17 at 11:48