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/ja va/test/ME TA-INF/out put.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
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/ja
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
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("u ser.home") , FILENAME);
final /* (maybe) static */ String FILENAME = "output.txt";
...
File outputFile = new File(System.getProperty("u
ASKER
i want to do something like this
BufferedReader in = new BufferedReader(new InputStreamReader(this.get Class().ge tClassLoad er().getRe sourceAsSt ream("outp ut.txt")))
just give the name of the file, no path location
how i do this
BufferedReader in = new BufferedReader(new InputStreamReader(this.get
just give the name of the file, no path location
how i do this
ASKER
final String FILENAME = "output.txt";
File outputFile = new File(System.getProperty("u ser.home") , FILENAME);
System.out.println("get: "+outputFile );
only gives this
C:\Documents and Settings\sandursh\output.t xt
i want to do something like this i want to do something like this
BufferedReader in = new BufferedReader(new InputStreamReader(this.get Class().ge tClassLoad er().getRe sourceAsSt ream("outp ut.txt")))
but it gives error
File outputFile = new File(System.getProperty("u
System.out.println("get: "+outputFile );
only gives this
C:\Documents and Settings\sandursh\output.t
i want to do something like this i want to do something like this
BufferedReader in = new BufferedReader(new InputStreamReader(this.get
but it gives error
>>InputStreamReader(this.g etClass(). getClassLo ader().get ResourceAs Stream("ou tput.txt") ))
InputStreamReader(this.get Class().ge tResourceA sStream("o utput.txt" )))
will suffice. The file output.txt should be in the root of your classpath if you do that
InputStreamReader(this.get
will suffice. The file output.txt should be in the root of your classpath if you do that
ASKER
what u mean by this pls
The file output.txt should be in the root of your classpath if you do that
thanks
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.
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().getResourceAsSt ream("/out put.txt");
should load it fine
getClass().getResourceAsSt
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).
Why not, surely the directory that contains the class is visible by the classloader (otherwise how would it load the class).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:-)
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
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).
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 :-)