java create file in folder

www.lau‮tt‬uri.com
java create file in folder

To create a file in a specific folder in Java, you can use the createNewFile method of the File class.

For example, to create a file called "output.txt" in the current directory, you can use the following code:

File file = new File("output.txt");
file.createNewFile();

This creates a new file called "output.txt" in the current directory. If the file already exists, the createNewFile method throws an IOException.

To create a file in a specific directory, you can specify the directory path in the File constructor. For example, to create a file called "output.txt" in the "C:\temp" directory, you can use the following code:

File file = new File("C:\\temp\\output.txt");
file.createNewFile();

You can also use the mkdirs method of the File class to create the parent directories of the file if they don't exist. For example, to create a file called "output.txt" in the "C:\temp\files" directory, you can use the following code:

File file = new File("C:\\temp\\files\\output.txt");
file.getParentFile().mkdirs();
file.createNewFile();

This creates the directory "C:\temp\files" and the file "output.txt" inside it. If the directory already exists, the mkdirs method does nothing.

Created Time:2017-11-03 00:14:52  Author:lautturi