Opening a File by Name

// opening a file for reading
											BufferedReader is =
											new BufferedReader(new FileReader("file.txt"));
											// opening a file for writing
											BufferedWriter out =
											new BufferedWriter(new FileWriter("afile.txt"));

In this phrase, we show how you can create a BufferedReader for reading input from a file specified by a filename—in this case somefile.txt—and how you can create a BufferedWriter for writing text to an output file specified by name, afile.txt.

It is very easy to open a file by name in Java. Most of the input and output stream and reader classes you will use have an option of specifying the file by name in the stream or reader’s constructor.

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.