2

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();
        }
   }
}  
Preety
  • 21
  • 2
  • 1
    I believe that `FileWriter` will just go ahead and create the file if it does not already exist. So either way, you will see that file being written to. – Tim Biegeleisen Aug 11 '17 at 06:27
  • I do not think it is duplicate. While creating the FileWriter object file does exist. However, I am deleting it after FileWriter object gets created. Once file gets deleted BufferedWriter.write("hello") line gets executed. Just want to know why BufferedWriter.write is not throwing IOException when file does not exist while writing. – Preety Aug 11 '17 at 06:34
  • Because it creates the file if it does not exist, that's why. – Tim Biegeleisen Aug 11 '17 at 06:41
  • Hi Tim, Pls do look at my code. At the end of the program execution , I do not see the file. – Preety Aug 11 '17 at 06:46
  • Are you sure that you are looking in the right place for the file? – Tim Biegeleisen Aug 11 '17 at 06:47
  • 1
    Voting to reopen. Not a valid duplicate. Behavior is as OP describes: no `IOException` is thrown if the file is trashed or even hard-deleted after its creation. It is also not automatically re-created after deletion. – Robby Cornelissen Aug 11 '17 at 06:48
  • @RobbyCornelissen Did you read the duplicate link? You won't get an exception if the file isn't present. Rather, it will create the file. – Tim Biegeleisen Aug 11 '17 at 06:50
  • 1
    @TimBiegeleisen Yes I have read the link. That's not the point. OP describes a scenario in which he deletes the file after its creation and then expects an exception upon writing (or at least flushing) to the deleted file. I have tried above code on the latest Ubuntu LTS and no such exception occurs. – Robby Cornelissen Aug 11 '17 at 06:52
  • Hi Tim, it does not create the file. I checked the path from where I am executing the program also I checked tmp directory. Where You want me to look – Preety Aug 11 '17 at 06:53
  • Agreed with @RobbyCornelissen – Preety Aug 11 '17 at 06:55
  • @TimBiegeleisen It might well be, and if you can ferret out the Javadoc in which this behavior is described, you can answer this question and OP will maybe even accept it. Until then, kindly un-wield your duplicate hammer and reopen the question. – Robby Cornelissen Aug 11 '17 at 07:03
  • 1
    @RobbyCornelissen You sunk my battleship! That deserves a random upvote +1 – Tim Biegeleisen Aug 11 '17 at 07:15
  • @TimBiegeleisen Awesome! Thanks :) – Robby Cornelissen Aug 11 '17 at 07:25

1 Answers1

1

If you were running on Windows, Windows would not let you delete the open file.

Ergo you aren't running on Windows. The behaviour on Unix and brethren having deleted the file is that the file continues to exist but doesn't have any names (i.e. you have unlinked the name rather than deleting the file). You can keep writing to it until the disk fills up, or hell freezes over, without getting any errors. However you can never read the file, and as soon as the last opener closes it the space used is reclaimed.

Nothing to do with BufferedWriter, or FileWriter, or FileOutputStream, or Java.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • @EJK Hi EJK, so you mean to say here, that when we delete the file in unix using "rm" command, file continue to exist but does not have name. I deleted the file using 'rm' command. That file was not symbolic or hardlink file. It was regular file. Can you point me to the document where it says ' having deleted the file is that the file continues to exist but doesn't have any names – Preety Aug 11 '17 at 21:17
  • That's not what I 'meant' to say. That's what I **said.** This is how UNIX file systems work. – user207421 Aug 12 '17 at 01:38