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

Creating a File

File f = new File("myfile.txt");
											boolean result = f.createNewFile();

This phrase uses the createNewFile() method to create a new file with the filename specified when constructing the File object—in this case, myfile.txt. The createNewFile() method will return a boolean value of true if the file was successfully created and false if the specified filename already exists.

Another method is available in the File class for creating a temporary file: It’s called createTempFile(). This is a static method on the File class. Here, we show how to use this method to create a temporary file:

File tmp =
   File.createTempFile("temp", "txt", "/temp");

The parameters that we pass to the createTempFile() method are the temp file’s prefix, suffix, ...

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