By dave | October 23, 2013

Hot on the heals of the last article, interested in what other goodies may be in the new file IO package, and wanting to try the new catch block, I cooked up another example.

In this example, I create a Path object for an example directory, into which I then create a file and write some text into the file. Some things of note here:

  • We do not need to concern ourselves with closing the writer objects as they are opened in the new auto-close section.
  • We can convert between Path objects and File objects using toFile on a Path.
  • The Files singleton contains many methods for manipulating files and directories.

 

package com.thecoderscorner.niostuff;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;

public class NioFileOperations {

    public static final String EXAMPLE_DIRECTORY_PATH = "c:\\Dev\\test";

    public static void main(String[] args) throws IOException {
        // get the example directory as a path object using nio.
        Path dir = Paths.get(EXAMPLE_DIRECTORY_PATH);

        // convert to an old File object
        File oldFile = dir.toFile();
        System.out.println("Old File: " + oldFile.getAbsolutePath());

        // create a file newFile.txt using the Files singleton
        // under the example directory
        Path newFile = dir.resolve("newFile.txt");
        Files.createFile(newFile);

        // now we open new file for writing and put a few words in there,
        // notice that we don't have a block finally, because the Java 7
        // try now provides an auto close block at the top.
        try (
            // any resources (must implement AutoCloseable) in here
            // will automatically be closed on exiting the try block.
            FileWriter writer = new FileWriter(newFile.toFile());
            PrintWriter printWriter = new PrintWriter(writer)
        ) {
            // now write out to the file
            printWriter.println("Written to new file using the java7");
            printWriter.println("try block that will auto-close any");
            printWriter.println("resources in the brackets after try");
            printWriter.println("Created at: " + new Date() );
            // no need to close, it will auto close
        }
        catch(IOException e) {
            // just need to deal with any errors.
            e.printStackTrace();
        }


    }
}

When run it will create a file in the example directory, which you'll need to delete if it runs more than once. If the file already exists you'll get an exception. Below is an example of the created file

Written to new file using the java7
try block that will auto-close any
resources in the brackets after try
Created at: Wed Oct 23 22:27:41 BST 2013

and to standard out

Old File: c:\Dev\test

Process finished with exit code 0

Other pages within this category

comments powered by Disqus

This site uses cookies to analyse traffic, and to record consent. We also embed Twitter, Youtube and Disqus content on some pages, these companies have their own privacy policies.

Our privacy policy applies to all pages on our site

Should you need further guidance on how to proceed: External link for information about cookie management.

Send a message
X

Please use the forum for help with UI & libraries.

This message will be securely transmitted to our servers.