The Java Tutorials' Weblog

pageicon Wednesday Apr 01, 2009

Converting Pre-JDK7 File I/O Code

Prior to JDK7, the java.io.File class was the mechanism used for file I/O, but it had several drawbacks:

  • Many methods didn't throw exceptions when they failed, so it was impossible to get a useful error message. For example, if a file delete failed, the program would receive a "delete fail" but wouldn't know if it was because the file didn't exist, the user didn't have permissions, or if there was some other problem.

  • The rename method didn't work consistently across platforms.

  • There was no real support for symbolic links.

  • More support for metadata was desired, such as file permissions, file owner and other security attributes.

  • Accessing file metadata was inefficient.

  • Many of the File methods didn't scale. Requesting a large directory listing over a server could result in a hang. Large directories could also cause memory resource problems, resulting in a denial of service.

Perhaps you have legacy code that uses java.io.File and would like to take advantage of the java.nio.file.Path functionality with minimal impact on your code.

The java.io.File class provides the toPath method that converts an oldstyle File instance to a java.nio.file.Path instance:

Path input = file.toPath();

You can then take advantage of the rich feature set available to the Path class.

For example, say you had a some code that deleted a file:

file.delete();

You can modify this code to use the Path.delete method like this:

Path fp = file.toPath();
fp.delete();

That's all you need to do!

However, if you want to take advantage of the new Path feature set, you can examine the exception to learn why the delete failed:

Path fp = file.toPath();
try {
    fp.delete();
} catch (NoSuchFileException x) {
    System.out.format("%s: no such file or directory%n", fp);
} catch (DirectoryNotEmptyException x) {
    System.out.format("%s not empty%n", fp);
} catch (IOException x) {
    //File permission problems will be caught here.
    System.out.format("%s%n", x);
}

-- Sharon Zakhour

Comments:

Yes, the toPath method allows for local changes to workaround the many many problems of java.io.File. A few minor comments on the example: (i) Less folks get the wrong impression, one doesn't have to catch all these exceptions. Rather the delete method throws the checked IOException with the others being specialized IOExceptions for cases where code wants to take action for specific error cases. (ii) The SecurityException is an unchecked exception thrown when the security manager denied delete access. This shouldn't be confused with a permission problem on the file system. There is a specific IOException for that if you want. (iii) delete(true) is the same as delete(). The reason for the boolean parameter is to allow application indicate if they care if the file exists or not. This is important for cases where you have multiple threads or VMs attempting to delete the same file.

Posted by Alan on April 02, 2009 at 12:58 AM PDT #

very nice... thank you for ur time

Posted by birecik on May 06, 2009 at 09:13 PM PDT #

thankss for ur subject.. very nice

Posted by babalar günü on May 23, 2009 at 06:48 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

« November 2009
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
12
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
     
       
Today

Feeds

Search this blog

Links

Weblog menu

Today's referrers

Today's Page Hits: 195