You should use the secondary constructor for File to specify the directory in which it is to be symbolically created. This is important because the answers that say to create a file by prepending the directory name to original name, are not as system independent as this method.

Sample code:

String dirName = /* something to pull specified dir from input */;

String fileName = "test.txt";
File dir = new File (dirName);
File actualFile = new File (dir, fileName);

/* rest is the same */

Hope it helps.

Answer from Kaushik Shankar on Stack Overflow
🌐
W3Schools
w3schools.com › java › java_files_create.asp
Java Create Files
On Mac and Linux you can just write the path, like: /Users/name/filename.txt · File myObj = new File("C:\\Users\\MyName\\filename.txt"); ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
Baeldung
baeldung.com › home › java › java io › java – write to file
Java - Write to File | Baeldung
December 1, 2023 - Looking at the common usage practices, ... and FileChannel to write faster in larger files. Some of the APIs of these classes do allow more, but this is a good place to start. This article illustrated the many options of writing data to a file using Java....
🌐
Quora
quora.com › How-do-I-save-a-file-to-a-specific-directory-in-Java
How to save a file to a specific directory in Java - Quora
Answer (1 of 2): Java is pretty friendly with IO. Try something like this: [code]File file = new File("/some/absolute/path/myfile.ext"); OutputStream out = new FileOutputStream(file); // Write your data out.close(); [/code]Notes: * Your program needs permission to write to the directory. * I...
🌐
Java67
java67.com › 2014 › 02 › how-to-create-file-and-directory-in-java.html
How to Create File and Directory in Java with Example | Java67
First time it will create directory and file, and second time, it will just say that they already exists. This is the complete code of our sample Java program to create files and directories in Java. As I told same object java.io.File is used to represent both file and directory, its important to name variable properly to distinguish between them e.g.
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › io › file.html
Reading, Writing, and Creating Files (The Java™ Tutorials > Essential Java Classes > Basic I/O)
The Managing Metadata page explains file attributes, and how to access and set them. ... Several of the methods in this section take an optional OpenOptions parameter. This parameter is optional and the API tells you what the default behavior is for the method when none is specified. The following StandardOpenOptions enums are supported: WRITE – Opens the file for write access.
🌐
Blogger
javarevisited.blogspot.com › 2011 › 12 › create-file-directory-java-example.html
How to Create File and Directory in Java Example - Java IO Tutorial
Here is a simple example of how to create a file in Java: ... In this example of creating a File in Java, we have created one new file called stock file inside the stock directory on d drive first time when we execute this program it will check for the file if it will not get that file simply create the new file and flag will become true, next time when we again run this program the file is already get created inside d:\\stock folder so it will not create the file and flag value will be false.
🌐
Marco Behler
marcobehler.com › guides › java-files
How To Work With Files In Java
December 9, 2020 - They let you write and read files, completely in-memory, without ever hitting your disk. Super-fast and a great fit for testing (as long as you don’t run out of memory, erm…​ ). There are two Java in-memory file systems that are worth looking at. One choice is Memory File System. Let’s see how you would create an in-memory filesystem with it. package com.marcobehler.files; import com.github.marschall.memoryfilesystem.MemoryFileSystemBuilder; import java.io.IOException; import java.nio.file.FileSystem; import java.nio.file.Files; import java.nio.file.Path; public class MemoryFileSystem { public static void main(String[] args) throws IOException { try (FileSystem fileSystem = MemoryFileSystemBuilder.newMacOs().build()) { Path inMemoryFile = fileSystem.getPath("/somefile.txt"); Files.writeString(inMemoryFile, "Hello World"); System.out.println(Files.readString(inMemoryFile)); } } }
🌐
Mkyong
mkyong.com › home › java › how to create directory in java
How to create directory in Java - Mkyong.com
July 29, 2020 - File files = new File("C:\\Directory2\\Sub2\\Sub-Sub2"); if (<b>!</b>files.exists()) { if (files.mkdirs()) { ... Thanks for your feedback. We updated the article, the feedback is irrelevant now.
🌐
W3Schools
w3schools.com › java › java_files_write.asp
Java Write To Files
Explanation: This program tries to write some text into a file named filename.txt. If everything works, the program will print "Successfully wrote to the file." in the console. If something goes wrong (for example, the file cannot be opened), it will print "An error occurred." instead. Since Java 7, you can use try-with-resources.
🌐
TutorialsPoint
tutorialspoint.com › how-to-create-a-new-directory-by-using-file-object-in-java
How to create a file in a specified directory using Java
August 1, 2019 - The following is an example of file in a specified directory. import java.io.IOException; import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class CreateFile { public static void main(String[] args) throws IOException { Path p1 = Paths.get("C:/"); Files.createDirectories(p1.getParent()); try { Files.createFile(p1); } catch (FileAlreadyExistsException e) { System.err.println("already exists: " + e.getMessage()); } } }
🌐
Mkyong
mkyong.com › home › java › java create and write to a file
Java create and write to a file - Mkyong.com
October 2, 2020 - In Java, we can use Files.write to create and write to a file.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-write-to-file
Java Write to File - 4 Ways to Write File in Java | DigitalOcean
August 3, 2022 - FileWriter: FileWriter is the simplest way to write a file in Java. It provides overloaded write method to write int, byte array, and String to the File. You can also write part of the String or byte array using FileWriter.