开发者

Best way to write a text file to internal sorage?

I'm trying to write a text file to an internal storage and wondered what's the best way would be to do it, the text file will contain strings.

I have:

    File file = new File(getFilesDir() + subFolderName + "/" + fileName);
                BufferedWriter writer;


                try {
 开发者_如何转开发                   writer = new BufferedWriter(new FileWriter(file));

                    writer.write("ID, Date, Address, Body");
                    writer.write("\n");
                    for (String s : list) {
                        writer.write(s);
                        writer.write("\n");
                    }
                    writer.write("\n");
                    writer.flush();
                    writer.close();
                } catch (IOException e) {                   
                    e.printStackTrace();
}

I just get a java.lang.NullPointerException at the creation of the new file :/

Thanks in advance.

This is the code which i ended up with which worked as i wanted it to:

public void WriteFileInternal(ArrayList<String> list, String FileName, Context context) {

    BufferedWriter bw;
    try {
        ContextWrapper cw = new ContextWrapper(context);
        File directory = cw.getDir("SMSMonitor", Context.MODE_PRIVATE);

        if (!directory.exists()){
            directory.createNewFile();
            directory.mkdir();

            }
        File file = new File(directory +"/"+ FileName);
        file.createNewFile();

        bw = new BufferedWriter(new FileWriter(file));
        bw.write("ID, Date, Body, Phone Number");
        bw.write("\n");

        for (String s1 : list) {
            bw.write(s1);
            bw.write("\n");
        }
        bw.write("\n");
        bw.flush();
        bw.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}


I had this method to store information about the user and password (still need to be encripted, but you can use it as a basis)

public void saveUserData(String user, String password){
    FileOutputStream fos; 
    DataOutputStream dos;
    try {
        File f = this.getFilesDir();
        String s = f.getCanonicalPath();
        File file= new File(s + "/user.txt");
        if(file.exists()){
            file.delete();
        }
        file.createNewFile();
        fos = new FileOutputStream(file);
        dos = new DataOutputStream(fos);
        dos.write(user.getBytes());
        dos.writeChars("\n");
        dos.write(password.getBytes());
    } catch (IOException e) {       
        e.printStackTrace();
    }
}


package com.file;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

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

public class FileWrite extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    String FILENAME = "hello_file";
    String string = "hello world!";

    FileOutputStream fos;
    try {
        fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
           fos.write(string.getBytes());
            fos.close();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