Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileWriter not writing to file in Android?

I am trying to write to a new file named "filename.txt" and write out "hi" when I open an app. How do I go about this? I run this code in Eclipse, press F11, open up the AVD, and click on the app. I get the "Hello World, appname!" message, but no file is created.

package com.paad.comparison;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;

public class ComparisonOfControlCreationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
        String string1 = "Hey you";
        FileWriter fWriter;
        try{
            fWriter = new FileWriter("filename.txt", true);
            fWriter.write("hi");
            fWriter.flush();
            fWriter.close();
        } catch(Exception e) {
            e.printStackTrace();
        }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

EDIT: For anyone that happens to run into this problem in the future, make sure your AndroidManifest.xml file has the following line in it:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
like image 788
Miguel Diaz Avatar asked Jan 16 '23 04:01

Miguel Diaz


2 Answers

Try like this:

FileWriter fWriter;
File sdCardFile = new File(Environment.getExternalStorageDirectory() + " \filename.txt");
Log.d("TAG", sdCardFile.getPath()); //<-- check the log to make sure the path is correct.
try{
     fWriter = new FileWriter(sdCardFile, true);
     fWriter.write("hi");
     fWriter.flush();
     fWriter.close();
 }catch(Exception e){
          e.printStackTrace();
 }
like image 127
FoamyGuy Avatar answered Jan 28 '23 01:01

FoamyGuy


You need to call sync() before closing the file, but you need a FileDescriptor for that, and I don't believe it's available directly from FileWriter. However, you can create a FileWriter object using a FileDescriptor, and FileOutputStream can provide one for this purpose.

The following is based on your initial code, but I've opted to store the file on the sdcard in my example, so to run the code below as is your app manifest will need the correct permission added prior to the <application> tag.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

That said, the outcome won't be affected if you want store the file on the device directly.

Code (tested on a Desire HD):

package com.paad.comparison;

import java.io.FileOutputStream;
import java.io.FileWriter;

import android.app.Activity;
import android.os.Bundle;

public class ComparisonOfControlCreationActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        String string1 = "Hey you";

        FileOutputStream fos ;

        try {
            fos = new FileOutputStream("/sdcard/filename.txt", true);

            FileWriter fWriter;

            try {
                fWriter = new FileWriter(fos.getFD());
                fWriter.write("hi");
                fWriter.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                fos.getFD().sync();
                fos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Finally, as flush() is called by close(), calling flush() immediately prior to close() is superfluous, though not harmful.

like image 29
Chilledrat Avatar answered Jan 28 '23 01:01

Chilledrat