I’m trying to read a file in eclipse and print it. The problem is that the compiler always says to me that the file or directory doesn’t exist. I have to use relative paths.
The relevant part of the project routes is:
- uva.pfc.refactoringEngine.core <–Project
- …
- src
- uva.pfc.refactoringengine.core.actions <– Actual Package
- …
- CreateEnumSetPlusClas.java <–File from I want to read the EnumSetPlus.java file
- …
- EnumSetPlus.java <– File I want read and print
- uva.pfc.refactoringengine.core.actions <– Actual Package
This is the code:
String total="";
File actual = new File("src/EnumSetPlus.java");
FileReader filereader = null;
try {
filereader = new FileReader(actual);
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block e.printStackTrace();
}
BufferedReader input = new BufferedReader(filereader);
try {
while ((line = input.readLine()) != null)
{
total += line + "\n";
}
input.close();
}
catch (IOException e) {
// TODO Auto-generated catch block e.printStackTrace();
}
System.out.println(total);
I think the problem is that I have to do something if I want the file path recognised by de eclipse project.
Could you help me??
Thaks beforehand.
I’d use
getClass().getResourceAsStream("/EnumSetPlus.txt")– this will look for the file on the root of the classpath (which isbin/, but all files from src go to bin). You then get anInputStreamwhich you can adapt toRedaervianew InputStreamReader(stream, encoding)