Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to interact with file system  RSS feed

 
Harish Kashyap
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all
while trying to create a file in the local file system through java application ,i was unable to find a way.
because i wanna create a directory through java and inside that directory i wanna create a file. can any body help me creating a directory in the local file system through java.
thanx
harish
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To create a directory you can use the mkdir() or mkdirs() of File class. These methods are available starting with jdk 1.2.
 
Pragya Prakash
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code to create a directory & to create a new file within the directory.





import java.io.*;
public class makeDir {
public static void main(String args[])
{
// Create a file object, pass the desired directory path(or name) as a parameter to the constructor
File f = new File("c:/Temp2");
//Will Create a directory, if it doesn't already exist
f.mkdir();
/* Create a new file object, pass the desired File path(or name) as a parameter to the constructor
This is required as the File object doesn't have any method to change directories.
*/
File f2 = new File("c:/Temp2/temp.txt");
try{
//Will create a new file, if it doesn't already exist
f2.createNewFile();
}
catch(IOException ie)
{
System.out.println("Error !");
}
}
}





Let me know, if you have any further doubts
Regards,
Pragya

[This message has been edited by Pragya Prakash (edited September 18, 2000).]
[This message has been edited by Pragya Prakash (edited September 18, 2000).]
[This message has been edited by Pragya Prakash (edited September 18, 2000).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!