简体   繁体   English

Android 文件不存在(但同时存在!)

[英]Android File Doesn't Exist (But does at the same time!)

I have an Android program that creates a file and checks for it in an if statement.我有一个 Android 程序,它创建一个文件并在 if 语句中检查它。 The purpose of this is to be able to save values in the app.这样做的目的是能够在应用程序中保存值。 The app works like this.该应用程序是这样工作的。 I put the values in on the phone and I crash the app to reset it.我将这些值输入到手机上,然后让应用程序崩溃以重置它。 The values were saved on the file.这些值已保存在文件中。 When I open the app again, it's supposed to skip the screen that lets me input the values.当我再次打开应用程序时,它应该跳过让我输入值的屏幕。 When I do this, the app acts as if the file never existed.当我这样做时,该应用程序就好像该文件从未存在过一样。 Upon debug, I found that the file was indeed created.在调试时,我发现该文件确实已创建。 It should've been created on internal storage, but I also added the permission for external storage writing just in case.它应该是在内部存储上创建的,但我还添加了外部存储写入权限以防万一。 Can someone help me or give me an alternative please?有人可以帮助我或给我一个替代方案吗?

File file = new File("user.txt");
    if (file.exists())
    {
        ...
    }
        File f = new File(); 

it just create a file instance of file in runtime.它只是在运行时创建文件的文件实例。 You need to use following code to create a file.您需要使用以下代码来创建文件。

        File file = new File("data/data/your package name/test.txt");
        if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

尝试给它一个完整的路径,而不仅仅是文件名,就像这样

File mFile = new File("/sdcard/mysdfile.txt");

The assets folder is read only at runtime.资产文件夹在运行时是只读的。

From http://developer.android.com/guide/topics/data/data-storage.html#filesInternal来自http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

Tip: If you want to save a static file in your application at compile time, save the file in your project res/raw/ directory.提示:如果要在编译时在应用程序中保存静态文件,请将文件保存在项目 res/raw/ 目录中。 You can open it with openRawResource(), passing the R.raw.您可以使用 openRawResource() 打开它,传递 R.raw。 resource ID.资源标识。 This method returns an InputStream that you can use to read the file (but you cannot write to the original file).此方法返回一个 InputStream 可用于读取文件(但不能写入原始文件)。

Take a look at using SharedPreferences for saving primitive data types.看看使用SharedPreferences来保存原始数据类型。

        Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

                File direct = new File(Environment.getExternalStorageDirectory() + "/Folder/Filename");
                if (!direct.exists()) {
                    direct.mkdir(); // directory is created;
                }
                if (isSDPresent) {
                    Path = Environment.getExternalStorageDirectory() + "/Folder/Filename";

                } else {
                    Path = getFilesDir() + "/Folder/Filename";
                }
        File f = new File(Path);
      if(!file.exists()) {
       try {
                        file.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM