Java BufferedWriter.writer(String str) not throwing IOException while writing to file which does not exist.
Here is what I am doing.
Once FileWriter
objects get created (file is present at this time). I put the sleep method for 15 sec. and delete the file manually for testing purpose.Pls look at the code. While executing out.write("hello world"),
it is not throwing IOException. Pls explain me why it does not throw IOException if file does not exist while writing.
It is quite possible that the file may get deleted because of some reason.
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
class Demo {
public static void main(String[] args) throws InterruptedException {
FileWriter fwriter=null;
BufferedWriter out=null;
try{
String fileName=args[0];
File file = new File(fileName);//File exist at this point of time
fwriter = new FileWriter(file);
System.out.println("======Waiting====");
Thread.sleep(10000); //Deleted the file maually
out = new BufferedWriter(fwriter);
out.write("hello world"); //Does not throw IOException even file does not exist.
out.close();
System.out.println("===Done===");
}catch (IOException e) {
System.out.println("io exp.");
e.printStackTrace();
}
}
}