java.io packager provides many data such as primitives, object, localized characters, etc.
Stream
Sequence of data is called stream. There are two types of Streams:
- InPutStream: The InputStream is used to read data from a source.
- OutPutStream: The OutputStream is used for writing data to a destination.
Byte Streams
It uses byte streams to perform input and output of 8-bit bytes. Byte streams are very helpful it related to many classes but some classes are more used FileInputStream and FileOutputStream.
Example:
import java.io.*; public class CopyFile { public static void main(String args[]) throws IOException { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("input.txt"); out = new FileOutputStream("output.txt"); int c; while ((c = in.read()) != -1) { out.write(c); } }finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } }
Character Streams
Java Character streams are used to perform input and output for 16-bit unicode. Character streams are very helpful it related to many class but some class are more used FileReader and FileWriter. Generally FileReader uses FileInputStream and FileWriter uses FileOutputStream.
import java.io.*; public class CopyFile { public static void main(String args[]) throws IOException { FileReader in = null; FileWriter out = null; try { in = new FileReader("input.txt"); out = new FileWriter("output.txt"); int c; while ((c = in.read()) != -1) { out.write(c); } }finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } }
Here input.txt with the following content:
this is copy file
Standard Streams
Other programming language like c++ have STDIN, STDOUT and STDERR. but Java have the following three standard streams:
- Standard Input It’s represented as System.in and keyboard is used as standard input stream.
- Standard Output It’s represented as System.out and computer screen is used for standard output stream.
- Standard Error It’s represented as System.err and computer screen is used for standard error stream.
Example:
import java.io.*; public class ReadConsole { public static void main(String args[]) throws IOException { InputStreamReader cin = null; try { cin = new InputStreamReader(System.in); System.out.println("Enter number, 'e' to escape."); char c; do { c = (char) cin.read(); System.out.print(c); } while(c != 'e'); }finally { if (cin != null) { cin.close(); } } } }
we program continues to read and output the same character until we press ‘e’:
$ javac ReadConsole.java $ java ReadConsole Enter characters, 'e' to escape. 1 1 e e q q
Reading and Writing Files
The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination. The two types of streams are FileInputStream and FileOutputStream.
FileInputStream
Objects can be created using the keyword new and there are several types of constructors available.
InputStream f = new FileInputStream("C:/java/world");
Following constructor takes a file object to create an input stream object to read the file:
File f = new File("C:/java/world"); InputStream f = new FileInputStream(f);
Many list of method can be used to read to stream or to do other operations on the stream.
- public void close() throws IOException{} – This method used to closes the file output stream. Releases any system resources associated with the file. Throws an IOException.
- protected void finalize()throws IOException {} – This method used to cleans up the connection to the file. Ensures that the close method of this file output stream is called when there are no more references to this stream. Throws an IOException.
- public int read(int r)throws IOException{} – This method used to reads the specified byte of data from the InputStream. Returns an int. Returns the next byte of data and -1 will be returned if it’s the end of the file.
FileOutputStream
FileOutputStream is used to create a file and write data into it.
OutputStream f = new FileOutputStream("C:/java/WORLD")
Following constructor takes a file object to create an output stream object to write the file.
File f = new File("C:/java/WORLD"); OutputStream f = new FileOutputStream(f);
- public void close() throws IOException{} – This method used to closes the file output stream. Releases any system resources associated with the file. Throws an IOException.
- protected void finalize()throws IOException {} – This method used to cleans up the connection to the file. Ensures that the close method of this file output stream is called when there are no more references to this stream. Throws an IOException.
- public void write(int w)throws IOException{} – This methods used to writes the specified byte to the output stream.
import java.io.*; public class fileStreamTest { public static void main(String args[]) { try { byte bWrite [] = {10,9,8,25,14}; OutputStream os = new FileOutputStream("test.txt"); for(int x = 0; x < bWrite.length ; x++) { os.write( bWrite[x] ); // writes the bytes } os.close(); InputStream is = new FileInputStream("test.txt"); int size = is.available(); for(int i = 0; i < size; i++) { System.out.print((char)is.read() + " "); } is.close(); } catch (IOException e) { System.out.print("Exception"); } } }
Listing Directories
list( ) method is mainly provided by File object to list down all the files and directories available in a directory as follows:
import java.io.File; public class ReadDir { public static void main(String[] args) { File file = null; String[] paths; try { // create new file object file = new File("/local"); // array of files and directory paths = file.list(); // for each name in the path array for(String path:paths) { // prints filename and directory name System.out.println(path); } } catch (Exception e) { // if any error occurs e.printStackTrace(); } } }
This will produce the following result based on the directories and files available in your /local directory:
Output
test1.txt test2.txt ReadDir.java ReadDir.class