• 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

reading a file without mentioning the whole path

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheetal Malhotra
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The same directory as what?
 
Sheetal Malhotra
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
any examples please
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sheetal Malhotra:
any examples please



David Payne has given a very good example! Please have a look at all the replies!
 
Paper beats rock. Scissors beats tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic