Write a string to a file

后端 未结 4 1947

I want to write something to a file. I found this code:

private void writeToFile(String data) {
    try {
        OutputStreamWriter outputStreamWriter = new Ou         


        
4条回答
  •  轮回少年
    2021-01-31 18:51

    This Method takes File name & data String as Input and dumps them in a folder on SD card. You can change Name of the folder if you want.

    The return type is Boolean depending upon Success or failure of the FileOperation.

    Important Note: Try to do it in Async Task as FIle IO make cause ANR on Main Thread.

     public boolean writeToFile(String dataToWrite, String fileName) {
    
                String directoryPath =
                        Environment.getExternalStorageDirectory()
                                + File.separator
                                + "LOGS"
                                + File.separator;
    
                Log.d(TAG, "Dumping " + fileName +" At : "+directoryPath);
    
                // Create the fileDirectory.
                File fileDirectory = new File(directoryPath);
    
                // Make sure the directoryPath directory exists.
                if (!fileDirectory.exists()) {
    
                    // Make it, if it doesn't exist
                    if (fileDirectory.mkdirs()) {
                        // Created DIR
                        Log.i(TAG, "Log Directory Created Trying to Dump Logs");
                    } else {
                        // FAILED
                        Log.e(TAG, "Error: Failed to Create Log Directory");
                        return false;
                    }
                } else {
                    Log.i(TAG, "Log Directory Exist Trying to Dump Logs");
                }
    
                try {
                    // Create FIle Objec which I need to write
                    File fileToWrite = new File(directoryPath, fileName + ".txt");
    
                    // ry to create FIle on card
                    if (fileToWrite.createNewFile()) {
                        //Create a stream to file path
                        FileOutputStream outPutStream = new FileOutputStream(fileToWrite);
                        //Create Writer to write STream to file Path
                        OutputStreamWriter outPutStreamWriter = new OutputStreamWriter(outPutStream);
                        // Stream Byte Data to the file
                        outPutStreamWriter.append(dataToWrite);
                        //Close Writer
                        outPutStreamWriter.close();
                        //Clear Stream
                        outPutStream.flush();
                        //Terminate STream
                        outPutStream.close();
                        return true;
                    } else {
                        Log.e(TAG, "Error: Failed to Create Log File");
                        return false;
                    }
    
                } catch (IOException e) {
                    Log.e("Exception", "Error: File write failed: " + e.toString());
                    e.fillInStackTrace();
                    return false;
                }
            }
    

提交回复
热议问题