Java 7如何操纵文件属性
作者:chszs,转载需注明。博客主页:http://blog.csdn.net/chszs
准备写点基础性的文章,Java 7已经出来很长一段时间了,但是很多Java程序员仍然停留在早期的Java 5或Java 6的基础上,对Java 7的新特性从未加以使用。本文介绍如何利用Java 7的NIO包读取文件属性以及修改文件属性。详情如下:
1、读取文件属性
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.logging.Level;
import java.util.logging.Logger; public class ReadFileAttribute1 {
public static void main(String[] args){
Path filePath = Paths.get("d:\\test.txt");
try{
BasicFileAttributes ra = Files.readAttributes(filePath, BasicFileAttributes.class);
System.out.println("CREATION TIME:" + ra.creationTime());
System.out.println("LAST ACCESS TIME:" + ra.lastAccessTime());
System.out.println("FILE SIZE:" + ra.size());
System.out.println("LAST MODIFIED:" + ra.lastModifiedTime());
System.out.println("IS SYSBOLIC LINK:" + ra.isSymbolicLink());
System.out.println("IS FOLDER:" + ra.isDirectory());
System.out.println("IS FILE:" + ra.isRegularFile()); }catch(IOException e){
Logger.getLogger(ReadFileAttribute1.class.getName()).log(Level.SEVERE, null, e);
}
}
}
输出:
CREATION TIME:2013-04-09T05:45:30.685152Z
LAST ACCESS TIME:2013-08-12T00:30:43.19369Z
FILE SIZE:32
LAST MODIFIED:2013-12-10T13:35:08.438828Z
IS SYSBOLIC LINK:false
IS FOLDER:false
IS FILE:true
2、另一种读取文件属性的方法
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger; public class ReadFileAttribute2 {
public static void main(String[] args){
Path fp = Paths.get("D:\\test.txt");
try{
Files.getAttribute(fp, "basic:size");
System.out.println("CREATION TIME:" + Files.getAttribute(fp, "basic:creationTime").toString());
System.out.println("LAST ACCESS TIME:" + Files.getAttribute(fp, "basic:lastAccessTime").toString());
System.out.println("FILE SIZE:" + Files.getAttribute(fp, "basic:size").toString());
System.out.println("LAST MODIFIED:" + Files.getAttribute(fp, "basic:lastModifiedTime").toString());
System.out.println("IS SYSBOLIC LINK:" + Files.getAttribute(fp, "basic:isSymbolicLink").toString());
System.out.println("IS FOLDER:" + Files.getAttribute(fp, "basic:isDirectory").toString());
System.out.println("IS FILE:" + Files.getAttribute(fp, "basic:isRegularFile").toString());
}catch(IOException e){
Logger.getLogger(ReadFileAttribute2.class.getName()).log(Level.SEVERE, null, e);
} }
}
输出:
CREATION TIME:2013-04-09T05:45:30.685152Z
LAST ACCESS TIME:2013-08-12T00:30:43.19369Z
FILE SIZE:32
LAST MODIFIED:2013-12-10T13:35:08.438828Z
IS SYSBOLIC LINK:false
IS FOLDER:false
IS FILE:true
3、修改文件属性
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.logging.Level;
import java.util.logging.Logger; public class UpdateFileAttribute1 {
public static void main(String[] args){
Path fp = Paths.get("D:\\test.txt");
try{
BasicFileAttributes ra = Files.readAttributes(fp, BasicFileAttributes.class);
long currentTimeMillis = System.currentTimeMillis();
FileTime fileTime = FileTime.fromMillis(currentTimeMillis);
Files.setAttribute(fp, "basic:creationTime", fileTime);
FileTime creationTime = (FileTime)Files.getAttribute(fp, "basic:creationTime");
System.out.println("NEW CREATION TIME:" + creationTime.toString());
System.out.println("OLD CREATION TIME:" + ra.creationTime());
}catch(IOException e){
Logger.getLogger(UpdateFileAttribute1.class.getName()).log(Level.SEVERE, null, e);
}
}
}
输出:
NEW CREATION TIME:2013-12-10T15:04:26.159Z
OLD CREATION TIME:2013-04-09T05:45:30.685152Z
4、还可以修改文件别的属性
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.logging.Level;
import java.util.logging.Logger; public class UpdateFileAttribute2 {
public static void main(String[] args){
Path fp = Paths.get("D:\\test.txt");
try{
BasicFileAttributes ra = Files.readAttributes(fp, BasicFileAttributes.class);
long currentTimeMillis = System.currentTimeMillis();
FileTime fileTime = FileTime.fromMillis(currentTimeMillis);
Files.setAttribute(fp, "basic:lastAccessTime", fileTime);
FileTime creationTime = (FileTime)Files.getAttribute(fp, "basic:lastAccessTime");
System.out.println("NEW LAST-ACCESS-TIME:" + creationTime.toString());
System.out.println("OLD LAST-ACCESS-TIME:" + ra.lastAccessTime());
}catch(IOException e){
Logger.getLogger(UpdateFileAttribute2.class.getName()).log(Level.SEVERE, null, e);
}
}
}
输出:
NEW LAST-ACCESS-TIME:2013-12-10T15:07:46.261Z
OLD LAST-ACCESS-TIME:2013-08-12T00:30:43.19369Z
5、另一种修改文件属性的方法
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.logging.Level;
import java.util.logging.Logger; public class UpdateFileAttribute3 {
public static void main(String[] args){
Path fp = Paths.get("D:\\test.txt");
try{
BasicFileAttributes ra = Files.readAttributes(fp, BasicFileAttributes.class);
long currentTimeMillis = System.currentTimeMillis();
FileTime fileTime = FileTime.fromMillis(currentTimeMillis);
Files.setLastModifiedTime(fp, fileTime);
FileTime time = (FileTime)Files.getAttribute(fp, "basic:lastModifiedTime");
System.out.println("NEW LAST-MODIFIED-TIME:" + time.toString());
System.out.println("OLD LAST-MODIFIED-TIME:" + ra.lastModifiedTime());
}catch(IOException e){
Logger.getLogger(UpdateFileAttribute3.class.getName()).log(Level.SEVERE, null, e);
}
}
}
输出:
NEW LAST-MODIFIED-TIME:2013-12-10T15:13:07.134Z
OLD LAST-MODIFIED-TIME:2013-12-10T13:35:08.438828Z