Notes on Java File I/O
There are several Java classes that deal with file I/O at different levels. You can view a simple example of a Java program using these methods here.
The class File represents file and directory names. It is used to identify a particular file. Objects of the class File can be used to manipulate the directory entries for a file, but are not used directly to access the data in a file.
public File(String pathname)
Creates a new
File instance by converting the given pathname string into an
FileInputStreamobtains input bytes from a file in a file system. A file output stream is an output stream for writing data to a
FileInputStream inherits from InputStream andFileOutputStream inherits from OutputStream. These parent classes provide methods from reading and writing bytes to streams, including the standard streams System.in and System.out.
public FileOutputStream(File file) throws IOException
Creates a file output stream
to write to the file represented by the specified
public FileInputStream(File file) throws FileNotFoundException
FileInputStream by opening a connection to
an actual file, the file named by the
file in the file system.
FileInputStreams and FileOutputStreams only read a sequence of bytes. Java programs generally do not deal directly with byte objects. The classes DataInputStream and DataOutputStream use FileInputStreams and FileOutputStreams to read bytes and convert them to primitive Java data types. A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream. A data input stream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in. Methods of the classes DataInputStream and DataOutputStream write data to the file in their internal format. You can write a float variable with the value 1.23 to a file and then read it back in with another program to retrieve the same value. If you look at the file with an editor, you will not see the characters “1.23”. The float will be written as four bytes whose value in binary are the internal representation of the number.
Bytes are 8 bit values while characters in Java are 16 bit Unicode values. To read or write characters in Java, objects of the class InputStreamReader and OutputStreamWriter can be used. An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and translates them into characters. An OutputStreamWriter writes characters to an output stream, translating characters into bytes.
public OutputStreamWriter(OutputStream out)
public InputStreamReader(InputStream in)
Objects of InputStreamReader and OutputStreamWriter perform I/O to read each character. Performance can be improved by buffering the I/O. Object of the classes BufferedReader and BufferedWriter can be used to buffer the data from InputStreamReader and OutputStreamWriter objects.
public BufferedReader(Reader in)
Create a buffering character-input stream that uses a default-sized input buffer.
public String readLine() throws IOException
Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed. The returned string contains the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
public BufferedWriter(Writer out)
Create a buffered character-output stream that uses a default-sized output buffer.
public void write(String s, int off, int len) throws IOException
Write len characters of a String starting at character off.
Creates a JFileChooser pointing to the user's home directory.
public File getSelectedFile()
Returns the selected file. This can be set either by the programmer via setFile() or by a user action, such as either typing the filename int the UI or selecting the file from a list in the UI.
public void setFileFilter(FileFilter filter)
Sets the current File Filter. The file filter is used by the filechooser to filter out files from view from the user.
public int showOpenDialog(Component parent)
Pops up an "Open File" file chooser dialog
public int showSaveDialog(Component parent)
Pops up a "Save File" file chooser dialog.
public abstract class javax.swing.filechooser.FileFilter
public abstract boolean accept(File f)
Whether the given file is accepted by this filter.
public abstract String getDescription()