• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

working with files and directories

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
sudha javvadi
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sudha,

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

Anyways now IT is out of your system.



Regards,
cmbhatt
 
reply
    Bookmark Topic Watch Topic
  • New Topic