在Java中设置文件创建时间戳
我知道设置创建时间戳在Java中并不存在,因为Linux没有,但是有没有一种方法可以在Java中设置文件(Windows)的创建时间戳?我在这里做了一个基本的修改时间戳编辑器在Java中设置文件创建时间戳,java,file,date,jfilechooser,Java,File,Date,Jfilechooser,我知道设置创建时间戳在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("In
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");
}
}
}
我相信你有以下选择:
GetFileTime
和SetFileTime
请不要介意标题,它也有一个设置创建时间的方法。我希望您能设法让它工作。如果您使用的是jdk>=1.7,您应该搜索java.nio 你也可以试试这个(在Macos Mavericks上对我很有效,给我两个不同的时间戳):
以下是如何在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)
方法参数分别设置上次修改时间、上次访问时间和创建时间 从Java 7开始,您可以使用和creationTime
属性:
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);
}
可以找到其他属性:
这是如何设置文件创建时间的?不幸的是,在某些Unix上设置创建时间失败(例如,OS X,即使HFS上应该支持它)。如果你想确定它确实被设置好了,在你写了之后阅读并检查!对我来说,它在使用ext3和ext4的Linux上悄无声息地失败了,因为这些FS不支持creationDates。在Java中读取creationDate将返回lastModifiedDate!嗯,这是否允许设置上次修改的时间<(早于)创建时间???
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);
}
Name Type
-------------------------------
"lastModifiedTime" FileTime
"lastAccessTime" FileTime
"creationTime" FileTime
"size" Long
"isRegularFile" Boolean
"isDirectory" Boolean
"isSymbolicLink" Boolean
"isOther" Boolean
"fileKey" Object