准备写点基础性的文章,Java程序员仍然停留在早期的Java 6的基础上,对Java 7的属性以及修改文件属性
-
import .io.IOException;
-
import ..file.Files;
-
import ..file.Path;
-
import ..file.Paths;
-
import ..file.attribute.BasicFileAttributes;
-
import .util.logging.Level;
-
import .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 .io.IOException;
-
import ..file.Files;
-
import ..file.Path;
-
import ..file.Paths;
-
import .util.logging.Level;
-
import .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 .io.IOException;
-
import ..file.Files;
-
import ..file.Path;
-
import ..file.Paths;
-
import ..file.attribute.BasicFileAttributes;
-
import ..file.attribute.FileTime;
-
import .util.logging.Level;
-
import .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 .io.IOException;
-
import ..file.Files;
-
import ..file.Path;
-
import ..file.Paths;
-
import ..file.attribute.BasicFileAttributes;
-
import ..file.attribute.FileTime;
-
import .util.logging.Level;
-
import .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 .io.IOException;
-
import ..file.Files;
-
import ..file.Path;
-
import ..file.Paths;
-
import ..file.attribute.BasicFileAttributes;
-
import ..file.attribute.FileTime;
-
import .util.logging.Level;
-
import .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