The first / was causing a problem in the string. You have to get the full string without that.

I have tried with my class to achieve it :

String path = Test.class.getProtectionDomain().getCodeSource().getLocation().getPath();
path = path.substring(1, path.length()) + "file.txt";
System.out.println(path);
Path out = Paths.get(path);
System.out.println(out.isAbsolute());

Output :

F:/software/workspace/file.txt
true
Answer from Anish B. on Stack Overflow
🌐
Medium
medium.com › @AlexanderObregon › javas-files-copy-method-explained-aba9aeb73486
Java’s Files.copy() Method Explained | Medium
January 12, 2025 - This makes it an excellent choice for modern Java applications. At its core, Files.copy() performs a copy operation by reading the source file or directory and writing its contents to the target location.
🌐
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...
🌐
Makeinjava
makeinjava.com › home › create new file & folder (directory) in java (example)
Create new file & directory (folder) using File class in java (with example)
January 3, 2024 - Successfully created new file, path:%s", newFile.getCanonicalPath()); } else { //File may already exist System.out.printf("\n2. Unable to create new file"); } //Create new directory under C:\nonExistedDirectory\directory File oneMoreDirectory = new File(dirPath + File.separator + newDirName); //Create directory for existed path.
🌐
Stack Overflow
stackoverflow.com › questions › 21269796 › java-method-to-just-put-a-file-in-a-folder-both-as-parameters › 21270373
Java method to just put a file in a folder (both as parameters) - Stack Overflow
FileInputStream fis = new FileInputStream(pFile); FileOutputStream fos = new FileOutputStream(targetFile); copyStream(fis, fos, 10000); fos.flush(); fos.close(); fis.close(); ... public static void copyStream(final InputStream inputStream, final ...
Find elsewhere
🌐
GitHub
github.com › Microsoft › vscode-java-test › issues › 144
java.io.FileNotFoundException because target-Folder not available · Issue #144 · microsoft/vscode-java-test
March 21, 2018 - But with Java Test Runner I'm getting a java.io.FileNotFoundException. @Test public void test() throws IOException { try (final FileOutputStream out = new FileOutputStream("target/file.txt")) { out.write("A string".getBytes()); } } In the launch configuration I can add the target folder to the source path to make it work.
Author   microsoft
🌐
Marco Behler
marcobehler.com › guides › java-files
How To Work With Files In Java
December 9, 2020 - You can use this guide to learn how to work with files in Java through the Path API. From reading and writing files, to watching directories & using in-memory file systems.
🌐
Medium
medium.com › @encorex32268 › java-copy-file-source-to-target-a2c693da6d41
[Java]Copy File Source To Target. copy file use java api , copy folder… | by LiHan | Medium
December 6, 2018 - String sourceFile ="C:\\Users\\lee_chen\\Desktop\\example\\source\\Account.txt"; String targetFile ="C:\\Users\\lee_chen\\Desktop\\example\\target\\Account.txt"; Path sourcePath = new File(sourceFile).toPath(); Path targetPath = new File(targetFile).toPath(); try { Files.copy(sourcePath,targetPath, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { e.printStackTrace(); }
🌐
Stack Overflow
stackoverflow.com › questions › 23217971 › how-do-you-specify-a-target-directory-for-a-txt-file-to-be-passed-in
java - How do you Specify a target directory for a txt file to be passed in - Stack Overflow
April 22, 2014 - enter code hereI am trying to specify a directory for a txt.file which is being created when the jar file is run, is there anyway to specify as an argument in the console the directory of where i want that txt file to be created: ... public static void main(String[] args) { // TODO Auto-generated method stub C:\Users\802591720\Documents\mystuff\Programs\JavaCode\xmlSchema\ManageBillingCreditAndDebit.20130219.xsd //String fileName = "ManageBillingCreditAndDebit.20130219.xsd"; //String Source = "C:\Users\607817544\as1\"; String path1 = args[0]; String FileName = args[1]; TagDef tagDef; String[]
🌐
TutorialsPoint
tutorialspoint.com › javaexamples › file_dir.htm
How to create a file in a specified directory using Java
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()); } } }
🌐
Baeldung
baeldung.com › home › java › java io › java – create a file
Java - Create a File | Baeldung
August 29, 2024 - This article is part of the “Java – Back to Basic” series here on Baeldung. In the examples, we’ll define a constant for the file name: private final String FILE_NAME = "src/test/resources/fileToCreate.txt"; And we’ll also add a clean-up step to make sure that the file doesn’t already exist before each test, and to delete it after each test runs: @AfterEach @BeforeEach public void cleanUpFiles() { File targetFile = new File(FILE_NAME); targetFile.delete(); }
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › io › copy.html
Copying a File or Directory (The Java™ Tutorials > Essential Java Classes > Basic I/O)
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases. See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases. You can copy a file or directory by using the copy(Path, Path, CopyOption...) method. The copy fails if the target file exists, unless the REPLACE_EXISTING option is specified.
🌐
Stack Overflow
stackoverflow.com › questions › 30305588 › how-to-write-a-file-to-a-particular-folder-in-java
how to write a file to a particular folder in java? - Stack Overflow
May 18, 2015 - The package name is same as the name of the destination folder. Changing the package name or the folder name, every method listed is working fine
🌐
Stack Overflow
stackoverflow.com › questions › 41637131 › how-to-insert-a-file-in-a-specific-target-file
java - How to insert a file in a specific target file - Stack Overflow
January 13, 2017 - (Note that the upcoming Tycho version 1.0.0 will further improve rootfiles support in Tycho by supporting the root.folder.<subfolder> syntax.) ... I would use build profiles to accomplish this. You can specify a profile for any platform you support, and optionally add the activation: <profiles> <profile> <id>win32-win32-x86</id> <activation> <os> <arch>x86</arch> <family>windows</family> </os> </activation> <properties> <target.os>win32</target.os> <target.ws>win32</target.ws> <target.arch>x86</target.arch> </properties> </profile> <profile> <id>win32-win32-x86_64</id> <activation> <os> <arch>