2

In my program (simple plain cosole app) I am reading a file a.txt. Now I will be giving the program to someone else and he should be able to run it. I don't want the file path to be fixed like D:\a.txt , instead it should be relative to my program. Where should I place the file so that my program always finds it?

File file = new File("D:\aks.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while( (str= bufferedReader.readLine())!=null){

}

My code is working fine when I hard code the path like D:\a.txt

James Dunn
  • 8,064
  • 13
  • 53
  • 87
ako
  • 2,511
  • 3
  • 18
  • 11

1 Answers1

1

Put the file in the classpath (e.g. the package root or in a certain package) and just get it straight from the classpath as follows

InputStream input = getClass().getResourceAsStream("/a.txt");
// ... (continue with InputStreamReader and so on)

(the exact path depends on the location of the current class and whether you prefix with / to start from package root and which classloader you're using)

Package and distribute it as a single executabele JAR file.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @BalusC. Can you elaborate more on it?My file reading logic is given in my post. – ako May 23 '11 at 04:09
  • What part exactly don't you understand? Decorating an `InputStream` with a `BufferedReader`? Just so: `reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));` where `UTF-8` is the charset the file is saved in (can be omitted, but that's a bad idea, also with `FileReader` already ...) – BalusC May 23 '11 at 04:09
  • I want to give source code as a eclipse projec not as a jar.Please suggest – ako May 23 '11 at 04:10
  • Just do the same (put that file in the classpath), but then give it as Eclipse project instead :) Packaging as JAR is not necessary, it just prevents the enduser (a non-Java-developer) from fiddling with two or more loose files which may make things more worse. – BalusC May 23 '11 at 04:11
  • am using this File file = new File("D:\aks.txt"); FileReader fileReader = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(fileReader); – ako May 23 '11 at 04:15
  • Yes, I know. What part of the answer don't you understand? It seems pretty simple to me. To start, put `aks.txt` in Eclipse in the same package as your Java class is sitting in (that's the classpath already). Then get it from there by `getClass().getResourceAsStream("aks.txt")`. That's all. Did you ever *Try* it? Why not? – BalusC May 23 '11 at 04:16
  • I am not sure what this has to do with [a Mesopotamian town](http://en.wikipedia.org/wiki/Ur). Please write proper English so that non-native English speakers also understand you. – BalusC May 23 '11 at 04:17
  • I used this code:InputStream inputSteam = getClass().getResourceAsStream("nasaIp.txt"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputSteam)); and am getting error – ako May 23 '11 at 04:27
  • @BalusC: I have placed my input file under src folder of eclipse – ako May 23 '11 at 04:28
  • Then it's apparently not in the same package as the class where you're running this code. If the text file is in the package root (the src folder), then you need to locate it by `"/nasalp.txt"` instead (or `"/aks.txt"` or whatever one you think/said it is). If still in vain, please share the error. They are not for decoration. They tell something about the cause of the problem. – BalusC May 23 '11 at 04:28
  • It worked.Nowi placed the file under src folder and used path /aks.txt – ako May 23 '11 at 04:38