简体   繁体   中英

Creation date of file in android

How to get the creation date of file in android. I know about file.lastModified(), but I really need the creation date, that you can see in OS Windows in Properties of file.

If someone know the solution of this task, please, write it below this message. Thanks!

Creation dates are not supported by every operating system. That's why Java doesn't have a method to get the creation date of a file. I ran into this problem recently also.

What I did was append the timestamp as appendix for the file.

File f = new File("myFile-" + System.currentTimeMillis());

When you later look for your file, you'll be able to extract the appendix and convert it back to a date to find it's creation date.

String fileName = f.getName();
String[] split = fileName.split("-");
long timeStamp = 0;

try {
    timeStamp = Long.parseLong(split[1]);
} catch(NumberFormatException nfe) {
    nfe.printStackTrace();
}

System.out.println("Creation date for file " + f + " is " + new Date(timeStamp));

I find the solution of my question, may be some one need it: You can get creation date via executing bash command or script like described here

stat <filepath>

For executing script from android:

p = Runtime.getRuntime().exec("stat " + /mnt/sdcard01/somefile);
p.waitFor();

BufferedReader reader = 
     new BufferedReader(new InputStreamReader(
 p.getInputStream()));
String line = reader.readLine();
while (line != null) {
 line = reader.readLine();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM