1

i have tried something like below to write a file, read it and delete. is this a best way ?

public class WriteFileExample {

private void createFile(String filename){
        FileOutputStream fop = null;
        File file;
        String content = "This is the text content";

        try {

            file = new File(filename);
            fop = new FileOutputStream(file);

            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            // get the content in bytes
            byte[] contentInBytes = content.getBytes();

            fop.write(contentInBytes);
            fop.flush();
            fop.close();

            System.out.println("Done");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fop != null) {
                    fop.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

}

private void readAndDeleteFile(String filename){
   try {
            DeleteOnCloseFileInputStream  fis = new DeleteOnCloseFileInputStream(new File(filename));

            System.out.println("Total file size to read (in bytes) : "
                    + fis.available());

            int content;
            while ((content = fis.read()) != -1) {
                // convert to char and display it
                System.out.print((char) content);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        WriteFileExample  exe = new WriteFileExample ();

        exe.createFile("c:/mytestfile.txt");
        readAndDeleteFile("c:/mytestfile.txt");
    }
}

Second class extending FileInput stream

public class DeleteOnCloseFileInputStream extends FileInputStream {
   private File file;
   public DeleteOnCloseFileInputStream(String fileName) throws FileNotFoundException{
      this(new File(fileName));
   }
   public DeleteOnCloseFileInputStream(File file) throws FileNotFoundException{
      super(file);
      this.file = file;
   }

   public void close() throws IOException {
       try {
          super.close();
       } finally {
          if(file != null) {
             file.delete();
             file = null;
         }
       }
   }
}
buttowski
  • 4,657
  • 8
  • 27
  • 33
  • at the time of you try to delete the file why do you have to read it into the InputStream? – Hunter Zhao Apr 03 '13 at 06:20
  • fop.flush() is redundant because fop.close() will anyway flush the stream. Using BufferedWriter or BufferedReader for writing or reading from stream is a better option because of performance issues. – Kishore Apr 03 '13 at 06:32
  • This question should be post [here](http://codereview.stackexchange.com/users/23081/subodh) – subodh Apr 03 '13 at 06:35
  • i want to delete the file once i complete reading the file. i just want to use the file i create as a temp file. – buttowski Apr 03 '13 at 09:00

1 Answers1

2

when you read the file, use buffer to get higher efficiency.

InputStream is = BufferedInputStream(new FileInputStream(new File(fileName)));

Hope to help you.

Hunter Zhao
  • 4,589
  • 2
  • 25
  • 39