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 OverflowThe 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
Just use this
String pathTemp=System.getProperty("user.dir"); //it will give the root directory of where your program runs
String pathTemp=pathTemp+"/target/"+fileName; //here file name from the argument
Path path=Paths.get(pathTemp);
Files.write(path,list,Charset.defaultCharset());
Java is pretty friendly with IO. Try something like this:
File file = new File("/some/absolute/path/myfile.ext");
OutputStream out = new FileOutputStream(file);
// Write your data
out.close();
Notes:
- Your program needs permission to write to the directory.
- If the first character of your path string is not
/, it will be relative to your "current" directory - If you're writing text, you might find a
BufferedWritereasier:BufferedWriter writer = new BufferedWriter(new FileWriter(file));. It hasnewLine()andwrite(String)methods
When you insantiate your FileOutputStream you can pass an absolute path to the constructor. Like this:
FileOutputStream os = new FileOutputStream("/path/to/file.txt");
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.
Use:
File file = new File("Z:\\results\\results.txt");
You need to double the backslashes in Windows because the backslash character itself is an escape in Java literal strings.
For POSIX system such as Linux, just use the default file path without doubling the forward slash. this is because forward slash is not a escape character in Java.
File file = new File("/home/userName/Documents/results.txt");
You could also use java.nio.file.Paths:
URL url = Paths.get("target", "results.csv").toUri().toURL();
I got the solution for this. Here is how I am implementing. I have my own class CSVReader which uses CSVReader library. I am writing my results in results.csv file under target directory.
URL location = CSVReader.class.getProtectionDomain().getCodeSource().getLocation();
String path = location.getFile().replace("classes/","") + "results.csv";
In the above code I am getting the target directory path and removing the classes/ by string replace method and appending my desired file name. Hope this might help someone.
If you want to access the current directory you should reference the file directly, and use try with resources to clean up the output stream:
public static void main(String args[]) throws IOException {
int max=10;
File output = new File("output.txt");
System.out.println("Writing to: "+output.getAbsolutePath());
try(BufferedWriter writer = new BufferedWriter(new FileWriter(output))) {
writer.write(Integer.toString(max));
}
}
The code is simpler in later JDKs using Path and Files:
public static void main(String ... args) throws IOException {
int max = 10;
Path output = Path.of("output.txt"); // Use Paths.get in JDK9
System.out.println("Writing to: "+output.toAbsolutePath());
Files.writeString(output, Integer.toString(max));
}
create a new folder and keep your java and output.txt file inside the folder then compile and run the program. Hope it will solve your problem