想要文件日期,但只能使用FileInputStream

问题描述 投票:0回答:1

我在一个应用上进行编码,并且已经设法使用以下代码对文本文件进行读写:

    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());

我尝试过使用相同的字符串作为fileName,但是它不会成为e文件。做的时候

file.exists(); // = false

对于文件名,我仅使用文件名,不使用路径。我以最简单的方式进行了读取/写入,并希望保持这种状态...

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

问候

java android file fileinputstream last-modified
1个回答
1
投票

用途:

File file = new File(ctx.getFilesDir(), fileName);
© www.soinside.com 2019 - 2024. All rights reserved.