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 OverflowYou 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");
Videos
you can use getAbsolutePath() function:
FileWriter fstream = new FileWriter(new File(".").getAbsolutePath()+"//folder//out.txt",true);
and i sugest you to take a look at this thread
Creating a folder named text in the code's directory is file system independent. To create a file in {project folder}/text/out.txt you can try this:
String savePath = System.getProperty("user.dir") + System.getProperty("file.separator") + text;
File saveLocation = new File(savePath);
if(!saveLocation.exists()){
saveLocation.mkdir();
File myFile = new File(savePath, "out.txt");
PrintWriter textFileWriter = new PrintWriter(new FileWriter(myFile));
textFileWriter.write("Hello Java");
textFileWriter.close();
}
Don't forget to catch the IOException!
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");
In my project i created a "logs" folder outside the src folder with the file definition:
File file = new File("logs/file.txt");
So I expect you can create a file there with File("/file.txt")
Try using the asbolute path. When you use the relative path, the file is not being created inside the folder you think it is.
You can try the following to check where your relative path is:
Path currentRelativePath = Paths.get("");
String s = currentRelativePath.toAbsolutePath().toString();
System.out.println("Current relative path is: " + s);
Try it and let me know.
EDIT: See this for more information: http://docs.oracle.com/javase/tutorial/essential/io/pathOps.html
The best way to do it is:
String path = "C:" + File.separator + "hello" + File.separator + "hi.txt";
// Use relative path for Unix systems
File f = new File(path);
f.getParentFile().mkdirs();
f.createNewFile();
You need to ensure that the parent directories exist before writing. You can do this by File#mkdirs().
File f = new File("C:/a/b/test.txt");
f.getParentFile().mkdirs();
// ...
Something like:
File file = new File("C:\\user\\Desktop\\dir1\\dir2\\filename.txt");
file.getParentFile().mkdirs();
FileWriter writer = new FileWriter(file);
Since Java 1.7 you can use Files.createFile:
Path pathToFile = Paths.get("/home/joe/foo/bar/myFile.txt");
Files.createDirectories(pathToFile.getParent());
Files.createFile(pathToFile);
File dir = new File("tmp/test");
dir.mkdirs();
File tmp = new File(dir, "tmp.txt");
tmp.createNewFile();
BTW: For testing use @Rule and TemporaryFolder class to create temp files or folders
You can create paths relative to a directory with the constructors that take two arguments: http://docs.oracle.com/javase/6/docs/api/java/io/File.html
For example:
File tempfile = new File("user.dir/tmp", "tempfile.txt");
By the way, the backslash "\" can be used only on Windows. In almost all cases you can use the portable forward slash "/".