Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems reading file

 
tom rubbens
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm working in the path c:/info3/be/kahosl
there i have my Test2.class with following code:
package be.kahosl;
...
try {
FileInputStream istream = new FileInputStream("/be/kahosl/test.txt");
java.io.BufferedReader reader = new java.io.BufferedReader(istream);
String line;
while((line = in.readLine()) != null) {
jTextArea1.setText(line);
System.out.println("OK");
}
}
catch (FileNotFoundException e) {
System.out.println("error file");
}
catch (IOException e) {
System.out.println("error");
}
as you c i'm trying to read the file test.txt who is in the same folder as my class and my class is package be.kahosl. I always get: error file... does anyone know what i'm doing wrong?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Tom.
I think the first forward slash in the string representing the path to the file is interpreted as the root of the filesystem. That is an abolute path, but the file is not in c:\be\khsol\ directory.
A BufferedReader needs a Reader for construction. But an InputStream "is" not such class.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although this is probably happening in a SWING GUI of some sort. It really doesn't pertain so Swing at all. So I am moving it to I/O and Streams...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic