Java 获取文件创建时间2
2015-08-06 09:34
537 查看
package com.dancen.test; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; /** * Runtime获取文件创建时间示例 * * @author Dancen * */ public class FileCreatedDemo { public static void main(String[] args) { try { String fileCreated = getFileCreated("D:\\xiyou.jpg"); System.out.println(fileCreated); } catch(Exception e) { e.printStackTrace(); } } public static String getFileCreated(String path) { if(null == path) { return null; } return getFileCreated(new File(path)); } public static String getFileCreated(final File file) { if(null == file) { return null; } String rs = null; final StringBuilder sb = new StringBuilder(); Process p = null; try { p = Runtime.getRuntime().exec(String.format("cmd /C dir %s /tc", file.getAbsolutePath())); } catch(IOException e) { return rs; } final InputStream is = p.getInputStream(); final InputStreamReader ir = new InputStreamReader(is); final BufferedReader br = new BufferedReader(ir); Runnable runnable = new Runnable() { @Override public void run() { String data = null; try { while(null != (data = br.readLine())) { if(-1 != data.toLowerCase().indexOf(file.getName().toLowerCase())) { String[] temp = data.split(" +"); if(2 <= temp.length) { String time = String.format("%s %s", temp[0], temp[1]); sb.append(time); } break; } } } catch(IOException e) { e.printStackTrace(); } finally { try { if(null != br) { br.close(); } if(null != ir) { ir.close(); } if(null != is) { is.close(); } } catch(IOException e) { e.printStackTrace(); } } } }; Thread thread = new Thread(runnable); thread.start(); try { thread.join(); } catch(InterruptedException e) { e.printStackTrace(); } if(0 != sb.length()) { rs = sb.toString(); } return rs; } }
相关文章推荐
- Java多线程——<四>让线程有返回值
- java获取文件创建时间
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- Myeclipse错误:Errors occurred during the build. Errors running builder 'DeploymentBuilder' on project
- Eclipse中查看JDK和JAVA框架源码的设置
- Java 注解
- Java线程:线程的同步与锁
- JAVA 时间处理
- (Dynamic Proxy)动态代理模式的Java实现
- java内部类
- Java中引用类型vs值类型&对引用类型所做的GC操作简介
- Spring调用存储过程
- eclipse加载工程中出现红叹号
- Java利用Zxing生成二维码
- JAVA 四大域对象总结
- java.util.regex.PatternSyntaxeException
- java Collection 介绍
- java中的观察者模式2
- 关于Resin在Eclipse或者Myeclipse上利用Debug模式部署的方法以及遇到的一些问题
- Eclipse+Struts2+Spring+MyBatis+json+gson环境搭建