celine_chen 2017-03-27 16:12 采纳率: 33.3%
浏览 893

我用的centos6.3,想用java获取文件的创建时间,该怎么办?

网上有说生成dll文件那个不会啊。。。还有要使用window下cmd那个,我在centos应该不能用吧
package hadoop;

import java.io.BufferedReader;

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

public class readcreatetime {
public static void main(String[] args)

{

try

{

String fileCreated = getFileCreated("/mnt/hgfs/share/test/{62B8B091-7322-448E-9FDE-CD37A646E868}.bmp");

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;  
    }  

}
这样获取出来的是null

  • 写回答

1条回答 默认 最新

  • qq_23830427 2017-03-28 02:08
    关注

    当成参数传进去可以么

    通过shell执行程序,创建时间当成参数传进去

    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?