I am wondering what would happen if I tried to read files in Java which might be modified by another processes. For example given the pseudocode:
File f = new File("a");
if (f.exists()) {
// A
BufferedReader br = new BufferedReader(new FileReader(f));
// B
String line = "";
while ((line = br.readLine() ) != null ) {
// C
out.println(line);
}
}
What would happen if at those commented places (A/B/C) the file name had been changed by another process? Would it differ if instead the file was removed or replaces by another? Would any of that be affected if different kind od file reading was implemented?
You can, and should, eliminate point A by removing the
exists()test and catchingFileNotFoundException,and once you have the file open it doesn’t matter to you what it’s called, and on some operating systems it is impossible to rename an open file. Also, there is no reason to initialize the ‘line’ variable.