• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Creating a directory if it doesn't exist and then writing a file

 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



As you can see, right now, I am writing a file named employee_status_downloader_unixtimestamp.csv in an already created directory on RHEL server as shown in this line of code below:




So, I manually created a folder named JACK and then specified the above path so that the files related to the userName JACK are inside JACK folder.

Since the userName can be different and I am already getting the userName in my method above from this line of code String userName = parts[1].trim();, I am wondering if I could make use of this
to create a directory based on the userName?

So, I am not using System.getProperty("user.name"); here since I am already getting userName in my method above.

After looking online , I found that people are suggesting to do something like this :

File fl = new File("/path/directory").mkdirs(); where directory is the name of the directory I want to create/exist. But since I am using FileWriter above,  I am wondering how should I go about using both so that
I could also create a directory if it doesn't exist and then write a file in the directory.

References:

Bozho's answer:

https://stackoverflow.com/questions/3634853/how-to-create-a-directory-in-java
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you create a File object for the directory you want to create you can use the isDirectory() and exists() methods to determine if a directory (or file) with that name already exists and if it doesn't use the mkDir() or mkDirs() method to create the directory. I suggest you read the Java docs for these methods to see the difference between them.
 
Sheriff
Posts: 21922
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both mkdir and mkdirs returns false if the directory already exists, so it's not necessary to check for existence first. I tend to use mkdirs because it will create all intermediate directories as well. When using java.nio.file.Files I also tend to use createDirectories; an added bonus is that it doesn't throw an exception if the directory already exists, unlike createDirectory.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies if I'm being pedantic here but mkdir and mkdirs returning false means they couldn't create the directory not that the directory already exists. A return value of false probably is because the directory already exists but it could be for other reasons, such as the user doesn't have permission to create directories or the file system is unable to create directories.

 
Saloon Keeper
Posts: 11915
253
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Rob, the easiest way to do this is with Files.createDirectories(). You can also easily use it with Paths.get() to compose your file path:
 
Rob Spoor
Sheriff
Posts: 21922
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tony Docherty wrote:Apologies if I'm being pedantic here but mkdir and mkdirs returning false means they couldn't create the directory not that the directory already exists. A return value of false probably is because the directory already exists but it could be for other reasons, such as the user doesn't have permission to create directories or the file system is unable to create directories.


True, that's why I prefer to use Path instead of File. It throws proper IOExceptions if things go wrong.

@Stephan: working in C# lately? In Java we use "try" instead of "using"
 
Stephan van Hulst
Saloon Keeper
Posts: 11915
253
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I seem to be mixing up my languages a lot lately.
 
Climb the rope! CLIMB THE ROPE! You too tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic