Java 7 也出来了一段时间了,不知道大家有没有开始使用新版本的Java,本文以《Java 7 new Features》为例,翻译出了其中Java的改进点,翻译未必准确,有瑕疵处欢迎指正。本文最后会附上这本书,对Java 7感兴趣的同学可以翻下,未必会立即用到,倒是了解下Java的最新情报总是有好处的。
Java 7 JDK的内容可以参见这里。
CHAPTER 1:Java Language Improvement
本章主要讲解了Java 7 语法上的一些改进,有一些还是挺贴心的,不过还得习惯新加的这些语法操作。
- Using String literals in Switch Statement:Switch操作以前只支持int,enum类型,现在增加支持String,大小写敏感并且null的话,会抛出NullPointerException
- Using underscores in literals to improve code readability:数字类型添加下划线来增加可读性,不过添加也有几个规则限制,你只要正常使用下划线就对了。
- Using the try-with-resources block to improve exception handling code:try抛出的异常会被catch抓住,现在一个catch就能抓住多个Exception
- Creating a resource that can be used with the try-with-resources technique:实现AutoCloseable接口,如果操作文件异常的话,就不必调用close方法,这个处理已经在接口里面实现了。
- Catching multiple exception types to improve type checking:和上面说的是一回事,就是在catch中抓住多个异常。
- Rethrowing exceptions in Java 7:将异常抛出,和以前变化不大,也是重新定义自己的异常。
- Using the diamond operator for constructor type inference:尖括号能够简化泛型操作。
- Using the @SafeVarargs annotation:添加SafeVarargs参数,去除可变参数的警告。
HAPTER 2:Locating
File and Directories Using Paths
新添加Path类,按照名字的含义一样,就是操作文件路径的
- Creating a Path object:添加Path类,所有关于path有关的操作都可以通过这个类实现。
- Interoperability between java.io.File and java.nio.file.Files:File和Files之间的交互操作。
- Converting a relative path into an absolute path:相对地址到绝对地址以及URI表示转换。
- Removing redundancies by normalizing a path:标准化path,主要是.和..在文件系统的意义的转换。
- Combining paths using path resolution:path组合,实际上是rootPath和file生成绝对地址或者相对地址,有个resolveSibling函数比较有意思。
- Creating a path between two locations:两个位置的location的相对信息,用.或者..来表示相对位置,.和..的符号含义和linux相同;注意包含根目录时的比较。
- Converting between path types:Path、URI、absolutePath、RealPath的转换。
- Determining whether two paths are equivalent:通过Path的equals、compareTo还有Files.isSameFile皆可以判断出路径文件是否一致,这几个方法被重写过。
CHAPTER 3:Obtaining
File and Directory Information
文件系统的一些信息,包括目录、文件等,主要是文件的属性,注意在不同的操作系统下,会有不同的view。
- Determining the file content type::判断contextType,如text/plain、application/msword等
- Obtaining a single attribute at a time using the getAttribute method:使用getAttribute方法获得文件属性,如size、creationTime、lastAccessTime,文件属性有定义,如果使用了错误的文件属性,会报IllegalArgumentException。
- Obtaining a map of file attributes:使用readAttributes,得到文件属性的一个列表。
- Getting file and directory information:得到文件的常规信息,如读、写、执行、隐藏等各个属性
- Determining operating system support for attribute views:根据操作系统获得相应的attributeView。
- Maintaining basic file attributes using the BasicFileAttributeView:使用BasicFileAttributeView,获得文件的基本属性信息。
- Maintaining POSIX file attributes using the PosixFileAttributeView:使用PosixFileAttributeView,获得Posix系统下的额外文件信息,如owner、group、Permission等。
- Maintaining FAT table attributes using the DosFileAttributeView:使用DosFileAttributeView,获得Dos系统下的文件信息,如读、写、文件、是否是系统文件等。
- Maintaining file ownership attributes using the FileOwnerAttributeView:获得文件系统的owner。
- Maintaining a file's ACL using the AclFileAttributeView:该AttributeView能够获得文件的系统信息,应该为系统的设置信息。
- Maintaining user-defined file attributes using the UserDefinedFileAttributeView:该attributeView能够给文件增加自己定义的属性,供前面的attributeView获得该属性。
CHAPTER 4:Managing
File and Directory
这个是很常见的文件的Copy、Move、create、delete等操作,不过对于link文件,系统有了额外的支持,对于link文件,拷贝的设定不一样,拷贝的结果也有差异,需要仔细看看API文档。
- Creating files and directories:createDirectory和createFile能够创建目录和文件。
- Controlling how a file is copied:Files.copy函数能够复制文件,不过copy函数的几个属性比较有意思,ATOMIC_MOVE、COPY_ATTRIBUTE、REPLACE_EXISTING;这个copy函数行为不太一致,容易混淆:copy软连接文件、copy目录、copy InputStream到文件、copy文件到OutputStream,细节大家可以仔细看附件内容。
- Managing temporary files and directories:createTempDirectory和createTempFile创建临时目录和临时文件,目录和文件的名字是一长串数字,貌似是随机数。
- Setting time-related attributes of a file or directory:修改attributeView的时间相关的属性。
- Managing file ownership:修改File Owner信息
- Managing ACL file permissions:修改ACL permission信息。
- Managing POSIX attributes:修改Posix系统的attribute信息。
- Moving a file and a directory:Files.move,该函数和copy函数比较类似。
- Deleting files or directories:Files.delete,这个没有什么好说的。
- Managing symbolic links:Files.createSysbolicLink,创建软连接;Files.createLink,创建hard link,这个和软连接有什么区别呢?文中论述比较多,大家可以自己看看。
注意:此处的Files、Path类皆出自于Java.nio.file包,这个是Java 7新添的内容。
CHAPTER 5:Managing
FileSystem
FileSystem类,这个是用来逻辑化硬盘访问的,并且提供了常见的文件遍历、删除等操作,注意,这个操作实际上是提供了对文件操作的接口,比直接操作文件更为灵活。
- Getting FileStore information:FileStore是StoragePool、device、partition、volume、concrete file system或者其它任何文件存储实现;FileStore包含storage的一些可用信息,如TotalSpace、unallocatedSpace、name、type等,你可以把它看成存储设备,具体是怎么存储的,你可以不用关心。
- Getting Filesystem information:FileSystem是用来管理FileStore的,看起来FileStore是硬件逻辑,FileSystem是软件逻辑;举个例子吧,你有三块硬盘,这三块硬盘的信息就是FileStore,你把三块硬盘放到机箱中,装完系统后分成C、D、E、F等盘,这些分区就是FileSystem了,这个例子比较糙,大体就是这个意思了。
- Using the SimpleFileVisitor class to traverse filesystems:SimpleFileVisitor提供了几个方法,preVisitDirectory、PostVisitDirectory、visitFile、VisitFileFailed等方法,看到这几个方法有没有感觉眼熟,写完这几个方法就可以遍历FileSystem了,深度遍历。
- Deleting a directory using the SimpleFileVisitor class:用SimpleFileVisitor删除目录,在PostVisitDirectory中删除目录,这个是遍历文件之后的操作。
- Copying a directory using the SimpleFileVisitor class:用SimpleFileVisitor删除目录,这个需要在PreVisitDirectory中做。
- Processing the contents of a directory by using the DirectoryStream interface:用DirectoryStream处理目录的内容,这个思路倒是不错。
- Filtering a directory using globbing:有DirectoryStream,必然有带Filter的DirectoryStream,支持匹配符。
- Writing your own directory filter:编写自己的Filter,这个思路和FileFilter逻辑类似,不过是java.nio.file.DirectoryStream.Filter
- Monitoring file events using WatchEvents:WatchEvents,向目录注册watchService,同时添加自己感兴趣的events,这个设计有点像监听模式。
- Understanding the ZIP filesystem provider:访问Zip FileSystem,访问Zip文件和本地其它文件没有什么区别,函数屏蔽了这种区别。
未完待续,参见下篇
分享到:
相关推荐
java7 new features cookbook
Java 7 New Features Cookbook.pdf
Java 7 New Features Cookbook
Java 7 New Features Cookbook, by Richard Reese The following applies to example files from material published. Please note that the examples are not production code and have not been carefully ...
Enhance your development skills with Java's state-of-the-art features and projects to make your applications leaner and faster... Make the best use of new Java features and libraries in your applications
java8-new-features总结
Creating Pop Up Window Using New Features in WebDynpro Java SAP NetWeaver
JDK1.8新特性,New Features in Java SE 8
Java Virtual Machine Support for Non-Java Languages: Java SE 7 introduces a new JVM instruction that simplifies the implementation of dynamically typed programming languages on the JVM. Garbage-First...
RightFax 9.0 Guide to New Features RightFax 9.0 Guide to New Features
features of Java 9 are included wherever they fit in these chapters. The Module API, Reactive Streams, and Stack-Walking API, which were added in Java 9, are covered in depth in their own chapters. ...
Fully updated to reflect Java SE 7 language changes, Core Java®, Volume II—Advanced Features, Ninth Edition, is the definitive guide to Java’s most powerful features for enterprise and desktop ...
Fully updated to reflect Java SE 7 language changes, Core Java®, Volume II—Advanced Features, Ninth Edition, is the definitive guide to Java’s most powerful features for enterprise and desktop ...
Introduce Java 7 new features
Put simply, the new features in Java 8 along with the (less-obvious) changes in Java 9 are the biggest change to Java in the 21 years since Java 1.0 was released. Nothing has been taken away, so all ...
Many old Java idioms are no longer required and new features like lambda expressions will increase programmer productivity, but navigating these changes can be challenging., , Core Java® for the ...
Rewritten and reorganized to illuminate powerful new Java features, idioms, and best practices for enterprise and desktop development, it contains hundreds of up-to-date example programs—all ...
Java 9 and its new features add to the richness of the language--one of the most-used languages to build robust software applications. Java 9 comes with a special emphasis on modularity, implemented ...