6

I am to attempting to create directory and write data into that file. I am trying to use java nio to write file for more efficiently. My doubt is how to close it after write it below is my code. Please advice me.

And is this correct way to create directory and write large data files [200 kb].

    java.nio.file.Path path = java.nio.file.Paths.get(filePath);

    try {
        if (!java.nio.file.Files.exists(path)) {
            java.nio.file.Files.createDirectories(path.getParent());
            path = java.nio.file.Files.createFile(path);
            path = java.nio.file.Files.write(path, data.getBytes());
        } else {
            java.nio.file.Files.write(path, data.getBytes("utf-8"), 
                    java.nio.file.StandardOpenOption.CREATE, java.nio.file.StandardOpenOption.TRUNCATE_EXISTING);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
    }
1
  • 1
    Instead of Files.write, with Java 11 you can just do it using Files.writeString which saves you having to call getBytes. Commented Feb 7, 2019 at 12:01

3 Answers 3

6
  1. Closing file

You do not have to close it as the write method itself ensurer's that file will be closed once last byte is written.

As per description of Files.write() method.

Writes bytes to a file. The options parameter specifies how the the file is created or opened. If no options are present then this method works as if the CREATE, TRUNCATE_EXISTING, and WRITE options are present. In other words, it opens the file for writing, creating the file if it doesn't exist, or initially truncating an existing regular-file to a size of 0. All bytes in the byte array are written to the file. The method ensures that the file is closed when all bytes have been written (or an I/O error or other runtime exception is thrown). If an I/O error occurs then it may do so after the file has created or truncated, or after some bytes have been written to the file.

  1. Correct way to create directory and write large data files.

It's fine as long as you handle all exception conditions.

2
  • @ Umesh. Can we able to pass File object to write using nio.
    – deadend
    Commented Jul 8, 2017 at 9:00
  • no, there are no such method provided, but you can always write yours Commented Jul 8, 2017 at 9:08
3

The Files.write(...) method "...ensures that the file is closed when all bytes have been written ..", see Java SE 8 Doc

For writing data into a file see Reading, Writing, and Creating Files

2
2

If you are using JDK 1.7 onwards, use try-with-resource it will automatically take care of closing of the resource. Eg. try(initialize your file resource here){} Read more about try-with-resource here

2
  • @ Anil, can we able to use java nio in try-with-resource, i tried link. but i did not get.
    – deadend
    Commented Jul 8, 2017 at 8:02
  • @deadend there is even an example given of java nio of the give java doc page, please go through that. Commented Jul 8, 2017 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.