Java Streams Tutorial – Input Output Files in Java

Java Streams Tutorial – Input Output Files in Java

Java Streams Tutorial

In file processing, Input means the flow of data into a program and output means the flow of data out of the program. Input to the program may come to various sources such as

  • Keyboard
  • Mouse
  • Microphone
  • Disk
  • Network, etc.

The output may go to the following destinations

  • Screen
  • Printer
  • Disk
  • Speaker
  • Network, etc.

 

Java uses the concept of streams to represent the flow of data from input devices to program and from program to output devices.

The concept of sending data from one stream to another has made java stream a powerful tool for file processing. Following java packages and classes are used to implement java input and output streams.

Java Streams Classes

Java.io package contains various java stream classes to process data in java. These classes are categorized into two groups based on the data type on which java files operate-

  • Byte stream classes
  • Character stream classes

 

Byte stream classes in java

Byte stream classes are designed to provide the functions and features to read and write data in the form of bytes.

Byte streams are unidirectional therefore they can transmit bytes only in one direction. Java provides two kinds of byte stream classes:-

  • Input stream classes
  • Output stream classes

 

Input stream classes

Input stream classes are used to read 8 bits data and transmit data into a program

 

Output stream classes

Output stream classes are used to write 8 bits of data received from a program and send it to various output destinations sources.

 

Character stream classes

Character stream classes are used to read and write 16 bits of Unicode characters. Character stream classes are of two types-

  • Reader stream classes
  • Writer stream classes

 

Reader stream classes

Reader stream classes are designed to read characters from an existed file.

 

Writer stream classes

Output stream classes contain functions that allow the features to write data into a new file.

 

Other useful Input/Output Classes in Java Streams

Java.io package supports some more useful classes to perform more functions. These functions are present in the following classes-

  • RandomAccessFile
  • StreamTokenizer

 

RandomAccessFile

The RandomAccessFile allows reading and writing bytes, text and java data types to and from any location.

StreamTokenizer

The StreamTokenizer class is a subclass of Object class. It allows breaking up a stream of text from an input text file into meaningful pieces called tokens.

 

File Class in Java – Java Streams Tutorial

The java.io package includes a class – File class. File class provides supports for creating files and folders.

File class contains several constructors for instantiating the File objects.

File class also contains methods to do the following operations-

  1. Creating a file
  2. Opening a file
  3. Closing a file
  4. Deleting a file
  5. Getting the name of a file
  6. Getting the size of a file
  7. Checking the presence of a file
  8. Renaming a file
  9. To check whether a file is readable
  10. To check whether a file is writable

 

Creating a Java File – Java Streams Tutorial 1

//Following program copy characters from one file to another file

Import java.io.*;

Class CopyChar

{

public static void main (String args[])

{

File iFile = new File (“indata.txt”);

File oFile = new File (“outdata.txt”);

FileReader ins=null;

FielWriter outs=null;

Try

{

ins=new fileReader(iFile);

outs=new fileWriter(oFile);

char ch;

while ((ch = ins.read())!= -1)

{

outs.write(ch);

}

}

catch(IOException e)

{

System.out.println(e);

System.exit(-1);

}

Finally

{

Try

{

ins.close();

outs.close();

}

Catch(IOException e){ }

}

}

}

 

Compile and run the program CopyChar.java

Javac CopyChar.java

Java CopyChar

Now the program will copy the characters of “indata.txt” into outdata.txt.

 

 

Writing bytes to a file – Java Streams Tutorial 2

The following program writes cities names – MEERUT and DELHI into a text file-cityfile.txt.

// Writing bytes to a file

Import java.io.*;

class WritingBytes

{

public static void main (String args[])

{

byte cities []={‘M’,’E’,’E’,’R’,’U’,’T’,’\n’,’D’,’E’,’L’,’H’,’I’};

FileOutputStream outfile = null;

Try

{

outfile = new FileOutputStream(“cityfile.txt”);

outfile.write(cities);

outfile.close();

}

catch(IOException e)

{

System.out.println(e);

}

}

}

 

Compile and run the program WritingBytes.java

Javac WritingBytes.java

Java WritingBytes

Now to see the output Type cityfile.txt, and you will see the following output-

MEERUT

DELHI

 

Reading bytes from a file – Java Streams Tutorial 3

Following program reads the cities written in the file cityfile.txt and display on the screen.

// Reading cities from the file and displaying on the screen

Import java.io.*;

Class ReadingBytes

{

Public static void main(String args[])

{

FileInputStream infile=null;

Int b;

try

{

Infile = new FileInputStream(args[0]);

While (b=infile.read() ) != -1)

{

System.out.println((char) b);

}

Infile.close();

}

Catch (IOException e)

{

System.out.println(e);

}

}

}

Compile and run the program ReadingBytes.java

Javac ReadingBytes.java

Java ReadingBytes

Output-

MEERUT

DELHI

 

Copying bytes from one file to another file

// Copy bytes from one file to another file

Import java.io.*;

Class copyBytes

{

Public static void main(String args[])

{

FileInputStream infile = null;

FileOutputStream outfile=null;

byte byteread

try

{

Infile = new FileInputStream (“indata.txt”);

Outfile = new FileInputStream (“outdata.txt);

Do

{

byteread =(byte) infile.read();

outfile write (byteread);

}

While (byteread != -1);

}

catch(FileNotFoundException e)

{

System.out.println(“File not foun”);

}

Catch(IOException e)

{

System.out.println(e.getMessage());

}

Finally

{

Try

{

infile.close();

outfile.close();

}

Catch (IOException e){}

}

}

}

Compile and run the program

 

Practice Questions

Q1. What is the file? Why we use files?

Q2. What is the stream? How the concept of the stream used in java

Q3. Write a program to write the names of students of your class into a file – name.txt.

Q4. Read the file you created in the Q3. and display the names of the students on the screen.

 

 

Thanks for reading, Welcome to your comments on this Post

This site uses Akismet to reduce spam. Learn how your comment data is processed.