回答
File類有一個setLastModified方法。這就是ANT所做的。
除了已知的Android錯誤之外,File.setLastModified在大多數Android設備上都沒有做任何事情。 – 2015-12-19 17:57:49
除了shell'touch'創建文件,並沒有。 – 2016-08-07 21:58:12
我知道Apache Ant有一個Task就是這樣。
見source code of Touch(它可以告訴你如何他們這樣做)
他們使用FILE_UTILS.setFileLastModified(file, modTime);
,它採用ResourceUtils.setLastModified(new FileResource(file), time);
,它採用了org.apache.tools.ant.types.resources.Touchable
,通過org.apache.tools.ant.types.resources.FileResource
實現...
基本上,它是File.setLastModified(modTime)
通話。
這裏有一個簡單的片斷:
void touch(File file, long timestamp)
{
try
{
if (!file.exists())
new FileOutputStream(file).close();
file.setLastModified(timestamp);
}
catch (IOException e)
{
}
}
爲什麼'file.createNewFile()'而不是'FileOutputStream(file).close()'? – Harvey 2015-08-20 19:04:45
這個問題只提到更新時間戳,但我想我把這個在這裏反正。我正在尋找像Unix一樣的觸摸,如果它不存在,它也會創建一個文件。
對於任何使用Apache Commons的人來說,FileUtils.touch(File file)
就是這麼做的。
下面是從(內聯openInputStream(File f)
)的source:
public static void touch(final File file) throws IOException {
if (file.exists()) {
if (file.isDirectory()) {
throw new IOException("File '" + file + "' exists but is a directory");
}
if (file.canWrite() == false) {
throw new IOException("File '" + file + "' cannot be written to");
}
} else {
final File parent = file.getParentFile();
if (parent != null) {
if (!parent.mkdirs() && !parent.isDirectory()) {
throw new IOException("Directory '" + parent + "' could not be created");
}
}
final OutputStream out = new FileOutputStream(file);
IOUtils.closeQuietly(out);
}
final boolean success = file.setLastModified(System.currentTimeMillis());
if (!success) {
throw new IOException("Unable to set the last modification time for " + file);
}
}
我的2美分,基於@Joe.M answer
public static void touch(File file) throws IOException{
long timestamp = System.currentTimeMillis();
touch(file, timestamp);
}
public static void touch(File file, long timestamp) throws IOException{
if (!file.exists()) {
new FileOutputStream(file).close();
}
file.setLastModified(timestamp);
}
如果您已經使用Guava:
com.google.common.io.Files.touch(file)
由於File
爲a bad abstraction,最好是使用Files
和Path
:
public static void touch(final Path path) throws IOException {
Objects.requireNotNull(path, "path is null");
if (Files.exists(path)) {
Files.setLastModifiedTime(path, FileTime.from(Instant.now()));
} else {
Files.createFile(path);
}
}
- 1. 使用ADB模擬觸摸
- 2. 在Java中模擬觸摸滾動
- 3. 在iphone上模擬觸摸
- 4. Android adb命令來模擬觸摸事件和超級用戶apk建議
- 5. 用javascript模擬觸摸事件
- 6. Chrome DevTools觸摸模擬不起作用
- 7. 在非觸摸屏安卓設備上模擬觸摸屏
- 8. 在JAVA中模擬shell命令
- 9. midlet命令中的觸摸事件
- 10. 模擬的UITextField觸摸由UIButton的
- 11. 從android內核模擬觸摸事件
- 12. BB10瀑布 - 模擬觸摸事件
- 13. Android藍牙觸摸板模擬
- 14. Angular:在觸摸設備上模擬mouseleave
- 15. 在ios7/8上模擬觸摸
- 16. 模擬器上的多點觸摸
- 17. Android模擬器支持Windows觸摸屏
- 18. 上YUI模擬觸摸事件3
- 19. 模擬iPhone中的觸摸事件
- 20. 模擬命令提示符命令
- 21. 在Android模擬器中模擬多點觸摸
- 22. Linux expect命令模擬
- 23. 使用觸摸屏的虛擬鼠標/觸摸板
- 24. 觸摸屏上的Windows將通過觸摸手指來模擬MouseDown事件嗎?
- 25. 如何使用標準Windows命令觸摸文件?
- 26. 在Windows 8中使用Python模擬觸摸事件
- 27. react-native ios模擬器觸摸不起作用
- 28. 如何使用xamarin模擬android上的屏幕觸摸?
- 29. 模擬觸摸輸入的Android應用程序
- 30. 是否有附加模塊或擴展模擬觸摸事件?
應該有人提出這是一個增強請求unix4j:https://github.com/tools4j/unix4j – 2017-05-21 05:37:43
我不明白的關係標題和問題在這裏? – Lealo 2017-09-28 19:29:26