• Post Reply Bookmark Topic Watch Topic
  • New Topic

reading a file without mentioning the path  RSS feed

 
Sheetal Malhotra
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hii...Is there a way to read a file by only mentioning its filename without the entire path where the file is located...
Actually I created a java code which takes an input from a Input.in file. It generates an output in Output.out file. Now to read the input file I used BufferedReader and FileReader and to generate the output file I used PrintWriter, so I used the path and filename to read the input file and generate the output file. When I ran this code it worked in the same way as 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.

Thanks so much in Advance.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do that if the file is available in the classpath.
Either of the following can be used based on the environment:

  • getResource() or getResourceAsStream() in Class
  • getResource() or getResourceAsStream() in ClassLoader


  • What is the difference between the two?
    The methods in class expects the file to be in the same directory structure as the class' package name. (The directory struture root must be in the classpath)
    On the other hand, the methods in ClassLoader expects the file to be directly under the paths specified in the classpath.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!