Search code examples
androidadbtext-files

creating text files Android IO


I am trying to make a text file on an android phone and be able to populate it when needed. I am having problems, obviously, and would see if anyone can take a look at what I have so far.

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class ViewLog extends Activity {

    private TextView tv;
    private static final String TAG = "MEDIA";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.TextView01);

        CreateExternalLogFile("I am adding something into the text file!");
    }

private void CreateExternalLogFile(String s){
    File root = android.os.Environment.getExternalStorageDirectory(); 
    tv.append("\nExternal file system root: "+root);
    File dir = new File (root.getAbsolutePath() + "/logs");
    dir.mkdirs(); 
    File file = new File(dir,"log.txt");
    try {
        FileOutputStream f = new FileOutputStream(file,true); //True = Append to file, false = Overwrite
        PrintWriter pw = new PrintWriter(f);
        System.out.println(s);
        pw.println(s);
        pw.flush();
        pw.close();
        f.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.i(TAG, "******* File not found. Did you" +
                        " add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
    } catch (IOException e) {
        e.printStackTrace();
    }   
    tv.append("\nFile written to \n"+file);


}
}

Any help would be appreciated. I have used adb to look for the file, but the file is not being created.


Solution

  • Edit: This works - Just tested it.

    Make sure you have: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in your manifest as well.

     package com.example.fileio;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.PrintStream;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    import android.widget.TextView;
    
    public class FilioActivity extends Activity {
    
        private TextView tv;
        private static final String TAG = "MEDIA";
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            tv = (TextView) findViewById(R.id.TextView01);
    
            CreateExternalLogFile("I am adding something into the text file!");
        }
    
    private void CreateExternalLogFile(String s){
        File sdCard = Environment.getExternalStorageDirectory();
        File dir = new File (sdCard.getAbsolutePath());
        dir.mkdirs();
        File file = new File(dir, "filename.txt");
        tv.append("\nExternal file system root: "+ dir);
        try {
            FileOutputStream f = new FileOutputStream(file,false); //True = Append to file, false = Overwrite
            PrintStream p = new PrintStream(f);
            p.print(s);
            p.close();
            f.close();
    
    
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Log.i(TAG, "******* File not found. Did you" +
                            " add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
        } catch (IOException e) {
            e.printStackTrace();
        }   
        tv.append("\nFile written to \n"+file);
    
    
    }
    }