为什么不setLastModified(时间)这个文件的工作? [英] Why doesn't setLastModified(time) work for this File?
问题描述
为什么在下面的代码中文件的日期没有改变?
fLocal.location
=现有的文件在C:\
fLocal.date
=要设定的日期长
boolean x = new File(fLocal.location).setLastModified(Long.parseLong(fLocal.date));
System.out.println(Changed:+ x);
System.out.println(new Date(new File(fLocal.location).lastModified()));
System.out.println(new Date(Long.parseLong(fLocal.date)));
输出:
更改:false
$
Fri Feb 15 23:02:51 CET 2013
Fri Feb 15 22:49:34 CET 2013
$ b $ div class =h2_lin>解决方案
这些都是可能阻止您更改文件时间的项目。
用一行文本创建一个简单的纯文本文件,保存并关闭编辑器。然后尝试在您的应用程序中使用该文件。确保在 File
Object
上调用 exists()
在尝试更改它的时间以确保您实际上拥有有效的文件之前。
Why is the date of the file in the following code not changed?
fLocal.location
= Existing file in C:\
fLocal.date
= Date to set in Long
boolean x = new File(fLocal.location).setLastModified(Long.parseLong(fLocal.date));
System.out.println("Changed: " + x);
System.out.println(new Date(new File(fLocal.location).lastModified()));
System.out.println(new Date(Long.parseLong(fLocal.date)));
Output:
Changed: false
Fri Feb 15 23:02:51 CET 2013
Fri Feb 15 22:49:34 CET 2013
From my comments from earlier, follow these checks:
- Does your code have write access to the file?
- Is the file in open status?
- Are you currently reading (or writing!) the file with any other application at the time you are doing this?
These are all items that might prevent you from changing the time of the file.
Create a simple plain text file with a single line of text, save it and close out of the editor. Then try using that file in your application. Make sure you call exists()
on your File
Object
before you try to change the time of it to ensure you actually have a valid file.
这篇关于为什么不setLastModified(时间)这个文件的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!