A stream is a device for transmitting or retrieving 8-bit or byte values. The emphasis is on the action of reading or writing as opposed to the data itself. A file is a collection of items stored on an external device. The Java object FileStream provides the means to access the data values but does not actually hold the file contents.
There are two independent and largely parallel systems involved in I/O. InputStream and OutputStream are used to read and write 8-bit quantities and process binary files. The alternative hierarchy has two different classes Reader and Writer that are used to read and write 16-bit Unicode character values and process text files.
There are classes tied to a physical input source. These read values from a byte array, a file, or a pipe (e.g. a FileInputStream requires either a file or a file name). There are also virtual input classes that rely on one or more underlying input streams for their data source. For example the class PushbackInputStream adds the ability to unread already processed characters, returning them once more in response to a subsequent read operation.
One type of filtering is buffering which is provided by several buffered streams including BufferedInputStream and BufferedReader for performing binary and text input. BufferedOutputStream and BufferedWriter for binary and text outputs. A buffer is a relatively large region of memory used to temporarily store data while it is being input or output.
StreamTokenizer breaks a textual file into a sequence of tokens. It recognizes words and numbers.
The File class provides methods for dealing with files or directories. File systems are organized into a hierarchy. A path is a description of a file's location in the hierarchy. When a program is running, the program' directory is considered the current directory. Any files located in the current directory can be referred to by name alone. The relative path is the location of a file with respect to the current directory. The absolute path starts at the root directory.
public class File extends Object implements Serializable { // Constructors public File ( String path ); public File ( String path, String name ); // Public methods public boolean canRead(); // is the file readable? public boolean canWrite(); // is the file writeable? public boolean delete(); // delete the file public boolean exists(); // does the file exist public long lastModified(); // when was the file last modified public long length(); // How many bytes does it contain public boolean renameTo(File f); // rename this file to f's name }
File inFile = new File ( "FileIO.txt" );
import java.io.*; public class ReadTextFile { public static void main (String [] args) throws IOException { File inFile = new File ("input.txt"); FileReader fReader = new FileReader (inFile); BufferedReader bReader = new BufferedReader (fReader); try { String line = bReader.readLine(); while (line != null) { System.out.println (line); line = bReader.readLine(); } } catch (Exception e) { System.out.println (e.getMessage()); e.printStackTrace(); System.exit(0); } finally { fReader.close(); } } }
A text file can also be read using a Scanner object. Using the Scanner offers the advantage of using the methods that come with the Scanner class.
import java.util.Scanner; import java.io.File; public class ReadTextFile { public static void main (String [] args) throws IOException { File inFile = new File ("input.txt"); Scanner sc = new Scanner (inFile); while (sc.hasNextLine()) { String line = sc.nextLine(); System.out.println (line); } sc.close(); } }
FileWriter fWriter = new FileWriter (outFile, true);The class PrintWriter has methods print(), printf() and println() that will allow us to write to a file.
import java.io.*; public class WriteTextFile { public static void main (String [] args) throws IOException { File outFile = new File ("output.txt"); FileWriter fWriter = new FileWriter (outFile); PrintWriter pWriter = new PrintWriter (fWriter); pWriter.println ("This is a line."); pWriter.println ("This is another line."): pWriter.close(); } }
import java.io.*; public class WriteBinaryFile { public static void main (String [] args) throws IOException { File outFile = new File ("output.bin"); FileOutputStream outStream = new FileOutputStream (outFile); DataOutputStream output = new DataOutputStream (outStream); String name = "John Doe"; long ssNum = 123456789; double gpa = 3.85; try { output.writeUTF (name); output.writeLong (ssNum); output.writeDouble (gpa); } catch (Exception e) { System.out.println (e.getMessage()); e.printStackTrace(); System.exit(0); } finally { outStream.close(); } } }
import java.io.*; public class ReadBinaryFile { public static void main (String [] args) throws IOException { File inFile = new File ("input.bin"); FileInputStream inStream = new FileInputStream (inFile); DataInputStream input = new DataInputStream (inStream); String name; long ssNum; double gpa; try { while (true) { name = input.readUTF(); ssNum = input.readLong(); gpa = input.readDouble(); System.out.println (name + " " + ssNum + " " + gpa); } } catch (EOFException e) { // Do nothing if it is the end of file. } catch (Exception e) { System.out.println (e.getMessage()); e.printStackTrace(); System.exit(0); } finally { inStream.close(); } } }