Java 7 New Features(上篇)

Java 7 也出来了一段时间了,不知道大家有没有开始使用新版本的Java,本文以《Java 7 new Features》为例,翻译出了其中Java的改进点,翻译未必准确,有瑕疵处欢迎指正。本文最后会附上这本书,对Java 7感兴趣的同学可以翻下,未必会立即用到,倒是了解下Java的最新情报总是有好处的。

Java 7 JDK的内容可以参见这里

CHAPTER 1:Java Language Improvement

本章主要讲解了Java 7 语法上的一些改进,有一些还是挺贴心的,不过还得习惯新加的这些语法操作。

  1.  Using String literals in Switch StatementSwitch操作以前只支持intenum类型,现在增加支持String,大小写敏感并且null的话,会抛出NullPointerException
  2.  Using underscores in literals to improve code readability:数字类型添加下划线来增加可读性,不过添加也有几个规则限制,你只要正常使用下划线就对了。
  3.  Using the try-with-resources block to improve exception handling codetry抛出的异常会被catch抓住,现在一个catch就能抓住多个Exception
  4. Creating a resource that can be used with the try-with-resources technique:实现AutoCloseable接口,如果操作文件异常的话,就不必调用close方法,这个处理已经在接口里面实现了。
  5.    Catching multiple exception types to improve type checking:和上面说的是一回事,就是在catch中抓住多个异常。
  6. Rethrowing exceptions in Java 7:将异常抛出,和以前变化不大,也是重新定义自己的异常。
  7. Using the diamond operator for constructor type inference:尖括号能够简化泛型操作。
  8. Using the @SafeVarargs annotation:添加SafeVarargs参数,去除可变参数的警告。

HAPTER 2:Locating File and Directories Using Paths

新添加Path类,按照名字的含义一样,就是操作文件路径的

  1. Creating a Path object:添加Path类,所有关于path有关的操作都可以通过这个类实现。
  2. Interoperability between java.io.File and java.nio.file.FilesFileFiles之间的交互操作。
  3. Converting a relative path into an absolute path:相对地址到绝对地址以及URI表示转换。
  4. Removing redundancies by normalizing a path:标准化path,主要是...在文件系统的意义的转换。
  5. Combining paths using path resolutionpath组合,实际上是rootPathfile生成绝对地址或者相对地址,有个resolveSibling函数比较有意思。
  6. Creating a path between two locations:两个位置的location的相对信息,用.或者..来表示相对位置,...的符号含义和linux相同;注意包含根目录时的比较。
  7. Converting between path typesPathURIabsolutePathRealPath的转换。
  8. Determining whether two paths are equivalent:通过PathequalscompareTo还有Files.isSameFile皆可以判断出路径文件是否一致,这几个方法被重写过。

CHAPTER 3:Obtaining File and Directory Information
文件系统的一些信息,包括目录、文件等,主要是文件的属性,注意在不同的操作系统下,会有不同的view。
  1. Determining the file content type::判断contextType,如text/plainapplication/msword
  2. Obtaining a single attribute at a time using the getAttribute method:使用getAttribute方法获得文件属性,如sizecreationTimelastAccessTime,文件属性有定义,如果使用了错误的文件属性,会报IllegalArgumentException
  3. Obtaining a map of file attributes:使用readAttributes,得到文件属性的一个列表。
  4. Getting file and directory information:得到文件的常规信息,如读、写、执行、隐藏等各个属性
  5. Determining operating system support for attribute views:根据操作系统获得相应的attributeView
  6. Maintaining basic file attributes using the BasicFileAttributeView:使用BasicFileAttributeView,获得文件的基本属性信息。
  7. Maintaining POSIX file attributes using the PosixFileAttributeView:使用PosixFileAttributeView,获得Posix系统下的额外文件信息,如ownergroupPermission等。
  8. Maintaining FAT table attributes using the DosFileAttributeView:使用DosFileAttributeView,获得Dos系统下的文件信息,如读、写、文件、是否是系统文件等。
  9. Maintaining file ownership attributes using the FileOwnerAttributeView:获得文件系统的owner
  10. Maintaining a file's ACL using the AclFileAttributeView:该AttributeView能够获得文件的系统信息,应该为系统的设置信息。
  11. Maintaining user-defined file attributes using the UserDefinedFileAttributeView:该attributeView能够给文件增加自己定义的属性,供前面的attributeView获得该属性。

