【问题标题】:Difference between default file paths in pure Java and in Android纯 Java 和 Android 中的默认文件路径之间的区别
【发布时间】:2017-07-15 21:39:50
【问题描述】:

我在理解 Android 中的路径时遇到了问题。我正在尝试检查文件是否存在。它在纯 Java 中运行良好,但在 Android 代码中失败,我以同样的方式给出路径(它只是一个文件名)。我知道该文件存在(在 Android 中),因为我在调用 File 类的 exists() 方法之前通过读取它来检查它。我可以毫无问题地读取文件,但存在检查返回 false。所以我的问题是:'normal''android' Java 在路径方面有什么区别?

这个问题似乎类似于 '为什么 file.exists() 返回 false?' 但我已经阅读了一些内容(很多)但没有找到答案(两者都 -如何检查Android中是否存在文件以及纯Java中的路径和Android中的Java有什么区别。

我在下面粘贴说明案例的代码。

这在 Android 中不起作用:

//--------------------------BUTTONS ACTIONS-----------------------------------------------------

public void onSaveButtonClick(View view){
    msg = textInput.getText().toString();

    try {
        FileOutputStream fos = openFileOutput(fileName, MODE_PRIVATE);
        fos.write(msg.getBytes());
        fos.close();
        Toast.makeText(getApplicationContext(), "Zapiasano!", Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void onLoadButtonClick(View view){

    loadedMsg = "";
    String tmp;
    try {
        FileInputStream fis = openFileInput(fileName);
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader bufferedReader = new BufferedReader(isr);
        StringBuffer stringBuffer = new StringBuffer();
        while ((tmp=bufferedReader.readLine()) != null){
            loadedMsg += tmp + "\n";
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    textDisplay.setText(loadedMsg);

    //----------------------FILE CHECK---------------------------------------------

    File f = new File(fileName);
    if(f.exists()){
        textDisplay.setText("File exsists");
    } else{
        textDisplay.setText("File doesn't exsists");
    }
}

这适用于纯 Java:

public static void main(String[] args) {

    String fileName = "test.file";
    String str = "hello kitty!";
    String loaded = "this should not load";

    //-----------------SAVE------------------------------------------------
    try {
            FileOutputStream fos;
            fos = new FileOutputStream(fileName);
            fos.write(str.getBytes());
            fos.close();
            System.out.println("saved");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FileExists.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(FileExists.class.getName()).log(Level.SEVERE, null, ex);
        }

    //------------------LOAD -----------------------------------------------
    try {
        FileInputStream fis = new FileInputStream(fileName);
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader bufferedReader = new BufferedReader(isr);
        loaded = bufferedReader.readLine();
        isr.close();
        fis.close();

    } catch (FileNotFoundException ex) {
        Logger.getLogger(FileExists.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(FileExists.class.getName()).log(Level.SEVERE, null, ex);
    }

    System.out.println(loaded);

    //----------------------FILE CHECK---------------------------------------------
    File file = new File(fileName);
    if(file.exists()){
        System.out.println("file exsists");
    }
}

输出:

保存

你好凯蒂!

文件存在

【问题讨论】:

    标签: java android


    【解决方案1】:

    所以我的问题是:在路径方面,“普通”Java 和“安卓”Java 有什么区别?

    其他 Java 环境假定某个当前工作目录。出于所有意图和目的,Android 不会。

    您的 Android 代码假定以下三件事是相关的:

    FileOutputStream fos = openFileOutput(fileName, MODE_PRIVATE);
    FileInputStream fis = openFileInput(fileName);
    File f = new File(fileName);
    

    前两个是相关的。第三个是没有意义的。等效的行是:

    File f = new File(getFilesDir(), fileName);
    

    在 Android 中,您总是使用框架提供的方法确定文件系统路径,从而为您提供工作的基本目录(getFilesDir()getExternalFilesDir()Environment 上的方法等) .

    【讨论】:

    • 谢谢 - 这解决了我的两个问题(有什么区别以及如何检查文件是否存在)。
    【解决方案2】:

    FileOutputStream(filename) 的实现都将文件保存在filename 给出的位置。所以他们有点假设它实际上是文件的绝对路径。

    不同之处在于他们用于到达该路径的“参考”。

    您系统上的 Java 会看到相对于源位置的路径, 所以你使用FileOutputStream(filename)将文件保存在//filename,然后使用File(filename)/<reference path>/filename获取该文件,你总能在那里找到该文件。

    Android 的 FileOutputStream(filename) 可以看到相对于您的包目录中的 files/ 目录的位置。虽然它对File(pathname) 的实现请查看相对于根目录的路径。

    因此,在 android 中,当您使用FileOutputStream(filename) 写入时,您正在写入文件/<path to your package directory>/files/filename,但是当您使用File() 时,您实际上尝试访问根目录下的文件,即/filename,这,并不存在。

    改为尝试:

    ....
    File f = new File (YourActivity.this.getFilesDir().getAbsolutePath() + filename);
    ...
    

    【讨论】:

    • 谢谢,你的回答很有帮助!
    猜你喜欢
    • 1970-01-01
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    • 2015-12-18
    • 1970-01-01
    • 2016-02-27
    相关资源
    最近更新 更多