`

java获取文件创建时间

阅读更多
java中没有获取文件创建时间的方法,只能间接获取。(文件创建时间是windows平台的专有概念)
	public static Date getFileCreateTime(File f) {
		try {
			return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(
					getFileCreateTimeStr(f));
		} catch(Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static String getFileCreateTimeStr(File f) {
		BufferedReader br = null;
		try {
			Process p = Runtime.getRuntime().exec(
					"cmd /c dir " + f.getAbsolutePath());
			
			br = new BufferedReader(new InputStreamReader(p.getInputStream()));
			
			String line = null;
			for(int i = 0; i < 6; i ++) {
				line = br.readLine();
			}
			
			return line.substring(0, 17).replace("/", "-").replace("  ", " ") + ":00";
		} catch(Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if(br != null) {
				try {
					br.close();
				} catch(Exception e) {}
			}
		}
	}
分享到:
评论
1 楼 slendersEye 2011-12-29  
...这个方法很不可靠,cmd很可能会显示last modified time

相关推荐

Global site tag (gtag.js) - Google Analytics