本文介绍了在Java中设置文件创建时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道设置创建时间戳不存在于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(输入要更改的文件);
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(\"输入扩展名为的文件名);
// String str = bf.readLine();
JOptionPane.showMessageDialog(null,输入要更改的文件);
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!你没有输入文件);
System.exit(0);
}

//System.out.println(以'dd-mm-yyyy-hh-mm-ss'格式输入最后修改日期:);
// String strDate = bf.readLine();
String strDate = JOptionPane.showInputDialog(以'dd-mm-yyyy-hh-mm-ss'格式输入最后修改的日期:);

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

if(file.exists()){
file.setLastModified(date.getTime());
JOptionPane.showMessageDialog(null,修改成功!);
}
其他{
JOptionPane.showMessageDialog(null,文件不存在!你不小心或什么?);
}
}
catch(异常e){
e.printStackTrace();
JOptionPane.showMessageDialog(null,3RR0RZ);
}
}
}


解决方案

我相信您有以下选项:


  1. 找到一个可以从命令行调用的工具。然后,您可以从您的java代码与它进行交互。

  2. 以下链接来自MSDN 显示了任何工具将会如何工作 - 特别注意函数 GetFileTime SetFileTime

这里我想你会很幸运:)在Google上搜索这些功能我在这里发现了一个帖子。 (不是接受的)到似乎完全符合你想要的使用JNA和上述方法。如果是这样,那么请再一次提出这个答案:)



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


I know setting the creation timestamp doesn't exist in Java because Linux doesn't have it, but is there a way to set a file's (Windows) creation timestamp in Java? I have a basic modification timestamp editor I made right here.

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");
        }
    }
}
解决方案

I believe you have the following options:

  1. Find a tool that does this and is callable from the command line. Then you can interact with it from your java code.
  2. The following link from MSDN File Times shows how any tool would be doing it - especially note the functions GetFileTime and SetFileTime.

And here I guess you will be lucky :) Searching for those functions on Google I found a post here on SO. This answer (not the accepted one) to How to Discover a File's Creation Time with Java seems to do exactly what you want using JNA and the methods above. And if it does, then please upvote that answer one more time :)

Please don't mind the title it has a method to set the creation time too. I hope you will manage to get it working.

这篇关于在Java中设置文件创建时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 15:53