我想实现一个查看文件生成时间的方法,网上找了个可以用的结果,我路径中含有空格,就一直看不到生成时间,打印出来都是null,没有空格就没问题,现在就想哪位可以帮忙看看,解决我路径有空格的方法,我找的方法如下:
public static String getFileCreateDate(File _file) {        File file = _file;        try {            Process ls_proc = Runtime.getRuntime().exec(                    "cmd.exe /c dir " + file.getAbsolutePath() + " /tc");            BufferedReader br = new BufferedReader(new InputStreamReader(ls_proc.getInputStream()));            for (int i = 0; i < 5; i++) {                br.readLine();            }            String stuff = br.readLine();            StringTokenizer st = new StringTokenizer(stuff);            String dateC = st.nextToken();            String time = st.nextToken();            String datetime = dateC.concat(time);            br.close();            return datetime;        } catch (Exception e) {            return null;        }    }File file = new File("D:\\My Work\\102.txt");String time = Timetake.getFileCreateDate(file);

解决方案 »

  1.   

    用引号把路径括起来
    "cmd.exe /c dir '" + file.getAbsolutePath() + "' /tc"); //注意这里用了单引号,也可以用双引号,但要用转义符
      

  2.   

    好像dos只能用双引号,那就用转义符吧
    "cmd.exe /c dir \"" + file.getAbsolutePath() + "\" /tc");
      

  3.   


    就是
    "cmd.exe /c dir 双引号" + file.getAbsolutePath() + "双引号 /tc");的意思
    双引号放在双引号里面,要进行转义
    "cmd.exe /c dir \\"" + file.getAbsolutePath() + "\\" /tc"); 
    我现在没环境,不能测试转义
    如果上面的转义不行,你直接用字符
    char[] qc = {(char)0x22}; String qs = new String(qc);
    "cmd.exe /c dir " + qs + file.getAbsolutePath() + qs + " /tc");
    总之,就是在你的file.getAbsolutePath()两边分别加上个双引号