• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading file both with absolute path and relative path  RSS feed

 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I need to read a file which will work both for absolute path and relative path and independent of OS(Linux,Windows,Unix).
Please help.
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, what exactly is the problem? Java can handle absolute and relative paths just fine, doesn't matter on what OS your program is running, and you don't have anything special to do for it.
 
Kousik Majumder
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which API should I use for that?
Can you give some code snippet.
 
Ranch Hand
Posts: 45
Fedora Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need any special APIs for this
in Java default OS File separator can be obtained by
File.Seprator
(i.e. whatever /,\,//,$,,:| )
so, for example if you are specifying file "sample.txt" for reading
you may write
File.Seprator+parentdirectory2+File.Seprator+parentdirectory1+File.Seprator+parentdirecory0+File.Seprator+sample.txt

for absolute path
and
parentdirecory0+File.Seprator+sample.txt

for relative path

Note the appearance of "File.Seprator" , and some one please put this post to "Beginning Java" section

Cheers,
Rudradutt Joshi
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kousik Majumder wrote:Which API should I use for that?
Can you give some code snippet.

Do you mean which API you should use to read a file? That depends on exactly what you want to do; is it a text file, that you want to read line by line, or is it a file with binary data?

The classes you'd normally use to read files are in the package java.io: FileInputStream, FileReader, BufferedReader etc.
 
Kousik Majumder
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am going to read both properties files and xml files and these files may stay either in my application(.ear) or outside application(in a zipped file in a different directory, may be in separate jboss partition).
So I need to write a code which will read files at any circumstances.
Using API's like FileInputStream, FileReader, BufferedReader is a good practice or any other API like ResourceBundle,getResourceAsStream are good for this kind of scenario?

Please advice.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, using getResourceAsStream is useful for loading any kind of files that can be found through the classpath, and makes it easy to load files that are inside JAR files.
 
Ranch Hand
Posts: 35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think you should expose one static function in your app that takes the path ... then it tries to read using java.io.File etc and if it fails to find it then it falls back on getResourceAsStream (or the other way around if most of the times the files are going to be inside a jar)

if the file can be inside a zip etc from where your classes are NOT loaded then you will need the zip file path and the entry inside the zip and use the classes under java.util.zip
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!