Sunday, April 17, 2011

Understanding Streams in Java


Understanding Streams in Java

PART I

By: Mohammed Saudi.

Download examples :

A Data Stream is a flow of data between data source and data sink (the data goes to it).
Data source or sink could be for example disk file, device or network socket.

Two types of streams available in java ; Byte streams and Character streams. They differ in the level they are dealing with data; either data is read/written in 8-bit fashion ( byte stream) or in 16-bit unicode character fashion (character based).

If you are not working with images or sounds, it would be easier and better to use character-based streams; they handle the conversion and encoding stuff.

There is also a helpful classes ( java.io.InputStreamReader and java.io.OutputStreamWriter) that handle the gap between those two types. They convert byte data into character-based.

Also there is a Data stream classes that reads data directly by type (int, string, float ,... ). They relay on a stream objects.

DataInputStream and DataOutputStream. are typical examples.
DataOutputStream(OutputStream out)
DataInputStream(InputStream in)

Examples of Byte Stream classes are
InputStreamReader, OutputStreamWriter, BufferedReader and BufferedWriter.

Examples of character Stream classes ( data sink streams) are
FileReader and FileWriter




===== Reading Files using byte-based streams ====

/*
This Program reads "myInputFile.txt" file byte by byte and write the data into the file "myOutputFile.txt"
you should have myInputFile.txt in the same path of this java file

By: Mohammed Saudi.
www.visiontutoring.net

*/
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ReadBytes {
public static void main(String[] args) throws IOException {
FileInputStream inStream = null;
FileOutputStream outStream = null;
try {
inStream = new FileInputStream("myInputFile.txt");
outStream = new FileOutputStream("myOutputFile.txt");
int c; //I use int here because I use the read function blow which returns an integer.

while ((c = inStream.read()) != -1) {
outStream.write(c);
}

}

catch (Exception fe){

System.out.println(fe.getMessage());
}

finally {
if (inStream != null) {
inStream.close();
}
if (outStream != null) {
outStream.close();
}
}
}
}

===== Reading Files using character-based streams ====

/*
This Program is the same as "ReadBytes" except it uses filereader and filewritter classes
you should have myInputFile.txt in the same path of this java file

* note: you may have a problem in character encoding when using filewritter class.
If so, use the OutputStreamWriter on a FileOutputStream instead.

By: Mohammed Saudi.
www.visiontutoring.net

*/

import java.io.*;


// Displays contents of a file
//(e.g. java Type app.ini)
public class ReadChars
{
public static void main(
String args[]) throws Exception
{
// Open input/output and setup variables
FileReader fr = new FileReader("myInputFile.txt");
FileWriter fw = new FileWriter("myOutputFile.txt");

char cToReadIn[] = new char[1000];
int read = 0;

// Read (and print) till end of file
while ((read = fr.read(cToReadIn)) != -1)
{
fw.write(cToReadIn);
}


// Close shop
fr.close();
fw.close();
}
}

Download examples :


msaudi.blogspot.com

No comments: