【问题标题】:Read and Write text file from my own class in Android Studio在 Android Studio 中从我自己的类中读取和写入文本文件
【发布时间】:2015-04-13 00:39:28
【问题描述】:

我一直在尝试创建一个名为 TextFileReaderWriter 的类只需使用 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();

        }


    }

}

【问题讨论】:

  • 你的日志猫说什么?无论如何,首先你的 getfilecontent 没有读取你的文件,它读取你的变量文件内容,其次你写在你的应用程序的私有文件夹中
  • 您是有意将其存储在内部存储中,还是您的意思是外部存储?此外,您的 getFileContents() 实际上并没有返回文件内容;当您重新启动应用程序(或活动,甚至)时,数据将被 gc'd。
  • 我还没有编写 getFileContents,我只使用 setFileContents 方法......现在的问题是它没有将我传递给它的字符串写入文件。它确实创建了文件,但它是空的。
  • @Override protected void onPause() { super.onPause(); TextFileReaderWriter write = new TextFileReaderWriter(writetext.getText().toString(), getBaseContext()); write.setFileContents(writetext.getText().toString()); Log.d(TAG, writetext.getText().toString()); }

标签: java android class


【解决方案1】:

你不需要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();
        }
    }

【讨论】:

    【解决方案2】:

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

    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;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-17
      • 2014-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多