• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Issue with mkdir()

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello



I am having an issue where I cant seem to make a folder in the C:\\Users" directory


https://i.imgur.com/UJM3sTC.png



Results:
https://i.imgur.com/4xDkrN9.png

WE found C Users
MAKE FOLDER  true
false


Does anyone have any suggestions??

Thank you for your help
 
Steve Datz
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found a solution :
_filePath = System.getProperty("user.dir");

however I would still like to know why I cant write to user despite it saying its writeable
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Datz wrote:I found a solution :
_filePath = System.getProperty("user.dir");



Chances are that code returns "C:\Users\Steve" and not "C:\Users", at least that's how it works on my Windows box. And perhaps you aren't an administrator so you can't create new users?
 
Steve Datz
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul it returns :
C:\Users\Steve\Documents\IntelliJProjects\CS358HW1

which is where my project is, and I can write here.


And I am an Admin, when I go to create a folder in C:\\Users I see an admin symbol there when I click the button.
(this is my home PC, ironically when I was at work this was the only place I had permission to write to and I wasnt an admin , both win10)
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you print the value returned by System.getProperty("user.dir")?  Are you confusing that property with user.home?
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say new File with a path in it it's a file object "theoretically" so asking if it is a folder when there is no existence of it is in fact a deceptive return on
isFolder because a file does not need to have an extension but you have not created it.

Second I don't know why you would put the mkdir command method in a println statement, that certainly would not help, the best. I can gather it's used the Object toString output but best remove it to its own line.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

don't know why you would put the mkdir command method in a println statement


To see the results.  I would add some text to the println to make it more readable:
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"user.dir" is the current working directory. That will vary depending on how you started up the Java application. Note that Windows has a separate working directory for each drive, so user.dir itself will be the current working directory for the currently-logged drive.

Since about Windows XP, Microsoft attempted to make Windows more like Unix/Linux and added a C:\Users directory which anchors each user for a Windows computer in the same way that /home would for Unix (including MacOS) or Linux. You should not store your own files or directories there. Instead store them either under your actual user home directory (for example, C:\Users\NormRadder) or under the "All Users" directory, depending on whether the data is unique to your login or might be shared between multiple logins. Java can tell what your home directory is via the user.home System Property.

So let's say that I want to create a directory for my application (MyApp) and I want a SessionData directory underneath if. So in other words "C:\Users\NormRadder\MyApp\SessionData". Here's one way to do it:


Note that this code is actually OS-Independent! there are no slashes, backslashes, or assumed drive IDs or anything that would tie the code to Windows, Linux, or even IBM's zOS mainframe operating system.

It's obviously not good enough for real work since I didn't check return codes or handle any possible Exceptions, but it's the nucleus of a very clean solution.

Incidentally, I don't usually do too much pre-checking. If I can be sure of the outcome, for example, I'll try and make the directory and if it fails because it aready exists, that's fine and it it doesn't fail that's fine, and if it fails because of bad naming, missing parent directory or whatever I deal with that as an exceptional condition. Also, the "isDirectory()" File method can detect missing directories as well as files whose names are the same as your intended directory name.

And for the confused, I should point out that java.io.File is badly named. It doesn't actually represent a file as such, but rather a filename and path.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I store very little of my stuff on the C: drive.  I've partitioned my HDD to have a D: drive for files I'm working on and an E: drive for data (files that are not being changed much like photos or documentation)
I don't write anything into C:\Users\Norm.  The C: drive is for MS and other installed programs.
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's common practice - in fact, it's factory default on some Linux distros - to put /home in its own partition. That makes it easier to survive major OS upgrades and to re-size or replace with a bigger disk if needed.

I expect that there's actually a registry setting for Windows to be able do the equivalent. I know that, for example, Program Files could be re-mapped. I've had to do it.

The user directory is, in fact intended precisely for per-user data, so it really should be employed to its best advantage.

One thing that Windows doesn't have a very good convention on is large/variable application data storage. The convention on Linux is to use /var/lib as the anchor for application variable data and /usr/local for costant data. /var, like /home, is often given its own filesystem/partition. /usr/local, properly used, is read-only. But, as I said, Windows was never designed with that sort of complexity in mind, so generally you end up either hanging something off a drive root or using Users\Shared Data (or whatever it's called this week).
 
Marshal
Posts: 4501
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Application data should be stored in the AppData directories:
    \Users\user-name\AppData\Local - available at single workstation only
    \Users\user-name\AppData\Roaming - available at all workstations

For example, on my Windows workstation:
    C:\Users\Ron\AppData\Local
    C:\Users\Ron\AppData\Roaming
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:For example, on my Windows workstation:
    C:\Users\Ron\AppData\Local
    C:\Users\Ron\AppData\Roaming


You can use System.getenv("LOCALAPPDATA") and System.getenv("APPDATA") to get those values.

Regarding the original issue, if you'd use Path instead of File you would have gotten a clear exception. For instance:

 
Tomorrow is the first day of the new metric calendar. Comfort me tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic