• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

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
0
Chakri25
Asked:
Chakri25
  • 8
  • 4
  • 3
  • +1
1 Solution
 
CEHJCommented:
You can use forward slashes in any OS. Just don't use drive letters
0
 
CEHJCommented:
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);
0
 
Chakri25Author Commented:
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Chakri25Author Commented:
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
0
 
CEHJCommented:
>>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
0
 
Chakri25Author Commented:
what u mean by this pls
The file output.txt should be in the root of your classpath if you do that

thanks
0
 
CEHJCommented:
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
0
 
objectsCommented:
> 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.
0
 
Jim CakalicSenior Developer/ArchitectCommented:
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.
0
 
CEHJCommented:
I would put it into a classpath root if it doesn't have a dedicated folder

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

should load it fine
0
 
objectsCommented:
> 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).
0
 
CEHJCommented:
The statement would be proved wrong by definition if the code i posted worked wouldn't it? ;-)
0
 
CEHJCommented:
:-)
0
 
Jim CakalicSenior Developer/ArchitectCommented:
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
0
 
objectsCommented:
> 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).
0
 
CEHJCommented:
LOL
0
 
objectsCommented:
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.
0
 
Jim CakalicSenior Developer/ArchitectCommented:
OK :-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now