Java 7如何操纵文件属性

准备写点基础性的文章,Java程序员仍然停留在早期的Java 6的基础上,对Java 7的属性以及修改文件属性

  1. import .io.IOException;
  2. import ..file.Files;
  3. import ..file.Path;
  4. import ..file.Paths;
  5. import ..file.attribute.BasicFileAttributes;
  6. import .util.logging.Level;
  7. import .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.             
  21.         }catch(IOException e){
  22.             Logger.getLogger(ReadFileAttribute1.class.getName()).log(Level.SEVERE, null, e);
  23.         }
  24.     }
  25. }

输出结果:
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、另一种读取文件

 

  1. import .io.IOException;
  2. import ..file.Files;
  3. import ..file.Path;
  4. import ..file.Paths;
  5. import .util.logging.Level;
  6. import .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.     }
  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

3、修改文件

 

  1. import .io.IOException;
  2. import ..file.Files;
  3. import ..file.Path;
  4. import ..file.Paths;
  5. import ..file.attribute.BasicFileAttributes;
  6. import ..file.attribute.FileTime;
  7. import .util.logging.Level;
  8. import .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、还可以修改文件别的

 

  1. import .io.IOException;
  2. import ..file.Files;
  3. import ..file.Path;
  4. import ..file.Paths;
  5. import ..file.attribute.BasicFileAttributes;
  6. import ..file.attribute.FileTime;
  7. import .util.logging.Level;
  8. import .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、另一种修改文件

 

  1. import .io.IOException;
  2. import ..file.Files;
  3. import ..file.Path;
  4. import ..file.Paths;
  5. import ..file.attribute.BasicFileAttributes;
  6. import ..file.attribute.FileTime;
  7. import .util.logging.Level;
  8. import .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

posted @ 2015-10-07 11:38  锐洋智能  阅读(555)  评论(0编辑  收藏  举报