Soatl
Soatl

Reputation: 10592

Creating Multiple File Directories

I have a program that takes a file that points to a folder somewhere. I need to then make two separate directories. For example, say I have a file base that points to folder Base. I would then want to create two directories dir1 and dir2.

I know that you do the following:

//Called in constructor
File base = new File (baseFileLocString);

//Make directories
File dir1 = new File (base.getAbsoluteFilePate() + "/dir1");
dir1.mkdir();

File dir2 = new File (base.getAbsoluteFilePate() + "/dir2");
dir2.mkdir();

I do not like this way though. Ideally I could use base and make the directories without having to create new Files. I feel like there should be a more efficient way to do this. Is that the case or no?

Upvotes: 0

Views: 202

Answers (2)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136022

There is an alternative

Files.createDirectory(Paths.get(base.getAbsoluteFilePath(), "dir1"));

besides it is better than File.mkdir because if something goes wrong mkdir returns false without expalnation and createDirectory throws an exception which explains what happened

Upvotes: 1

joaonlima
joaonlima

Reputation: 618

Instead of

File dir1 = new File (base.getAbsoluteFilePath() + "/dir1");

You could use

File dir1 = new File (base, "dir1");

Looks better, but the performance will stay the same

Upvotes: 1

Related Questions