I have a more complex program that involves using what’s written in a text file but it wasn’t working at all, nothing was being displayed and everything is dependent on the file. If i just make the string equal to something in my code and get rid of reading from the file then it works. I tried something as simple as displaying the line that is read but it doesn’t display anything when i run it.
Edit: It can’t find the path to the file… where is the default file usually in? The file is in the same directory as the program but it can’t find it, why?
public static void main(String[] args) {
String s;
try {
FileReader fstream=new FileReader("input.txt");
BufferedReader in=new BufferedReader(fstream);
while((s=in.readLine())!=null){
System.out.print(s);
}}catch(IOException e){
System.exit(0);
}
}
}
Don’t ignore exceptions. If nothing is read, it’s probably that an exception is thrown. Instead of letting it bubble and telling you what’s wrong, you catch it and exit silently. That’s like buying a fire alarm and setting its sound volume to 0: you’ll never know there is a fire.
Transform your program to
and see what happens.
Also, I removed the call to readLine() inside the loop. The line has already been read at this point.