File Input and Output
Java comes with many handy I/O classes to support the input and output through bytes stream and file system. Here a list of the Java I/O examples including file, temporary file and directory manipulation, encoding, serialized and also compression with zip or Gzip.
I/O Streams
A stream is a communication channel that a program has with the outside world. It is used to transfer data items in succession.
An Input/Output (I/O) Stream represents an input source or an output destination. A stream can represent many different kinds of sources and destinations, including disk files, devices, other programs, and memory arrays.
Streams support many different kinds of data, including simple bytes, primitive data types, localized characters, and objects. Some streams simply pass on data; others manipulate and transform the data in useful ways.
No matter how they work internally, all streams present the same simple model to programs that use them: A stream is a sequence of data.
Reading information into a program.
A program uses an input stream to read data from a source, one item at a time:
javac
Writing information from a program.
A program uses an output stream to write data to a destination, one item at time:
javac
The data source and data destination pictured above can be anything that holds, generates, or consumes data. Obviously this includes disk files, but a source or destination can also another program, a peripheral device, a network socket, or an array.
Ads by Google


The Java IO API
The java.io package contains many classes that your programs can use to read and write data. Most of the classes implement sequential access streams. The sequential access streams can be divided into two groups: those that read and write bytes and those that read and write Unicode characters. Each sequential access stream has a speciality, such as reading from or writing to a file, filtering data as its read or written, or serializing an object.

Reading and Writing a File
Example:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class MyFile {

public static void main(String[] args) {
MyFile myFile = new MyFile();
String input = myFile.readTextFile("testing1.txt");
System.out.println(input);
myFile.writeTextFile("Testing2.txt", input);
}

public String readTextFile(String fileName) {
String returnValue = "";
FileReader file = null;
String line = "";
try {
file = new FileReader(fileName);
BufferedReader reader = new BufferedReader(file);
while ((line = reader.readLine()) != null) {
returnValue += line + "¥n";
}
} catch (FileNotFoundException e) {
throw new RuntimeException("File not found");
} catch (IOException e) {
throw new RuntimeException("IO Error occured");
} finally {
if (file != null) {
try {
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return returnValue;
}

public void writeTextFile(String fileName, String s) {
FileWriter output = null;
try {
output = new FileWriter(fileName);
BufferedWriter writer = new BufferedWriter(output);
writer.write(s);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
}

Previous - Java Collections                                                          Next - Java Exceptions