aspose file tools*
The moose likes Beginning Java and the fly likes reading a file without mentioning the whole path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reading a file without mentioning the whole path" Watch "reading a file without mentioning the whole path" New topic
Author

reading a file without mentioning the whole path

Sheetal Malhotra
Greenhorn

Joined: Dec 27, 2007
Posts: 8
Hii is there any possible way to read a file only by its filename without specifying its path where the file is located.
FileReader and FileInputStream need a file located path to read the file.

Thanks.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
File paths can be absolute or relative. If they are absolute, they fully specify where the file is. If they are relative, they specify it in relation to the current working directory (system property "user.dir").

If neither of these suits you, then you have a problem. It is possible to read directories in Java, and hence to search a tree of directories for a file with a particular name. However, if there are a lot of directories to search, it will take a long time.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Sheetal Malhotra
Greenhorn

Joined: Dec 27, 2007
Posts: 8
Hii Peter...yeah if the input file is on the same directory then how can I read the file but just mentioning its filename..
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
The same directory as what?
Sheetal Malhotra
Greenhorn

Joined: Dec 27, 2007
Posts: 8
Well actually I created a java code which takes an input from a .in file. It generates an output to a .out file. Now to read the input file or to generate the output file I am using a path. The code executed in the way I wanted. Now I wanted to execute the same code on a different PC without editing it again. So I was just wondering if there is a way I can directly give the filename instead of giving a path.
Mark Newton
Ranch Hand

Joined: Jan 31, 2006
Posts: 129
OK - so you have a file structure like:

and you want to use MyClass.java to read in the MyFile.in file, and to write to a new MyFile.out file?

In that case, the following should be fine:
Sheetal Malhotra
Greenhorn

Joined: Dec 27, 2007
Posts: 8
Actually I have the input file on my desktop, and I am executing my .java file which is MyFile.java from command prompt. When I execute my MyFile.java file it will generate an output file to my desktop. This is when I use FileReader, BufferedReader to read the input file. I used the PrintWriter to generate the output file. For FileReader, BufferedReader and PrintWriter I am giving the whole path and file name. And, I am getting the output in the way I wanted it.

Now I want to execute the same java file on a different PC which has only the input file somewhere on the Same Drive where java is. That is when I try to execute the code ...I dont want to edit this code again....I will go to the command prompt and say only:
javac MyFile.java
java MyFile
diff MyFile.out test.out

So If I dont edit MyFile.java I will get an error. So I was just wondering if there is a way of using only the input filename when I try to read the input file using MyFile.java, so that if i try to execute on a different PC I will get the corrrect output without changing the code
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18674
    
    8

Peter Chase's very first answer is still the answer. If you don't want to specify the full path to the file then you have to arrange things so the name you provide is relative to the current working directory. That means you have to arrange things so the current working directory is set correctly.
Sheetal Malhotra
Greenhorn

Joined: Dec 27, 2007
Posts: 8
any examples please
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Sheetal Malhotra:
any examples please


David Payne has given a very good example! Please have a look at all the replies!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
wood burning stoves
 
subject: reading a file without mentioning the whole path