-2

I am trying to create lot of files in particular directory. If directory doesn't exist then it should create the directory and create bunch of files in it.

Whereever my program is running, it should create a "files" directory if it is not there and inside this "files" folder, I want to create bunch of files in it.

I have my below code but it looks like it is creating bunch of folders instead of one folder and all the files in that folder. What wrong I am doing?

for (Entry<String, String> entry : tasks.entrySet()) {
  // looks like something is wrong here but can't figure out what wrong I am doing?
  File file = new File("files/" + entry.getKey());
  file.mkdirs();
  try (BufferedWriter writer =
      new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),
          StandardCharsets.UTF_8))) {
    writer.write(entry.getValue());
  } catch (IOException ex) {
    // log error
  }
}
user1234
  • 145
  • 1
  • 12
  • 1
    [`File#mkdirs`](https://docs.oracle.com/javase/8/docs/api/java/io/File.html#mkdirs--) would be my first suggestion – MadProgrammer Feb 06 '18 at 21:04
  • I might also consider looking at the [`File(String, String)`](https://docs.oracle.com/javase/8/docs/api/java/io/File.html#File-java.lang.String-java.lang.String-) and [`File(File, String`)](https://docs.oracle.com/javase/8/docs/api/java/io/File.html#File-java.io.File-java.lang.String-) constructors for building paths – MadProgrammer Feb 06 '18 at 21:05
  • I was already using `File#mkdirs` but forgot to mention in my code. Edited my question to add that missing part. – user1234 Feb 06 '18 at 21:07
  • If you make a directory, _A_, you probably won't be able to make a file named _A_. You may want to only make folders up to the file, but not including the file – phflack Feb 06 '18 at 21:12
  • yeah in my case I want to create directory A if it is not present, then create bunch of files in that directory A. That's what I was trying to do but looks like I got something wrong and it doesnt do as expected. – user1234 Feb 06 '18 at 21:14
  • @user1234 phflack is trying to say `File file = new File("files/" + entry.getKey());` and `file.mkdirs();` is creating a directory structure `files/{entry}`. Instead, it should be `File parent = new File("files");` and `parent.mkdirs();` (you should also look at the return result) and then use `File file = new File(parent, entry.getKey());` to create a reference to the file – MadProgrammer Feb 06 '18 at 21:16
  • @MadProgrammer yeah I was about to try it out by using it like this - `File file = new File("files", entry.getKey());` and then call this `file.mkdirs();`. Is this what you meant I should do? – user1234 Feb 06 '18 at 21:18
  • @user1234 Have look at [this previous answer](https://stackoverflow.com/questions/21059085/how-can-i-create-a-file-in-the-current-users-home-directory-using-java/21059316#21059316) for more details and a recommend mechanism for using `mkdirs` and how to check it's state – MadProgrammer Feb 06 '18 at 21:18
  • 1
    @user1234 NO! You MUST create the directory as a seperate step, first `File parent = new File("files");` and `parent.mkdirs();` THEN `File file = new File(parent, entry.getKey());` to create a reference to the file you want to write to – MadProgrammer Feb 06 '18 at 21:19
  • now I see it why we need to do that. make sense but we still need to call `file.mkdirs();` after `File file = new File(parent, entry.getKey());` right? – user1234 Feb 06 '18 at 21:20
  • @user1234 `parent.mkdirs()`, don't call `file.mkdirs()`, if you read the JavaDocs, it states *"Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories"*, so `file.mkdirs()` is actually creating the directory structure described by `new File("files/" + entry.getKey())`, this includes `files` AND `entry.getKey()` as two seperate directories, this is why these two steps need to be done separately – MadProgrammer Feb 06 '18 at 21:33

1 Answers1

0

For example, you're trying to create file C:\Stuff\Things\other.txt

With your current code, you create the folder C:\Stuff\Things\other.txt\

When you attempt to write to the file, moo.txt, it cannot, because you put a folder there (...\other.txt\)

Instead, create the folders up to, but not including the file name, before writing your file (C:\Stuff\Things\)

File file = new File(...);
file.getParentFile().mkdirs();
try(BufferedWriter ...
phflack
  • 2,729
  • 1
  • 9
  • 21