aspose file tools*
The moose likes Beginning Java and the fly likes Creating a folder in C drive root Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating a folder in C drive root" Watch "Creating a folder in C drive root" New topic
Author

Creating a folder in C drive root

Aaron John
Ranch Hand

Joined: May 30, 2005
Posts: 74
I'd like to create a folder in the C:\ root location. I have a program which does this.



On my computer, this creates a folder called Test under C:\MyPrograms\Java\Test

How would I change the code above to create a folder under C:\Test, without specifying the C:\ prefix? If I ran my program under F:\MyPrograms\Java\Test, how could I achieve get the folder under F:\Test?
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
ok below shown
is the program for creating the folder in any Drive or path given by you.



public class Fold {


public static void main(String[] args) {
FileOutputStream fout;
FileInputStream fin;



String newFolderPath = "C:\\";
String newFolderName = "Sonu";
String spoonFeeding = newFolderPath + newFolderName;
File f = new File(spoonFeeding);
f.mkdir();

}
}


I hope this will help you.
Aaron John
Ranch Hand

Joined: May 30, 2005
Posts: 74
Originally posted by dhwani mathur:
ok below shown
is the program for creating the folder in any Drive or path given by you.



public class Fold {


public static void main(String[] args) {
FileOutputStream fout;
FileInputStream fin;



String newFolderPath = "C:\\";
String newFolderName = "Sonu";
String spoonFeeding = newFolderPath + newFolderName;
File f = new File(spoonFeeding);
f.mkdir();

}
}


I hope this will help you.


Not quite what I need. is currently hardcoded to C:\, which means the drive letter is specified.

What I want is a way to create a folder at the root level, e.g C:\Test or D:\Test, without specifying the drive letter.
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi,
Pass the drive where you want to create as a command line argument and then pass it to the File constructor.



Regards
Nik
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi Aaron,

call the static method as:
String path = System.getProperty("user.dir");

in path is the directory from where the application has started (where the class is).
Example:


The list in the output should contain the new folder. Perhaps "root" is not appropriate, because I don't think that it is really a root.

Yours,
Bu.


all events occur in real time
Sherlock Yao
Greenhorn

Joined: May 26, 2007
Posts: 8
I think you can try this to get the current drive:

File tempFile = new File(".");
String driveName = tempFile .getAbsolutePath().substring(0,2);


<i>Sorry for my poor English as I am a Chinese guy</i>
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
File f = new File("/Test");
f.mkdir();

should work, shouldn't it?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
ok ilja !!
i tried this it works the folder gets created in E drive.
Aaron John
Ranch Hand

Joined: May 30, 2005
Posts: 74
Originally posted by Ilja Preuss:
File f = new File("/Test");
f.mkdir();

should work, shouldn't it?


Thanks Ilja, your suggestion was what I was after
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Creating a folder in C drive root