• Post Reply Bookmark Topic Watch Topic
  • New Topic

help me  RSS feed

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;
public class rat
{
public static void main(String args[])
{
File f = new File("A:book/chapter1");
if(f.exists())
System.out.println("true");
if(f.isDirectory())
System.out.println("true");
f.getName();
}
}
I have craeted a folder book/chapter1 in A:drive and except that
it should display something in the output.But it is displaying me anything
 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you are using Win of M$,
replay
"File f = new File("A:book/chapter1");"
with
"File f = new File("A:\\book\\chapter1");"
and try.
 
shabbir zakir
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!
Thanks nash. But nothing is happening.When i run thhe program nothing is displayed
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After creating the file object in memory , to create the file object in the actual file system you have to create it using the create or createfile method( I forget the exact method name.....)of the file class. Then only would the actual file or dir. come into existance.The creation of the file object in memory does not automatically lead to creation of the actual file on the hard disk.
try this ....
import java.io.*;
public class rat
{
public static void main(String args[])
{
File f = new File("A:book/chapter1");
f.createFile(); // or f.create();
if(f.exists())
System.out.println("true");
if(f.isDirectory())
System.out.println("true");
f.getName();
}
}

 
nan sh
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created c:\book\chapter1 and run the following code,
it produced output as;

C:\CLASSES>java rat
true
true
C:\CLASSES>
import java.io.*;
public class rat
{
public static void main(String args[])
{
File f = new File("c:\\book\\chapter1");
if(f.exists())
System.out.println("true");
if(f.isDirectory())
System.out.println("true");
f.getName();
}
}
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
Just for your information the file separator makes no difference when working with the File class. The File class replaces any separator with the correct one. Therefore:
'\', '\\', and '/' are all acceptable on any OS!
Regards,
Manfred.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manfread
I tried the program by nan.
if i replace
File f = new File("c:\\book\\chapter1"); with
File f = new File("c:\book\chapter1");
it gives me a compiler error ,does it mean on windows we
have to use "\\" instead "\".
Chaitali
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chaitali,
If you are stuck on using '\' instead of good old Unix '/' then on Windows you must escape it with another '\'. So you are correct in using '\\' if you use backslashes instead of forward slashes. I prefer less typing and continue to use forward slash: '/'.
Manfred.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!