The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
February 2002



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Detecting a deleted file while writing to it

Posted by Bob on February 13, 2002 at 11:24 AM


I'd like to determine if a file is deleted/renamed while I'm writing to it without opening and closing it for each write call.
The simple code example below deminstrates my problem.
The code snippet simply writes the lines it reads from standar in to a file. If this file is deleted while it was being written to, I would have hoped to get some kind of exception, but I don't, and this is the problem. How can the program that has opened the file and is writing to it detect that the file is no longer there?

package bobtest;

public class writeTest
public static void main(String args[])
String rawLine;
System.out.println("Waiting for input");

BufferedReader br = new BufferedReader(new FileReader(;
BufferedWriter bw = new BufferedWriter(new FileWriter("/tmp/a"));
// Writer bw = new ileWritefileName);

while ((rawLine = br.readLine()) != null)
bw.write(rawLine + "\n");
catch (IOException ioe)
{ System.out.println(ioe.toString()); }
catch (Exception e)
{ System.out.println(e.toString()); }


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us