【问题标题】:Setting file creation timestamp in Java在 Java 中设置文件创建时间戳
【发布时间】:2012-03-01 04:12:26
【问题描述】:

我知道在 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");
        }
    }
}

【问题讨论】:

    标签: java file date jfilechooser


    【解决方案1】:

    我相信你有以下选择:

    1. 找到一个可以从命令行调用的工具。然后你就可以通过你的 java 代码与之交互了。
    2. 来自 MSDN File Times 的以下链接显示了任何工具将如何执行此操作 - 特别注意函数 GetFileTimeSetFileTime

    在这里,我想你会很幸运 :) 在 Google 上搜索这些功能时,我在 SO 上找到了一个帖子。 This answer(不是公认的)到How to Discover a File's Creation Time with Java 似乎完全可以使用 JNA 和上述方法完成您想要的操作。如果是这样,那么请再给这个答案投票一次:)

    请不要介意标题它也有设置创建时间的方法。我希望你能设法让它工作。

    【讨论】:

      【解决方案2】:

      如果您使用的是 jdk >= 1.7,则应搜索 java.nio

      你也可以试试这个(在 Macos Mavericks 上对我来说效果很好,给我两个不同的时间戳):

      file.setLastModified(created.getTime()); //Older Timestamp
      file.setLastModified(updated.getTime()); //Newer Timestamp
      

      【讨论】:

      • 这如何设置文件创建时间?
      【解决方案3】:

      下面是在 Java 7 中使用 nio 框架的方法:

      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);
      
      }
      

      BasicFileAttributeView.setTimes(FileTime, FileTime, FileTime) 方法参数分别设置上次修改时间、上次访问时间和创建时间。

      【讨论】:

      • 嗯,这是否允许设置最后修改时间
      【解决方案4】:

      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);
      }
      

      其他属性可以找到here:

      Name                  Type
      -------------------------------
      "lastModifiedTime"    FileTime
      "lastAccessTime"      FileTime
      "creationTime"        FileTime
      "size"                Long
      "isRegularFile"       Boolean
      "isDirectory"         Boolean
      "isSymbolicLink"      Boolean
      "isOther"             Boolean
      "fileKey"             Object
      

      【讨论】:

      • 不幸的是,在某些 Unix 上设置创建时间会静默失败(例如 OS X,即使它应该在 HFS 上受支持)。如果您想确定它确实已设置,请在编写后阅读并检查!
      • 对我来说,它在带有 ext3 和 ext4 的 Linux 上默默地失败了,因为这些 FS 不支持creationDates。在 Java 中读取 creationDate 将返回 lastModifiedDate!
      猜你喜欢
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 2019-09-09
      • 1970-01-01
      • 2012-02-29
      • 2015-02-26
      • 1970-01-01
      • 2015-07-06
      相关资源
      最近更新 更多