mercoledì 12 febbraio 2014

NIO 2

If like many Java developers you struggle each time you have to read or write a file, Java SE 7 came to your rescue by introducing a new IO package: java.nio. With a more expressive syntax, its goal is to replace the existing java.io package to allow:

  • A cleaner exception handling.
  • Full access to the file system with new features (support of specific operating system attributes,  •
  • symbolic links, etc.).
  • The addition of the notion of  FileSystemand FileStore(e.g., a partition disk).
  • Utility methods (move/copy files, read/write binary or text files, path, directories, etc.). 

The following code shows you the new java.nio.file.Pathinterface (used to locate a file or a directory in a file system) as well as the utility class java.nio.file.Files(used to get information about the file or to manipulate it). From Java SE 7 onward it is recommended to use the new NIO.2 even if the old java.io package has not been deprecated.
The following code, gets some information about the source.txt file, copies it to the dest.txt file, displays its content, and deletes it.
Path path = Paths.get("source.txt");

boolean exists = Files.exists(path);

boolean isDirectory = Files.isDirectory(path);

boolean isExecutable = Files.isExecutable(path);

boolean isHidden = Files.isHidden(path);

boolean isReadable = Files.isReadable(path);

boolean isRegularFile = Files.isRegularFile(path);

boolean isWritable = Files.isWritable(path);

long size = Files.size(path);

// Copies a file

Files.copy(Paths.get("source.txt"), Paths.get("dest.txt"));

// Reads a text file

List<String> lines = Files.readAllLines(Paths.get("source.txt"), UTF_8);

for (String line : lines) {

  System.out.println(line);

}

// Deletes a file

Files.delete(path);

Multi-catch exception

Until Java SE 6 the catch block could handle only one type of exception at a time. You therefore had to accumulate several catches to perform a specific action for each type of exception. And as shown in the following code you often have to perform the same action for each exception.
Example: using Several Catch Exception Clauses
try {
// Do something
} catch(SAXExceptione) {
e.printStackTrace();
} catch(IOExceptione) {
e.printStackTrace();
} catch(ParserConfigurationExceptione) {
e.printStackTrace();

}
With Java SE 7 if the handling of each exception is identical, you can add as many exception types as you want, separated by a pipe character as shown in the following code:
Example: Using Multicatch Exception
try {
// Do something
} catch(SAXException | IOException | ParserConfigurationException e) {
e.printStackTrace();

}

Try-with-resources

In several Java APIs, closing resources have to be managed manually, usually by a call to a close method in a finally block. This is the case for resources managed by the operating system such as files, sockets, or JDBC connections.
The following example shows how it is necessary to put the closing code in a finally block with exception handling, which decreases the readability of the code:
@Resource(lookup = "java:/MySQLDS")
private DataSource ds;
. . . .  
String sql = "select * from customer";
List list = new ArrayList();
Connection con =null;
PreparedStatement ps =null;
ResultSet rs =null;
try {
   con = ds.getConnection();
   ps = con.prepareStatement(sql);
 
   rs = ps.executeQuery();
        while (rs.next()) {
            list.add(rs.getInt("id"));
   }
   
} catch (SQLException e) {
    e.printStackTrace();
}
 finally {
    if (rs != null) {
        try {
            rs.close();
        } catch (SQLException e) {  }
    }
    if (ps != null) {
        try {
            ps.close();
        } catch (SQLException e) {  }
    }
    if (conn != null) {
        try {
            conn.close();
        } catch (SQLException e) {  }
    }
}
And here's the same example rewritten to use try-with-resource:
@Resource(lookup = "java:/MySQLDS")
private DataSource ds;
. . . .  
String sql = "select * from customer";
List list = new ArrayList();
try (Connection con = ds.getConnection();
     PreparedStatement ps = con.prepareStatement(sql);) {
 
    try (ResultSet rs = ps.executeQuery();) {
        while (rs.next()) {
            list.add(rs.getInt("id"));
        }
    }
} catch (SQLException e) {
    e.printStackTrace();
}

Diamond

Generics arrived with Java SE 5 with a rather verbose syntax. Java SE 7 brought a slightly lighter notation, called diamond, which does not repeat the declaration in the instantiation of an object. The following code gives an example of declaring generics both with and without the diamond operator.

Example: Declaring Generics with and Without Diamond
// Without diamond operator
List<String> list = new ArrayList<String>();
Map<Reference<Object>, Map<Integer, List<String>>> map =
new HashMap<Reference<Object>, Map<Integer, List<String>>>();

// With diamond operator
List<String> list = new ArrayList<>();
Map<Reference<Object>, Map<Integer, List<String>>> map = new HashMap<>();

String case

Before Java SE 7 only numbers (byte, short, int, long, char) or enumerations could be used in switch cases. It is now possible to use a switch on a Strcompare alphanumerical values. This avoids long lists of if/then/else and makes the code more readable. The following example shows you what you can now write in your applications.

Example: A String Case
String action = "update";

switch (action) {

case "create":
create();
break;

case "read":
read();
break;

case "udpate":
udpate();
break;

case "delete":
delete();
break;

default:
OtherAction(action);
}

venerdì 19 novembre 2010

Java 7 release date

Current expectation for Java7 release is Q4 2010, but you can expect to slip further.

As stated at StackOverFlow, the delay in releasing the new Java 7 is mostly due to conflicts with the Apache foundation. Many members of the JCP executive committees, whose votes are required for JSRs to become final, support Apache in this conflict. Consequently, many JCP executive committees have agreed not to approve any of Sun's JSRs until the license terms are "fixed".

You can download build b118 from this address:

 http://dlc.sun.com.edgesuite.net/jdk7/binaries/index.html

This list offers files for different platforms.