Issue
How to open a file that is not present in the current directory but in another directory. For example, I have a folder F:/test and my file is in F:/test/test2/doit.txt and D:/test3/doit2.txt
What to enter in path in parameter while making File object as follows :
File f = new File("/test2/doit.txt");
Solution
Irrespective of which operating system, a file for example, demo.txt can be accessed like
File file = new File("/d:/user/demo.txt");
in Windows where the file is at D:\user\
and
File file = new File("/usr/demo.txt");
in *nix
or *nux
where the file is at /usr/
Also, a file if wanted to be accessed relatively can be done as (considering the Windows example) :
Suppose I am in the songs directory in D: like:
D:/
|
|---songs/
| |
| |---Main.java
|
|---user/
|
|---demo.txt
and the code is inside Main.java, then the following code works.
File file = new File("../user/demo.txt");
Answered By - TechSpellBound Answer Checked By - Marilyn (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.