O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required