5

This is the problem I have: If part or all of the path does not already exist, the server should create additional directories as necessary in the hierarchy and then create a new file as above.

Files.createDirectories(path);

That's what I am currently using, but it does not create the end file. For example is the path="/hello/test.html" it will create a directory called "hello" and one called "test.html", I want the test.html to be a file. How can I do that?

4
  • 1
    Post the actual code that you're using so we can point you in the right direction. Commented Apr 5, 2014 at 0:21
  • 1
    See here: stackoverflow.com/questions/6142901/… Commented Apr 5, 2014 at 0:24
  • Yes, see what @Matt has pointed to, but also I've found that Java works with forward-slash / separators on every system I've tried - Unix, Mac, Windows, even VMS where paths look like volName:[topdir.subdir.otherdir]filename.txt Commented Apr 5, 2014 at 0:31
  • Thanks mate, I just realised that createDirectories only creates directories not files.... Commented Apr 5, 2014 at 0:34

3 Answers 3

5

This is what I did to solve this "problem" or misuse of the libraries.

Files.createDirectories(path.getParent());
Files.createFile(path);

The first line will get the parent directory, so lets say this is what I want to create "/a/b/c/hello.txt", the parent directory will be "/a/b/c/".

The second like will create the file within that directory.

Sign up to request clarification or add additional context in comments.

Comments

1

Have you looked at the javadoc? createDirectories only creates... directories. If you're intent on using Files.createDirectories, parse off the file name, call createDirectories passing only the path portion, then create a new file passing the entire path. Otherwise this is a better approach.

Files.createDirectories(path.substring(0, path.lastIndexOf(File.separator)+1));
File yourFile = new File(path);

2 Comments

See my comment in @helloworld's answer - don't parse paths yourself, there are already methods for that.
As I said in the answer, "If you're intent on using Files.createDirectories... Otherwise..."
0
  1. you can parse the 'path' variable to isolate the file and the directory using delimiter as '/', and do File file = new File(parsedPath); This would work only when you know that you ALWAYS pass the file name at the end of it.

  2. If you know when you are a) creating a directory b) creating a directory and file, you can pass the boolean variable that would describe if file needs to be created or not.

4 Comments

Better to use File.separator instead of hard-coding the '/'.
You're right, my intention was let user to get head started with the approach at least and then can better enhance it.
You don't have to parse first, you can just do file = new File(somepath) because File represents a path and file that could (but doesn't necessarily) exist. Then use file.getCanonicalPath() to get only the path part of it with all things like "foo/../bar" already resolved. Then call createDirectories on that.
@helloworld If that's the case then why mention the delimiter at all? Just admit your mistake instead of rationalizing it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.