File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes working with files and directories Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "working with files and directories" Watch "working with files and directories" New topic
Author

working with files and directories

sudha javvadi
Ranch Hand

Joined: Mar 23, 2007
Posts: 97
Hi All,
I am new to java and new to this group. I am planning to take scjp 5 exam shortly. I am going thru K&B book. I have a doubt in chapter 6. When i typed the code:

File myDir=new File("mydir");
myDir.mkdir();

I couldn't see any actual directory.The code compiled and i can see the .class file but not the actual directory created. Do i need to specify any path?? pls suggest me.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Sudha,


File myDir=new File("mydir");
myDir.mkdir();

I couldn't see any actual directory.The code compiled and i can see the .class file but not the actual directory created. Do i need to specify any path?? pls suggest me.


Didn't you run the class file using java command
>java classname
Only compiling wont do, the directory will be created at run time, when you launch your application.
The directory will be created on your current directory because no path you have included there!


Hope this helps you!

With Regards,
cmbhatt


cmbhatt
sudha javvadi
Ranch Hand

Joined: Mar 23, 2007
Posts: 97
Thank you! Chandra.Actually i did something more to that code.

import java.io.*;
class Testdir
{
public static void main(String args[])
{
try
{
File mydir = new File("mydir1");
mydir.mkdir();

File myfile = new File(mydir,"myfile1.txt");
PrintWriter pw = new PrintWriter(myfile);
pw.println("Scjp 1.5");
pw.println("is a tough exam");
pw.flush();
pw.close();

File file2 = new File(mydir,"myfile2.txt");
file2.createNewFile();

FileReader fr = new FileReader(myfile);
BufferedReader br= new BufferedReader(fr);
String str;
while((str=br.readLine()) !=null)
System.out.println(str);
fr.close();
}
catch(IOException ex)
{
System.out.println("Exception:" +ex);
}
}
}

After compiling and running the code above i got result as: Not equal.
Where did that "not equal" come from and i couldn't see any actual directory. I think i did something wrong with that code.Pls help.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hey Sudha,

Your program is perfectly OK!
I think you are running any other program whose name is something similar to your this class name. Check your current directory, where this .java file is saved, you will see a directory "dir1" and a file inside that in which you have written "SCJP is tough exam".

There is no question of printing Not equal so far as you this class file is concerned. Find any other program you may have created to check String equality for example.


Tell me what happened!
Waiting...


Regards,
cmbhatt
sudha javvadi
Ranch Hand

Joined: Mar 23, 2007
Posts: 97
Hi Chandra,

I don't know how i got that "not equal" result yesterday night. Now i tried it again and everything is fine. Thankyou!
sudha javvadi
Ranch Hand

Joined: Mar 23, 2007
Posts: 97
Hi Chandra,

I don't know how i got that "not equal" result yesterday night. Now i tried it again and everything is fine. Thankyou!
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Sudha,

I suspect, yesterday night a ghost corrupted your JVM! I feel awesome.

Anyways now IT is out of your system.



Regards,
cmbhatt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: working with files and directories