CHAPTER 4:Managing File and Directory

这个是很常见的文件的Copy、Move、create、delete等操作,不过对于link文件,系统有了额外的支持,对于link文件,拷贝的设定不一样,拷贝的结果也有差异,需要仔细看看API文档。

  1. Creating files and directoriescreateDirectorycreateFile能够创建目录和文件。
  2. Controlling how a file is copiedFiles.copy函数能够复制文件,不过copy函数的几个属性比较有意思,ATOMIC_MOVECOPY_ATTRIBUTEREPLACE_EXISTING;这个copy函数行为不太一致,容易混淆:copy软连接文件、copy目录、copy InputStream到文件、copy文件到OutputStream,细节大家可以仔细看附件内容。
  3. Managing temporary files and directoriescreateTempDirectorycreateTempFile创建临时目录和临时文件,目录和文件的名字是一长串数字,貌似是随机数。
  4. Setting time-related attributes of a file or directory:修改attributeView的时间相关的属性。
  5. Managing file ownership:修改File Owner信息
  6. Managing ACL file permissions:修改ACL permission信息。
  7. Managing POSIX attributes:修改Posix系统的attribute信息。
  8. Moving a file and a directoryFiles.move,该函数和copy函数比较类似。
  9. Deleting files or directoriesFiles.delete,这个没有什么好说的。
  10. Managing symbolic linksFiles.createSysbolicLink,创建软连接;Files.createLink,创建hard link,这个和软连接有什么区别呢?文中论述比较多,大家可以自己看看。
注意:此处的Files、Path类皆出自于Java.nio.file包,这个是Java 7新添的内容。


CHAPTER 5:Managing FileSystem
FileSystem类,这个是用来逻辑化硬盘访问的,并且提供了常见的文件遍历、删除等操作,注意,这个操作实际上是提供了对文件操作的接口,比直接操作文件更为灵活。
  1. Getting FileStore informationFileStoreStoragePooldevicepartitionvolumeconcrete file system或者其它任何文件存储实现;FileStore包含storage的一些可用信息,TotalSpaceunallocatedSpacenametype等,你可以把它看成存储设备,具体是怎么存储的,你可以不用关心。
  2. Getting Filesystem informationFileSystem是用来管理FileStore的,看起来FileStore是硬件逻辑,FileSystem是软件逻辑;举个例子吧,你有三块硬盘,这三块硬盘的信息就是FileStore,你把三块硬盘放到机箱中,装完系统后分成CDEF等盘,这些分区就是FileSystem了,这个例子比较糙,大体就是这个意思了。
  3. Using the SimpleFileVisitor class to traverse filesystemsSimpleFileVisitor提供了几个方法,preVisitDirectoryPostVisitDirectoryvisitFileVisitFileFailed等方法,看到这几个方法有没有感觉眼熟,写完这几个方法就可以遍历FileSystem了,深度遍历。
  4. Deleting a directory using the SimpleFileVisitor class:用SimpleFileVisitor删除目录,在PostVisitDirectory中删除目录,这个是遍历文件之后的操作。
  5. Copying a directory using the SimpleFileVisitor class:用SimpleFileVisitor删除目录,这个需要在PreVisitDirectory中做。
  6. Processing the contents of a directory by using the DirectoryStream interface:用DirectoryStream处理目录的内容,这个思路倒是不错。
  7. Filtering a directory using globbing:有DirectoryStream,必然有带FilterDirectoryStream,支持匹配符。
  8. Writing your own directory filter:编写自己的Filter,这个思路和FileFilter逻辑类似,不过是java.nio.file.DirectoryStream.Filter
  9. Monitoring file events using WatchEventsWatchEvents,向目录注册watchService,同时添加自己感兴趣的events,这个设计有点像监听模式。
  10. Understanding the ZIP filesystem provider:访问Zip FileSystem,访问Zip文件和本地其它文件没有什么区别,函数屏蔽了这种区别。

 

未完待续,参见下篇

猜你喜欢

转载自isilic.iteye.com/blog/1729439
今日推荐