FileInputStream istr = new FileInputStream(f); InputStreamReader irdr = new InputStreamReader( istr ); // promote int size = (int) f.length(); // get the file size (in bytes) char[] data = new char[size]; // allocate char array of right size irdr.read( data, 0, size ); // read into char array irdr.close(); String contents = new String(data);
String contents = /* some string */; FileOutputStream ostr = new FileOutputStream(f); OutputStreamWriter owtr = new OutputStreamWriter( ostr ); // promote owtr.write( contents, 0, contents.length() ); owtr.close(); // use close to ensure completion
InputStreamReader
BufferedReader.
FileInputStream istr = new FileInputStream(f);
InputStreamReader irdr = new InputStreamReader( istr ); // promote
BufferedReader brdr = new BufferedReader( irdr ); // promote
String line;
while ( (line = brdr.readLine()) != null )
{
// do something with the line you just read
}
brdr.close();
OutputStreamReader
PrintWriter.
FileOutputStream ostr = new FileOutputStream(f);
OutputStreamWriter owtr = new OutputStreamWriter( ostr ); // promote
PrintWriter pwtr = new PrintWriter( owtr, true ); // promote
/* The "true" in the definition of "pwtr" signifies that each println
* transmits the line to the file. If "false", or the boolean argument
* is omitted, then println buffers the lines until the print buffer
* is filled or the "pwtr.close()" occurs.
*/
while ( /*some condition*/ )
{
String line = /* some line of text */;
pwtr.println( line );
}
pwtr.close();
For programs which require both Read and Write access to a file, the RandomAccess class is the preferred choice.
FileInputStream istr = new FileInputStream(f); BufferedInputStream bstr = new BufferedInputStream( istr ); // promote int size = (int) f.length(); // get the file size (in bytes) byte[] data = new byte[size]; // allocate byte array of right size bstr.read( data, 0, size ); // read into byte array bstr.close();
FileOutputStream ostr = new FileOutputStream(f); BufferedOutputStream bstr = new BufferedOutputStream( ostr ); byte[] contents = /* some binary byte[] array */; bstr.write( contents, 0, contents.length ); bstr.close();
An object can be written to a file as long as it is serializable. This means that the class must implement the Serializable interface. Many standard Java objects are serializable such as GUI Components, data structures classes, standard classes from java.lang, etc.
Classes which are not serializable are typically those which represent some on-going connection, like open files classes (from java.io) and database connection classes (like java.sql.ResultSet). A user-defined class can be made serializable simply by declaring
class MyClass implements Serializable {
// ...
}
There are no member functions to implement, however,
this declaration will only be effective
if every object used by this class is itself serializable.
FileOutputStream ostr = new FileOutputStream(f); ObjectOutputStream oostr = new ObjectOutputStream( ostr ); oostr.writeObject( obj ); oostr.close(); // may be necessary to complete the write
FileInputStream istr = new FileInputStream(f); ObjectInputStream oistr = new ObjectInputStream( istr ); Object theObject = oistr.readObject(); oistr.close(); // this is probably not necessaryThe type of the object read is usually known; to use it one typically casts it to the known type:
MyClass c = (MyClass) theObject;Casting to the wrong type will cause an exception to occur, and so if there are more than one possible type for the object, we can usually distinguish between choices using the instanceOf operator in statements like this:
if (theObject instanceOf MyClass) {
MyClass c = (MyClass) theObject;
// etc.
}
Observe that the System.out.println operator is likely to succeed
without casting because it uses the member function toString() defined
on the Object level.