| Author |
Reading file both with absolute path and relative path
|
Kousik Majumder
Ranch Hand
Joined: Sep 30, 2007
Posts: 219
|
|
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.
|
Thanks in Advance,
Kousik
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12911
|
|
|
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.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Kousik Majumder
Ranch Hand
Joined: Sep 30, 2007
Posts: 219
|
|
Which API should I use for that?
Can you give some code snippet.
|
 |
Rudradutt Joshi
Ranch Hand
Joined: Dec 06, 2008
Posts: 45
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12911
|
|
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
Joined: Sep 30, 2007
Posts: 219
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12911
|
|
|
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.
|
 |
Tushar Kapila
Ranch Hand
Joined: Dec 23, 2007
Posts: 35
|
|
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
|
http://thehungersite.com | http://www.worldcommunitygrid.org/
|
 |
 |
|
|
subject: Reading file both with absolute path and relative path
|
|
|