Java 7如何操纵文件属性

Java 7如何操纵文件属性

 

准备写点基础性的文章,Java 7已经出来很长一段时间了,但是很多Java程序员仍然停留在早期的Java 5或Java 6的基础上,对Java 7的新特性从未加以使用。本文介绍如何利用Java 7的NIO包读取文件属性以及修改文件属性。详情如下:

1、读取文件属性

[java][/java] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. import java.io.IOException;
  2. import java.nio.file.Files;
  3. import java.nio.file.Path;
  4. import java.nio.file.Paths;
  5. import java.nio.file.attribute.BasicFileAttributes;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. public class ReadFileAttribute1 {
  9.     public static void main(String[] args){
  10.         Path filePath = Paths.get(“d:\\test.txt”);
  11.         try{
  12.             BasicFileAttributes ra = Files.readAttributes(filePath, BasicFileAttributes.class);
  13.             System.out.println(“CREATION TIME:” + ra.creationTime());
  14.             System.out.println(“LAST ACCESS TIME:” + ra.lastAccessTime());
  15.             System.out.println(“FILE SIZE:” + ra.size());
  16.             System.out.println(“LAST MODIFIED:” + ra.lastModifiedTime());
  17.             System.out.println(“IS SYSBOLIC LINK:” + ra.isSymbolicLink());
  18.             System.out.println(“IS FOLDER:” + ra.isDirectory());
  19.             System.out.println(“IS FILE:” + ra.isRegularFile());
  20.         }catch(IOException e){
  21.             Logger.getLogger(ReadFileAttribute1.class.getName()).log(Level.SEVERE, null, e);
  22.         }
  23.     }
  24. }

输出:
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、另一种读取文件属性的方法

[java][/java] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. import java.io.IOException;
  2. import java.nio.file.Files;
  3. import java.nio.file.Path;
  4. import java.nio.file.Paths;
  5. import java.util.logging.Level;
  6. import java.util.logging.Logger;
  7. public class ReadFileAttribute2 {
  8.     public static void main(String[] args){
  9.         Path fp = Paths.get(“D:\\test.txt”);
  10.         try{
  11.             Files.getAttribute(fp, “basic:size”);
  12.             System.out.println(“CREATION TIME:” + Files.getAttribute(fp, “basic:creationTime”).toString());
  13.             System.out.println(“LAST ACCESS TIME:” + Files.getAttribute(fp, “basic:lastAccessTime”).toString());
  14.             System.out.println(“FILE SIZE:” + Files.getAttribute(fp, “basic:size”).toString());
  15.             System.out.println(“LAST MODIFIED:” + Files.getAttribute(fp, “basic:lastModifiedTime”).toString());
  16.             System.out.println(“IS SYSBOLIC LINK:” + Files.getAttribute(fp, “basic:isSymbolicLink”).toString());
  17.             System.out.println(“IS FOLDER:” + Files.getAttribute(fp, “basic:isDirectory”).toString());
  18.             System.out.println(“IS FILE:” + Files.getAttribute(fp, “basic:isRegularFile”).toString());
  19.         }catch(IOException e){
  20.             Logger.getLogger(ReadFileAttribute2.class.getName()).log(Level.SEVERE, null, e);
  21.         }
  22.     }
  23. }

输出:

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、修改文件属性

[java][/java] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. import java.io.IOException;
  2. import java.nio.file.Files;
  3. import java.nio.file.Path;
  4. import java.nio.file.Paths;
  5. import java.nio.file.attribute.BasicFileAttributes;
  6. import java.nio.file.attribute.FileTime;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. public class UpdateFileAttribute1 {
  10.     public static void main(String[] args){
  11.         Path fp = Paths.get(“D:\\test.txt”);
  12.         try{
  13.             BasicFileAttributes ra = Files.readAttributes(fp, BasicFileAttributes.class);
  14.             long currentTimeMillis = System.currentTimeMillis();
  15.             FileTime fileTime = FileTime.fromMillis(currentTimeMillis);
  16.             Files.setAttribute(fp, “basic:creationTime”, fileTime);
  17.             FileTime creationTime = (FileTime)Files.getAttribute(fp, “basic:creationTime”);
  18.             System.out.println(“NEW CREATION TIME:” + creationTime.toString());
  19.             System.out.println(“OLD CREATION TIME:” + ra.creationTime());
  20.         }catch(IOException e){
  21.             Logger.getLogger(UpdateFileAttribute1.class.getName()).log(Level.SEVERE, null, e);
  22.         }
  23.     }
  24. }

输出:

NEW CREATION TIME:2013-12-10T15:04:26.159Z
OLD CREATION TIME:2013-04-09T05:45:30.685152Z

4、还可以修改文件别的属性

[java][/java] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. import java.io.IOException;
  2. import java.nio.file.Files;
  3. import java.nio.file.Path;
  4. import java.nio.file.Paths;
  5. import java.nio.file.attribute.BasicFileAttributes;
  6. import java.nio.file.attribute.FileTime;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. public class UpdateFileAttribute2 {
  10.     public static void main(String[] args){
  11.         Path fp = Paths.get(“D:\\test.txt”);
  12.         try{
  13.             BasicFileAttributes ra = Files.readAttributes(fp, BasicFileAttributes.class);
  14.             long currentTimeMillis = System.currentTimeMillis();
  15.             FileTime fileTime = FileTime.fromMillis(currentTimeMillis);
  16.             Files.setAttribute(fp, “basic:lastAccessTime”, fileTime);
  17.             FileTime creationTime = (FileTime)Files.getAttribute(fp, “basic:lastAccessTime”);
  18.             System.out.println(“NEW LAST-ACCESS-TIME:” + creationTime.toString());
  19.             System.out.println(“OLD LAST-ACCESS-TIME:” + ra.lastAccessTime());
  20.         }catch(IOException e){
  21.             Logger.getLogger(UpdateFileAttribute2.class.getName()).log(Level.SEVERE, null, e);
  22.         }
  23.     }
  24. }

输出:

NEW LAST-ACCESS-TIME:2013-12-10T15:07:46.261Z
OLD LAST-ACCESS-TIME:2013-08-12T00:30:43.19369Z

5、另一种修改文件属性的方法

[java][/java] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. import java.io.IOException;
  2. import java.nio.file.Files;
  3. import java.nio.file.Path;
  4. import java.nio.file.Paths;
  5. import java.nio.file.attribute.BasicFileAttributes;
  6. import java.nio.file.attribute.FileTime;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. public class UpdateFileAttribute3 {
  10.     public static void main(String[] args){
  11.         Path fp = Paths.get(“D:\\test.txt”);
  12.         try{
  13.             BasicFileAttributes ra = Files.readAttributes(fp, BasicFileAttributes.class);
  14.             long currentTimeMillis = System.currentTimeMillis();
  15.             FileTime fileTime = FileTime.fromMillis(currentTimeMillis);
  16.             Files.setLastModifiedTime(fp, fileTime);
  17.             FileTime time = (FileTime)Files.getAttribute(fp, “basic:lastModifiedTime”);
  18.             System.out.println(“NEW LAST-MODIFIED-TIME:” + time.toString());
  19.             System.out.println(“OLD LAST-MODIFIED-TIME:” + ra.lastModifiedTime());
  20.         }catch(IOException e){
  21.             Logger.getLogger(UpdateFileAttribute3.class.getName()).log(Level.SEVERE, null, e);
  22.         }
  23.     }
  24. }

输出:
NEW LAST-MODIFIED-TIME:2013-12-10T15:13:07.134Z
OLD LAST-MODIFIED-TIME:2013-12-10T13:35:08.438828Z

标签