Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IO question

 
Wai Iu
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will be the output? Consider a directory structure like
this (NT or 95) C:\JAVA\12345.msg --FILE
//CODE_A
import java.io.*;
public class IO {
public static void main(String args[])
{
File f1 = new File("\\12345.msg");
System.out.println(f1.getPath());
System.out.println(f1.getParent());
System.out.println(f1.isAbsolute());
System.out.println(f1.getName());
System.out.println(f1.exists());
System.out.println(f1.isFile());
}
}
The result will be
\12345.msg
\
true
12345.msg
false
false
If the line File f1 = new File("\\12345.msg")in the above code
is replaced with File f1 = new File("12345.msg"), the result
will be
12345.msg
null
false
12345.msg
true
true
Moreover, if the line File f1 = new File("\\12345.msg")in the
above code is replaced with File f1 = new File("..\\12345.msg"),
the result will be
..\123345.msg
..
false
12345.msg
false
false
What is the deifference between "..\\12345.msg", "12345.msg",
and "\\12345.msg"? which one stands for file? which one stands
for path? I am quite confusing about these concepts in File
class? Please help me.
Thanks!
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's pretend you are running your code from
C:\myStuff\myMessageHere
Then,
"12345.msg" looks for file C:\myStuff\myMessageHere\12345.msg
"\\12345.msg" looks for file C:\12345.msg
"..\\12345.msg" looks for file C:\myStuff\12345.msg
In other word,
1/ file 12345.msg in the current directory
2/ file \12345.msg at the root directory of your system
3/ file ..\12345.msg in the next parent directory
Hope it helps,
JRoch
 
Wai Iu
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Jroch!
Your comments are really help me understand File class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic