This document discusses input/output streams and files in Java. It describes input streams, which read data from sources like keyboards or files, and output streams, which write data to destinations like monitors or files. It then focuses on reading and writing files using FileInputStream, FileOutputStream, FileReader and FileWriter classes. Constructors and methods of each class are listed, with examples provided of reading and writing files byte-by-byte and character-by-character. The key difference between byte and character streams is also summarized.
2. I/O Stream
Input Stream: It is a stream which may be
connected with different i/p devices like
keyboard, file or network socket. It is used to
generate data.
Output Stream: It is used to produce data and
may be connected with different output devices
like monitor, file, network socket.
3. Files and Streams
File processing with classes in package java.io
FileInputStream for
byte-by-byte input from a file
FileOutputStream for
byte-by-byte output to a file
FileReader for char-by-char
input from a file
FileWriter for char-by-char
output to a file
4. Difference
byte Stream char Stream
1. It deals with byte.
2. Used to manipulate
data in binary
format.
3. Mainly uses audio,
video, img, txt files.
4. InputStream and
OutputStream are
top two abstract
classes of byte
Stream.
1. It deals with char or
Strings.
2. Used to manipulate
data in Unicode.
3. Can be used only
with txt files.
4. Reader & Writer are
top two abstract
classes of char
Stream.
6. Constructors, Methods Of
FileInputStream
CONSTRUCTORS:
FileInputStream(String fname);
FileInputStream(File F);
FileInputStream(String fold, String fname);
METHODS:
int read(); // n= fis.read(); // n is the byte read.
7. Constructors, Methods Of
FileOutputStream
CONSTRUCTORS:
FileOutputStream(String fname);
FileOutputStream(File F);
FileOutputStream(String fname, boolean app);
METHODS:
void write(int n); //write(n); //n is the byte to be
written
void write(byte []b); // writes the array contents to
file.
8. Example: FileInputStream
import java.io.*;
public class ReadStringFromFile {
public static void main(String[] args)
{
File file = new File("C://FileIO//ReadString.txt");
int ch;
StringBuffer strContent = new StringBuffer("");
FileInputStream fin = null;
try
{
fin = new FileInputStream(file);
13. Constructors, Methods Of FileReader
CONSTRUCTORS:
FileReader(String fname);
FileReader(File F);
FileReader(String fold, String fname);
METHODS:
int read(); // n= fis.read(); // n is the char read.
int read(char [] c, int offset, int len); // reads
character into an array and returns the number of
chars read.
14. Constructors, Methods Of FileWriter
CONSTRUCTORS:
FileWriter(String fname);
FileWriter(File F);
FileWriter(String fold, String fname);
METHODS:
void write(String n); // write(n); //n is the String to be
written to file.
public void write(char [] c, int offset, int len); // Writes a
portion of an array of characters starting from offset
and with a length of len.
public void write(String s, int offset, int len);
15. Example: FileReader
import java.io.*;
public class FileRead {
public static void main(String args[]) throws
IOException {
File file = new File("Hello1.txt");
file.createNewFile(); //returns true if file is created
FileWriter fw = new FileWriter(file);
fw.write("Thisn isn ann examplen");
fw.flush();
fw.close();
16. Continue...
FileReader fr = new FileReader(file);
char [] a = new char[50];
fr.read(a); //reads content to array
for(char c : a)
System.out.print(c);
fr.close(); }
}
17. Example: FileReader using
BufferedReader
import java.io.*;
public class Reader {
public static void main(String[]args) throws
IOException{
FileReader fr = new FileReader("C:/test.txt");
BufferedReader br = new BufferedReader(in);
while (br.readLine() != null)
{ System.out.println(br.readLine()); }
fr.close();
}