2012-02-08 41 views
11

Tôi biết việc đặt dấu thời gian tạo không tồn tại trong Java vì Linux không có nó, nhưng có cách nào để đặt dấu thời gian tạo của tệp (Windows) trong Java không? Tôi có một trình chỉnh sửa dấu thời gian cơ bản mà tôi đã thực hiện ngay tại đây.Đặt dấu thời gian tạo tệp trong Java

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

Trả lời

2

Tôi tin rằng bạn có các tùy chọn sau:

  1. Tìm một công cụ thực hiện điều này và là callable từ dòng lệnh. Sau đó, bạn có thể tương tác với nó từ mã java của bạn.
  2. Liên kết sau từ MSDN File Times cho biết công cụ sẽ hoạt động như thế nào - đặc biệt lưu ý các chức năng GetFileTimeSetFileTime.

Và ở đây tôi đoán bạn sẽ may mắn :) Tìm kiếm các chức năng đó trên Google Tôi tìm thấy một bài đăng ở đây trên SO. This answer (không phải là một chấp nhận) để How to Discover a File's Creation Time with Java dường như làm chính xác những gì bạn muốn bằng cách sử dụng JNA và các phương pháp trên. Và nếu có, xin vui lòng upvote rằng câu trả lời một lần nữa :)

Xin vui lòng không nhớ tiêu đề nó có một phương pháp để thiết lập thời gian tạo quá. Tôi hy vọng bạn sẽ quản lý để làm cho nó hoạt động.

0

Bạn nên tìm kiếm java.nio nếu bạn đang sử dụng jdk> = 1,7

Bạn cũng có thể thử loại này (làm việc tốt cho tôi trên hệ điều hành MacOS Mavericks và làm cho tôi hai timestamps khác nhau):

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

Làm thế nào thực hiện điều này thiết lập các tập tin thời gian sáng tạo? –

16

đây là cách bạn làm điều đó trong Java 7 với khuôn khổ 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); 

} 

các BasicFileAttributeView.setTimes(FileTime, FileTime, FileTime) đối số phương pháp thiết lập thời gian sửa đổi lần cuối, thời gian truy cập lần cuối, và thời gian tạo lại .

6

Bắt đầu từ Java 7, bạn có thể sử dụng java.nio.file.Files.setAttributecreationTime thuộc tính:

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

thuộc tính khác có thể được tìm thấy here:

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

Thật không may, việc thiết lập thời gian tạo không thành công trên một số Unix (ví dụ: OS X, mặc dù nó * cần * được hỗ trợ trên HFS). Nếu bạn muốn chắc chắn rằng nó đã được thực sự thiết lập, đọc sau khi bạn đã viết và kiểm tra! – hendrik

Các vấn đề liên quan