14 10 Creation and reading of a file

In the previous lesson we played with creation and reading separately. Here we combine both into a single example using FileWriter and FileReader. We keep only the variable that holds the file we want to work with and remove the rest of the leftover code.

In Java, the classes that read a file inherit from InputStream (or, for character streams, Reader). The interesting thing is that each reader class exposes several constructors. FileReader can be built from a File object or from a String path, so we will use both in the example to see how that works.

Write, flush, read, print

  • Instantiate a FileWriter with the file as a constructor argument.
  • Call write(...) to push characters into the buffer.
  • Call flush() to actually send the buffered content to disk without permanently closing the stream.
  • Open a FileReader on the same file, prepare a char[] array as the read buffer, then loop and call read(buffer) until the end of the file.
  • Print every character with System.out.print(...) so the content reappears in the console.

As always with file streams, do not forget to close both the writer and the reader once you are done. Closing them releases the underlying file handle and lets the operating system reclaim the resources. With this small program we have a full round trip: data goes from memory into the file, then comes back from the file into memory.

Summary

This lesson covers file creation and reading operations in Java using the FileReader and FileWriter classes. The course explains how these classes inherit from stream-based parent classes, demonstrates their constructors (accepting either File objects or string paths), and covers essential methods like flush() for buffering content and close() for properly terminating file streams.

Key points

  • FileReader and FileWriter are Java classes used for reading and writing file content, inheriting from stream-based parent classes
  • FileReader constructors accept either a File object or a string filepath as parameters
  • FileWriter is instantiated similarly and used to write character data to files
  • flush() method writes buffer content to the destination but keeps the stream open for further operations
  • close() method must always be called to permanently close and release file streams
  • Character arrays are typically used to store and display file content when reading

FAQ

What classes does Java provide for file operations?

Java provides FileReader for reading file content and FileWriter for writing to files. Both classes inherit from stream-based parent classes (InputStream and OutputStream respectively).

What is the difference between flush() and close()?

flush() writes the buffered content to the file destination but leaves the stream open for continued use. close() permanently closes the stream and releases system resources.

How do you read file content into a Java program?

Create a FileReader instance (with a filepath string), declare a character array, read the content into the array, and display it using System.out.println() or similar output methods.