简体   繁体   中英

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

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() . This method also takes a type argument to specify the type of subdirectory (such as DIRECTORY_MOVIES). If you don't need a specific media directory, pass null to receive the root directory of your app's private directory.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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