【问题标题】:Want the file date but can only use FileInputStream想要文件日期但只能使用 FileInputStream
【发布时间】:2019-11-28 21:07:50
【问题描述】:

我正在编写一个应用程序,并设法使用如下代码读取和写入文本文件:

    StringBuffer stringBuffer = new StringBuffer();
    try{
        FileInputStream fileInputStream = ctx.openFileInput(fileName);
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);

        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

        String lines;
        int counter = 0;
        while((lines = bufferedReader.readLine())!=null){
            if(counter>0){
                stringBuffer.append("\n");
            }
            stringBuffer.append(lines);
            counter++;
        }
    }
    catch (FileNotFoundException ex){
        throw ex;
    }
    catch (IOException ex){
        ex.printStackTrace();
        return null;
    }

但我也想获取文件的(最后修改)日期。

我发现了这样的代码:

    File file = new File(fileName);
    String path = file.getAbsolutePath();

    Date lastModDate = new Date(file.lastModified());

我试过这个,对文件名使用相同的字符串,但它没有变成 e 文件。 做的时候

file.exists(); // = false

对于文件名,我只使用文件名,没有路径。 我以最简单的方式进行了读/写,并希望保持这种方式......

文件是否缺少路径?在这种情况下,我可以从 FileInputStream 中获取路径吗?

问候

【问题讨论】:

    标签: java android file fileinputstream last-modified


    【解决方案1】:

    用途:

    File file = new File(ctx.getFilesDir(), fileName);
    

    【讨论】:

      猜你喜欢
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      • 2023-01-25
      • 1970-01-01
      • 2016-02-07
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      相关资源
      最近更新 更多