• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating a folder in C drive root

 
Aaron John
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sherlock Yao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can try this to get the current drive:

File tempFile = new File(".");
String driveName = tempFile .getAbsolutePath().substring(0,2);
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File f = new File("/Test");
f.mkdir();

should work, shouldn't it?
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok ilja !!
i tried this it works the folder gets created in E drive.
 
Aaron John
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic