在Java中设置文件创建时间戳

15 java jfilechooser date file

我知道设置Java中不存在创建时间戳,因为Linux没有它,但有没有办法在Java中设置文件(Windows)创建时间戳?我有一个基本的修改时间戳编辑器,我在这里制作.

import java.io.*;
import java.util.*;
import java.text.*;
import javax.swing.*;

public class chdt{
    static File file;
    static JFrame frame = new JFrame("Input a file to change");
    public static void main(String[] args) {
        try{

            final JFileChooser fc = new JFileChooser();
            fc.setMultiSelectionEnabled(false);

            //BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
            //System.out.println("Enter file name with extension:");
            //String str = bf.readLine();
            JOptionPane.showMessageDialog(null, "Input a file to change.");
            frame.setSize(300, 200);

            frame.setVisible(true);

            int retVal = fc.showOpenDialog(frame);
            if (retVal == JFileChooser.APPROVE_OPTION) {
                file = fc.getSelectedFile();
                frame.setVisible(false);
            } else {
                JOptionPane.showMessageDialog(null, "3RR0RZ!  You didn't input a file.");
                System.exit(0);
            }

            //System.out.println("Enter last modified date in 'dd-mm-yyyy-hh-mm-ss' format:");
            //String strDate = bf.readLine();
            String strDate = JOptionPane.showInputDialog("Enter last modified date in 'dd-mm-yyyy-hh-mm-ss' format:");

            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss");
            Date date = sdf.parse(strDate);

            if (file.exists()){
                file.setLastModified(date.getTime());
                JOptionPane.showMessageDialog(null, "Modification is successful!");
            }
            else{
                JOptionPane.showMessageDialog(null, "File does not exist!  Did you accidentally it or what?");
            }
        }
        catch(Exception e){
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "3RR0RZ");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

har*_*ska 24

以下是使用nio框架在Java 7中执行此操作的方法:

public void setFileCreationDate(String filePath, Date creationDate) throws IOException{

    BasicFileAttributeView attributes = Files.getFileAttributeView(Paths.get(filePath), BasicFileAttributeView.class);
    FileTime time = FileTime.fromMillis(creationDate.getTime());
    attributes.setTimes(time, time, time);

}
Run Code Online (Sandbox Code Playgroud)

BasicFileAttributeView.setTimes(FileTime, FileTime, FileTime)方法的参数分别设定的最后修改时间,最后访问时间,以及创建时间.


ROM*_*eer 15

Java 7开始,您可以使用java.nio.file.Files.setAttributecreationTime属性:

Path p = Paths.get("C:\\Users\\first.last\\test.txt");
try {
    Calendar c = Calendar.getInstance();
    c.set(2010, Calendar.MARCH, 20);
    Files.setAttribute(p, "creationTime", FileTime.fromMillis(c.getTimeInMillis()));
} catch (IOException e) {
    System.err.println("Cannot change the creation time. " + e);
}
Run Code Online (Sandbox Code Playgroud)

其他属性可以在这里找到:

Name                  Type
-------------------------------
"lastModifiedTime"    FileTime
"lastAccessTime"      FileTime
"creationTime"        FileTime
"size"                Long
"isRegularFile"       Boolean
"isDirectory"         Boolean
"isSymbolicLink"      Boolean
"isOther"             Boolean
"fileKey"             Object
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,设置创建时间在某些Unix上无声地失败(例如OS X,即使它应该*在HFS上支持).如果你想确定它实际设置了,请在写完后再阅读! (6认同)
  • 对我来说,它在带有 ext3 和 ext4 的 Linux 上默默地失败了,因为这些 FS 只是不支持 creationDates。在 Java 中读取 creationDate 将返回 lastModifiedDate! (2认同)

小智 2

我相信您有以下选择:

  1. 找到一个可以执行此操作并且可以从命令行调用的工具。然后您可以通过 Java 代码与其进行交互。
  2. MSDN File Times中的以下链接显示了任何工具如何执行此操作 - 特别注意函数GetFileTimeSetFileTime

我想你会很幸运:) 在 Google 上搜索这些功能时,我发现了一篇关于 SO 的帖子。How to Discover a File's Creation Time with Java的这个答案(不是公认的答案)似乎完全可以使用 JNA 和上述方法完成您想要的操作。如果确实如此,请再次投票赞成该答案:)

请不要介意它的标题,它也有一个设置创建时间的方法。我希望你能设法让它发挥作用。