Posted by
otaviojava on October 10, 2011 at 1:13 AM PDT
Welcome java 7 part 3 -NIO 2 JSR 203
So like the coin project, in the NIO 2 there aren't unprecedented features in this e -specification, but now it's possible do some easier work I/Os in java. With the class java.io.Files are possible to perform several operations in simple mode. For that it needs and uses the java.io.file.Path, this interface represents files and directories in the operating system.
Path path=Paths.get("file.txt");
|
Picture 1: Making a path, the String “file.txt” can be replace for any path in operating system, it's should directories, files, symbolic link and hard link.
public static void moveFile(Path arquivoOrigem, Path arquivoDestino) throws Exception { Files.move(arquivoOrigem, arquivoDestino, StandardCopyOption.REPLACE_EXISTING); }
public static void copyFile(Path arquivoOrigem, Path arquivoDestino) throws Exception { Files.copy(arquivoOrigem, arquivoDestino, StandardCopyOption.REPLACE_EXISTING); }
public static void deleteFile(Path arquivo) throws Exception { Files.delete(arquivo); }
public static Path createFile(String arquivo) throws Exception { return Files.createFile(Paths.get(arquivo)); }
public static Path createDictory(String diretorio) throws Exception { return Files.createDirectories(Paths.get(diretorio)); }
public static void creatSymbolicLink(Path linkSimbolico, Path arquivo) throws Exception { Files.createSymbolicLink(linkSimbolico, arquivo); }
public static void createLink(Path link, Path arquivo) throws Exception { Files.createLink(link, arquivo); }
|
Picture 2: I/O's operations using the java.nio.Files
It's possible retrieve information in java.io.Path in an easier way, and can get more about one path with six new interfaces.
public static void propertiesPath(Path path) throws Exception { System.out.println("size in bytes " + Files.size(path)); System.out.println("is directory ? " + Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS)); System.out.println("is regular ? " + Files.isRegularFile(path, LinkOption.NOFOLLOW_LINKS)); System.out.println("is hidden " + Files.isHidden(path)); System.out.println("last modified " + Files.getLastModifiedTime(path, LinkOption.NOFOLLOW_LINKS)); System.out.println("owner " + Files.getOwner(path, LinkOption.NOFOLLOW_LINKS)); System.out.println("\n \n \n Basic Files attributes \n \n \n "); BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class); System.out.println("creation time: " + attr.creationTime()); System.out.println("last acess: " + attr.lastAccessTime()); System.out.println("last modified: " + attr.lastModifiedTime()); System.out.println("is directory " + attr.isDirectory()); System.out.println("is other " + attr.isOther()); System.out.println("is regular: " + attr.isRegularFile()); System.out.println("symbolic link: " + attr.isSymbolicLink()); System.out.println("tamanho: " + attr.size()); System.out.println("\n \n \n Dos atributes file \n \n \n "); DosFileAttributes dosAttr = Files.readAttributes(path, DosFileAttributes.class); System.out.println(" only read " + dosAttr.isReadOnly()); System.out.println("hidden " + dosAttr.isHidden()); System.out.println("file " + dosAttr.isArchive()); System.out.println("system file " + dosAttr.isSystem()); //The other interfaces //PosixFileAttributeView //FileOwnerAttributeView //AclFileAttributeView //UserDefinedFileAttributeView }
|
Picture 3: get more with new interfaces
Now in NIO2 read and write in scalable manner is possible with the java.nio.channels.FileChannel, for example some big documents can use three Threads for reading and writing in quick mode.
public static void acessoEscalavel(Path path) throws Exception { String s = "I was here!\n"; byte data[] = s.getBytes(); ByteBuffer out = ByteBuffer.wrap(data); ByteBuffer copy = ByteBuffer.allocate(12); try (FileChannel fc = (FileChannel.open(path, StandardOpenOption.READ, StandardOpenOption.WRITE))) { //read int nread; do { nread = fc.read(copy); byte[] bytearr = copy.array(); String ss = new String(bytearr); System.out.println("lendo: " + ss); } while (nread != -1 && copy.hasRemaining()); //write in the begin file fc.position(0); while (out.hasRemaining()) { fc.write(out); } out.rewind(); long length = fc.size(); fc.position(length - 1); copy.flip(); while (copy.hasRemaining()) { fc.write(copy); } while (out.hasRemaining()) { fc.write(out); } } catch (IOException x) { System.out.println("I/O Exception: " + x); } }
|
Picture 3: write and read from FileChannel
In Path it is also possible to read and write in a normal way like with the java.io.File.
public static void readFile(Path arquivo) throws Exception { Charset charset = Charset.forName("UTF-8");
String strLine = null; try (BufferedReader reader = Files.newBufferedReader(arquivo, charset)) {
while ((strLine = reader.readLine()) != null) {
System.out.println("file line " + strLine);
}
} catch (IOException x) { System.err.format("IOException: %s%n", x); }
}
public static void writeFile(Path arquivo, String texto) throws Exception {
Charset charset = Charset.forName("UTF-8");
try (BufferedWriter writer = Files.newBufferedWriter(arquivo, charset)) { writer.write(texto, 0, texto.length());
} catch (IOException x) { System.err.format("IOException: %s%n", x); }
}
|
Picture 4: Writing and reading with the Path interface
Conclusion:
This article discusses the package NIO2, showing big features for facilitating I/Os process.