This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Cloud Application Architecture Patterns: Designing, Building, and Modernizing for the Cloud and have Kyle Brown, Bobby Woolf and Joseph Yodor on-line!
See this thread for details.
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

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

 
Ranch Hand
Posts: 494
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I seem to be mixing up my languages a lot lately.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic