在Android Studio中从我自己的类读取和写入文本文件

时间:2015-04-13 00:39:29

标签: java android class

我一直在尝试创建一个名为TextFileReaderWriter的类我想使用getter和setter来读取和写入文本文件,这样我就可以从任何地方调用类和方法只需使用setfileContents(somestring)somestring = getfileContents()这样的程序即可 例如:

TextFileReaderWriter trw = new TextFileReaderWriter();
trw.setfileContents(somestring); //this would write 'somestring' to the text file.
String somestring = trw.getfileContents(); //this would return 'somestring' from the text file.

这是我到目前为止所做的,但它没有对文件写入任何内容:

public class TextFileReaderWriter extends Activity{

            String fileContents;
            Context context;
            String TAG = "MYTAG";

    public TextFileReaderWriter(String fileContents, Context context) {
        this.fileContents = fileContents;
        this.context = context;
    }

    public String getFileContents() {
        return fileContents;
    }

    public void setFileContents(String fileContents) {
        this.fileContents = fileContents;

        FileOutputStream fos = null;
        try {
            fos = context.openFileOutput("UserInputStore", Context.MODE_PRIVATE);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        OutputStreamWriter osw = new OutputStreamWriter(fos);

        try {
            osw.write(fileContents);
            Log.d(TAG, fileContents);

        } catch (IOException e) {
            e.printStackTrace();

        }


    }

}

2 个答案:

答案 0 :(得分:0)

你不需要OutputStreamWriter - FileOutputStream就可以了。

    //what you had before
    FileOutputStream fos = null;
    try {
        fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    //use just the file output stream to write the data
    //data here is a String
    if (fos != null) {
        try {
            fos.write(data.getBytes());
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

答案 1 :(得分:0)

在磁盘上保存数据的方法:

protected static void saveDataOnDisk(String data) {

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

try {

ObjectOutput objectOutput = new ObjectOutputStream(byteArrayOutputStream);

objectOutput.writeObject(data);

byte[] buffer = byteArrayOutputStream.toByteArray();

File loginDataFile = (new File(filePath)); // file path where you want to write your data

loginDataFile.createNewFile();

FileOutputStream fileOutputStream = new FileOutputStream(loginDataFile);

fileOutputStream.write(buffer);

fileOutputStream.close();

objectOutput.flush();

objectOutput.close();

byteArrayOutputStream.flush();

byteArrayOutputStream.close();

Log.i(“SAVE”, ”———————-DONE SAVING”);

} catch(IOException ioe) {

Log.i(“SAVE”, “———serializeObject|”+ioe);

}

}

从磁盘获取数据的方法:

private static Object getDataFromDisk() {

try {

FileInputStream fileInputeStream = new FileInputStream(FilePath);

ObjectInputStream objectInputStream = new ObjectInputStream(fileInputeStream);

Object data = (Object) objectInputStream.readObject();

objectInputStream.close();

fileInputeStream.close();

return dataModel;

} catch (Exception error) {

Log.i(“FETCH”, ”—-getDataFromDisk———ERROR while reading|” + error);

}

return null;

}