I have a piece code which retries 3 more times to open a file (see below). Are there potential problems or side effects with the way it is written? How can it be improved? Also, why would one use Thread.currentThread().sleep(x)
instead of simply Thread.sleep(x)
? Any other code smell exists here?
import java.io.*;
public class MyTest{
public static void main(String[] args) {
int retryCount = -1;
String line = null;
do {
try {
BufferedReader br = new BufferedReader(
// try to open a file or stream (may not exist)
new InputStreamReader(new FileInputStream("input.txt")));
line = br.readLine();
br.close();
}
catch (IOException e) {
try{
// wait a few moment to see if the file or stream will be available soon
System.out.println("IOException occurred.");
Thread.currentThread().sleep(300);
}catch(InterruptedException ie){
ie.printStackTrace();
}
if (retryCount == -1) {
retryCount = 3;
} else {
retryCount--;
}
}
} while (retryCount > 0);
}
}