Link to home
Start Free TrialLog in
Avatar of Chakri25
Chakri25

asked on

Reading a txt file in java system independant way

Hi,
i have the following code in java on windows, and right now i'm hard coding the path, how do i make this a system
independant way, i,e be able to read in unix as well, assuming we only give the file name not the path.

File inFile = new File(
                        "V:/s_dev/DP_DEV/Nrk-MP/java/test/META-INF/output.txt");
BufferedReader input = null;
try {
                  input = new BufferedReader(new FileReader(inFile));
String lineFromOutFile = null;
while ((lineFromOutFile = output.readLine()) != null) {
   //read line by line
}


appreciate ur help
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You can use forward slashes in any OS. Just don't use drive letters
To ensure you don't run into permissions problems, you'd be better doing this sort of thing:

final /* (maybe) static */ String FILENAME = "output.txt";
...
File outputFile = new File(System.getProperty("user.home"), FILENAME);
Avatar of Chakri25
Chakri25

ASKER

i want to do something like this
BufferedReader in = new BufferedReader(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("output.txt")))


just give the name of the file, no path location

how i do this
final String FILENAME = "output.txt";
      
            File outputFile = new File(System.getProperty("user.home"), FILENAME);
            System.out.println("get: "+outputFile );
            
only gives this

C:\Documents and Settings\sandursh\output.txt

i want to do something like this i want to do something like this
BufferedReader in = new BufferedReader(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("output.txt")))
 but it gives error
>>InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("output.txt")))

InputStreamReader(this.getClass().getResourceAsStream("output.txt")))

will suffice. The file output.txt should be in the root of your classpath if you do that
what u mean by this pls
The file output.txt should be in the root of your classpath if you do that

thanks
To keep it simple - it should be in the same directory as your class if it's not in a package and in the package root if it is
> The file output.txt should be in the root of your classpath if you do that

Thats incorrect, output.txt would need to be in the same directory as the class making the call.
Really? I thought that it would need to be in the accessible classpath as defined to the ClassLoader that loaded the class from which with request was being made or any parent ClassLoader according to the ClassLoader delegation scheme. Anyway, the statement that it "would need to be in the same directory as your class" doesn't seem right to me.
I would put it into a classpath root if it doesn't have a dedicated folder

getClass().getResourceAsStream("/output.txt");

should load it fine
> Anyway, the statement that it "would need to be in the same directory as your class" doesn't seem right to me.

Why not, surely the directory that contains the class is visible by the classloader (otherwise how would it load the class).
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:-)
I was questioning the phrase "need to be". It certainly _could_ be in the same directory or jar as the class attempting to load it. But it could _also_ be in any directory or jar on the classpath in use by the ClassLoader which loaded the original class or any of it's parent ClassLoaders. The javadoc for for ClassLoader.getResource states "A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code." This statement would be wrong if it were true that the resource "would need to be in the same directory as your class".

Jim
> But it could _also_ be in any directory or jar on the classpath in use by the ClassLoader which loaded the original class or any of it's parent ClassLoaders.

Thats correct, in this scenario it easier to explain the same directory as the class, in contrast to what was previously stated that it should be in the root of the classpath (which is not only incorrect but also suffers from the same issue that you have brought up).
LOL
jim, And "need to be" was more intended to mean it should be in the same directory as the class instead of the root directory. It was not meant to mean that was only place it could be.
OK :-)