New Post has been published on heapload
New Post has been published on http://heapload.com/computer-science/io-in-java
IO in Java
IO in Java
Java has very optimized and efficient packages for IO (Input and Output) operations. Java uses concept of stream (packages of data/information) for input and output tasks that are effective and supports many data types.
For reading data/information, Java invokes InputStream, and for writing, it uses OutputStream objects.
Code Examples:
Byte Streams
Java byte stream is one the most common approaches for Input and Output operations. This method converts information in the form of 8-bit bytes packages and then transfer them to the destination.
package java.examples; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * * @author heapload.com */ public class byteStreamExample public static void main(String args[]) throws IOException FileInputStream input = null; FileOutputStream output = null; try input = new FileInputStream("a.txt"); output = new FileOutputStream("b.txt"); int counter; while ((counter = input.read()) != -1) output.write(counter); finally if (input != null) input.close(); if (output != null) output.close();
java-byte-stream
Procedure:
Create a new class and name it as byteStreamExample.java
Make two objects for FileInputStream and FileOutputStream with file name.
Iterate the FileInputStream object until it returns -1 as end of file.
Write all the stream information into FileOutputStream object.
Finally, close both the streaming objects.
Character Streams
Java character stream supports 16-bit (twice than byte streams) Unicode streams. This method converts information in the form of 16-bit Unicode packages and then transfer them to the destination. Like java byte streams, it has FileReader and FileWriter classes for IO purpose.
java-byte-stream-and-character-stream-comparision
import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; /** * * @author heapload.com */ public class characterStreamExample public static void main(String args[]) throws IOException FileReader in = null; FileWriter out = null; try in = new FileReader("a.txt"); out = new FileWriter("b.txt"); int counter; while ((counter = in.read()) != -1) out.write(counter); // 16-bit unicode stream (twice than byte stream) finally if (in != null) in.close(); if (out != null) out.close();
java-character-stream
Procedure:
Create a new class and name it as characterStreamExample.java
Make two objects for FileReader and FileWriter with file name.
Iterate the FileReader object until it returns -1 as end of file.
Write all the stream information into FileWriter object.
Finally, close both the streaming objects.
Read Complete File Once
If the file is small, then you can also avoid iterating InputStream object with this simple approach.
import java.io.File; import java.io.FileInputStream; /** * * @author heapload.com */ public class readFileOnce public static void main(String args[]) try File file = new File("a.txt"); FileInputStream fis = new FileInputStream(file); byte[] data = new byte[(int) file.length()]; fis.read(data); fis.close(); String str = new String(data, "UTF-8"); catch (Exception ex) System.out.println("Error In File Reading" + ex.toString());
java-read-file-once
Procedure:
Create a new class and name it as readFileOnce.java
Make two objects for File and FileInputStream.
Make a byte array with the size of the source file.
Write all the stream information once into FileInputStream object.
Finally, close FileInputStream object.














