简体   繁体   English

无法在内部存储中创建文件

[英]Can't create file in the internal storage

i am trying to create a file in the internal storage, i followed the steps in android developers website but when i run the below code there is no file created我正在尝试在内部存储中创建一个文件,我按照 android 开发人员网站中的步骤操作,但是当我运行以下代码时,没有创建文件

please let me know what i am missing in the code请让我知道我在代码中缺少什么

code :代码

File file = new File(this.getFilesDir(), "myfile");
    try {
        file.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    FileOutputStream fOut = null;
    try {
        fOut = openFileOutput("myfile",Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        fOut.write("SSDD".getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

默认情况下,这些文件是私有的,只有您的应用程序可以访问并在用户删除您的应用程序时被删除

For saving file:用于保存文件:

public void writeToFile(String data) {
    try {
        FileOutputStream fou = openFileOutput("data.txt", MODE_APPEND);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fou);
        outputStreamWriter.write(data);
        outputStreamWriter.close();
    }
    catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    }
}

For loading file:对于加载文件:

public String readFromFile() {

    String ret = "";

    try {
        InputStream inputStream = openFileInput("data.txt");

        if ( inputStream != null ) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString = "";
            StringBuilder stringBuilder = new StringBuilder();

            while ( (receiveString = bufferedReader.readLine()) != null ) {
                stringBuilder.append(receiveString);
            }

            inputStream.close();
            ret = stringBuilder.toString();
        }

    }
    catch (FileNotFoundException e) {
        Log.e("login activity", "File not found: " + e.toString());
    } catch (IOException e) {
        Log.e("login activity", "Can not read file: " + e.toString());
    }

    return ret;
}

Try to get the path for storing files were the app has been installed.The below snippet will give app folder location and add the required permission as well.尝试获取已安装应用程序后存储文件的路径。以下代码段将提供应用程序文件夹位置并添加所需的权限。

 File dir = context.getExternalFilesDir(null)+"/"+"folder_name";

If you are handling files that are not intended for other apps to use, you should use a private storage directory on the external storage by calling getExternalFilesDir() .如果您正在处理不适合其他应用程序使用的文件,您应该通过调用getExternalFilesDir()使用外部存储上的私有存储目录​​。 This method also takes a type argument to specify the type of subdirectory (such as DIRECTORY_MOVIES).此方法还采用类型参数来指定子目录的类型(例如 DIRECTORY_MOVIES)。 If you don't need a specific media directory, pass null to receive the root directory of your app's private directory.如果您不需要特定的媒体目录,请传递 null 以接收应用程序私有目录的根目录。

Probably, this would be the best practice.也许,这将是最佳实践。

Use this method to create folder使用此方法创建文件夹

public static void appendLog(String text, String fileName) {
    File sdCard=new File(Environment.getExternalStorageDirectory().getPath());
    if(!sdCard.exists()){
        sdCard.mkdirs();
    }
    File logFile = new File(sdCard, fileName + ".txt");
    if (logFile.exists()) {
        logFile.delete();
    }
    try {
        logFile.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        //BufferedWriter for performance, true to set append to file flag
        BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
        buf.write(text);
        buf.newLine();
        buf.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

In this method, you have to pass your data string as a first parameter and file name which you want to create as second parameter.在此方法中,您必须将数据字符串作为第一个参数传递,并将要创建的文件名作为第二个参数传递。

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

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