The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
February 2002

Advertisement

Advertisement

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.

Message:

Detecting a deleted file while writing to it

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

Hi

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;
import java.io.*;

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

try
{
BufferedReader br = new BufferedReader(new FileReader(FileDescriptor.in));
BufferedWriter bw = new BufferedWriter(new FileWriter("/tmp/a"));
// Writer bw = new ileWritefileName);

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





Replies:

Sponsored Links



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