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?

André Ferraz
  • 1,511
  • 11
  • 29
  • 1
    Post the actual code that you're using so we can point you in the right direction. – jhn Apr 05 '14 at 00:21
  • 1
    See here: http://stackoverflow.com/questions/6142901/how-to-create-a-file-in-a-directory-in-java – Matt Apr 05 '14 at 00: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` – Stephen P Apr 05 '14 at 00:31
  • Thanks mate, I just realised that createDirectories only creates directories not files.... – André Ferraz Apr 05 '14 at 00:34

3 Answers3

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.

André Ferraz
  • 1,511
  • 11
  • 29
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);
Community
  • 1
  • 1
Mike B
  • 5,390
  • 2
  • 23
  • 45
  • 1
    See my comment in @helloworld's answer - don't parse paths yourself, there are already methods for that. – Stephen P Apr 05 '14 at 00:28
  • As I said in the answer, "If you're intent on using Files.createDirectories... Otherwise..." – Mike B Apr 05 '14 at 00:30
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.

Pankaj Gadge
  • 2,748
  • 3
  • 18
  • 25
  • 2
    Better to use `File.separator` instead of hard-coding the '/'. – Mike B Apr 05 '14 at 00:25
  • You're right, my intention was let user to get head started with the approach at least and then can better enhance it. – Pankaj Gadge Apr 05 '14 at 00:27
  • 1
    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_. – Stephen P Apr 05 '14 at 00:27
  • 1
    @helloworld If that's the case then why mention the delimiter at all? Just admit your mistake instead of rationalizing it. – Mike B Apr 05 '14 at 00:29