Creating a directory/folder on a mac

+1 Cem Saglam · August 20, 2014
Hello,

I'm extremely new in using a mac, so I'm not good with directories, pathnames etc. (a minute to laugh)
Simply, all I want to do is to create a .txt file in which I'll store data. On windows I can just store that in C:/stuff or something. Can you suggest a stable path name like C:/stuff for mac?


ps: may be an extremely simple/stupid question. Yet I literally know very little about mac.

Post a Reply

Replies

Oldest  Newest  Rating
+4 Mathias Frits Rørvik · August 20, 2014
/ is the root folder, it is equivalent of C: on windows.

/Users/<your username>

This is your home folder, this is where you should store all your files. You can create your own directories here if you want. 

I personally have created a directory in my home directory /Users/cyanboy/ called Development where I again have subfolders for all my programming related projects.

Also you should have posted this topic in the Apple/Mac forum, not the Java forum ;)
+3 Mathias Frits Rørvik · August 20, 2014
I'm not sure if I quite understand what you're trying to say. Your java program is doing operations on a file, right? 
+2 Cem Saglam · August 20, 2014
Firstly, sorry about the wrong post place. Since I was coding in Java I assumed it'd be fine, as I'm trying to create a directory. ^^'

I tried what you suggested, and it worked, so thanks!
BUT, assume I'll be sharing this program with other mac users. The folder will try to be created at <myusername>, but that is not the case in any other computer.
Any opinions on that?
+2 Cem Saglam · August 20, 2014
Yes, it does.
Let me put it this way, quoting what you typed:

/ is the root folder, it is equivalent of C: on windows.

/Users/<your username>


Say my username is abc.
I can create and handle my files using /Users/abc (ON MY COMPUTER), which is great.


BUT I'll be sharing this program with other computers. Other computers will not have abc as a username, they'll have something entirely different.


So whenever my program tries to access /Users/abc on another computer, it will not work, since that mac has an entirely different username. 


Hence I need something like this:


/Users/<whateverTheUsernameIsOnThisComputer>


Is my problem clearer?
+2 Mathias Frits Rørvik · August 20, 2014
You got to access the needed files relative to where the program runs. Not use absolute path ;)
+1 Cem Saglam · August 20, 2014
Sorry, but still something doesn't add up.

Say my username is abc. And my code will be creating text files with data in them, at a directory called myProgram.

Hence I'm assuming /Users/abc/myProgram is the general place where the text files will be created via the code.
BUT, here is my question: the code will try to create text files in /Users/abc/myProgram


Will the fact that other mac's have different usernames (other than abc) not create a problem with the way I create my directory? Won't the code try to access abc and fail? (since it's not there to be found)
+1 Aubree Keegan · August 21, 2014
Can you post your code?
+1 Cem Saglam · August 23, 2014
I solved most of the problem. The program is now compatible to mac, with a minor bug.
Thanks for all your help, guys.
0 Çağlan Turgut · August 21, 2014
You can use 


String uname = System.getenv("USER");



to get to username and put to the place of "abc" but I think using the path that program runs on is much more safe .

Edit:

You can see the relative path by typing:


String filename = new File("").getAbsolutePath(); 
System.out.println(filename);
  • 1

Java / Android Development

106,968 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator