9

I have one scenario where I am trying to implement with the Java 7 'try with resource' feature.

My finally block contains an object of BufferedWriter and File, which I want to close using 'try with resource' feature, instead of closing it by calling close method explicitly.

But I checked on net and saw that the File class does not implement the AutoCloseable interface, but BufferedWriter does. So how can I manage this scenario to implement 'try with resource' feature?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
milind
  • 269
  • 5
  • 7
  • 16
  • The File class doesn't have an open() method or a close() method, so why it should be auto-closable is a mystery. It is the BufferedInputStream that needs closing. Not a real question. – user207421 May 02 '13 at 12:44

3 Answers3

5

You don't need to close a File because it's a pure Java object. It basically just holds the name of the file, nothing else (i.e. it does not require any OS resources to construct).

You only need to close your BufferedWriter and that is correctly AutocCloseable.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
5
 try (BufferedWriter br = new BufferedWriter(new FileWriter(path))) 

Use this simply, br will be closed automatically. Eg. http://www.roseindia.net/java/beginners/java-write-to-file.shtml

Vineet Singla
  • 1,609
  • 2
  • 20
  • 34
  • So here you are saying that instead of creating a File object, I should directly pass the path of the file to the FileReader constructor and bypass the creation of File object.Right? – milind May 02 '13 at 11:59
  • Yes, Absolutely, FileWriter provides a constructor like public FileWriter(String fileName)throws IOException Constructs a FileWriter object given a file name. – Vineet Singla May 02 '13 at 12:02
  • See , this sample code :http://www.roseindia.net/java/beginners/java-write-to-file.shtml – Vineet Singla May 02 '13 at 12:05
  • @milind: you can still use a `File` object, if you wish. You don't need to close that at all! – Joachim Sauer May 02 '13 at 12:26
0

You cannot create a BufferedWriter with File only, BufferedWriter requires a Writer, this how it should look like

    try (BufferedWriter w = new BufferedWriter(new FileWriter(new File("file")))) {
        ...
    }

try-with-resources will call close only on BufferedWriter. Unfortunately BufferedWriter API does say that it closes the underlying writer, but in fact it does. As for File it has nothing to do with try-with-resources since it is not Autocloseable.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • How this will help. Are you saying here that if I create the File object the way you have specified then when the control moves out of the try block BufferedWriter, FileWriter as well as File will automatically be closed? – milind May 02 '13 at 12:02
  • @milind: then please don't forget to upvote any answer that helped you solve it and accept the one that helped most. – Joachim Sauer May 02 '13 at 12:27
  • @ Joachim Sauer Ya thanks for reminding. I have upvoted on of the answer that helped me – milind May 02 '13 at 12:42