追加现有文件中的数据在Android和阅读 [英] append data in existing file in android and read it

查看:110
本文介绍了追加现有文件中的数据在Android和阅读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要追加在现有文件中的文本,但我不能读它(我可以读取第一个插入的数据),我不知道什么是错误的。
这是写code(保存文件):

 的FileOutputStream FOS = openFileOutput(测试,MODE_APPEND);
      ObjectOutputStream的OOS =新的ObjectOutputStream(FOS);      oos.writeObject(文本);      oos.flush();      oos.close();

这是如何读取(从文件中读取数据):

 的FileInputStream FIS = openFileInput(测试);      ObjectInputStream的OIS =新的ObjectInputStream(FIS);
  字符串s =(字符串)ois.readObject();      而(S!= NULL){
     Toast.makeText(getApplicationContext()中,s,Toast.LENGTH_SHORT).show();
      S =(字符串)ois.readObject();
      Toast.makeText(getApplicationContext()中,s,Toast.LENGTH_SHORT).show();
      }

恳求帮助我!有没有以书面形式或在读code

错误
解决方案

 公共布尔将writeToFile(字符串的文件名,字符串数据){
    尝试{
        FileOutputStream中FOS = openFileOutput(文件名,0);        OutputStreamWriter出=新OutputStreamWriter(openFileOutput(文件名,0));
        out.write(数据);
        out.close();
        返回true;
    }赶上(java.io.IOException异常五){
        e.printStackTrace();
        的System.out.println(-----在将writeToFile()的问题--------);
        返回false;
    }
}
公共字符串readFromFile(字符串XXX){
    StringBuffer的returnString =新的StringBuffer(); ;
    尝试{
        fstream的的FileInputStream = openFileInput(XXX);
        DataInputStream以在=新DataInputStream所(fstream的);
        BR的BufferedReader =新的BufferedReader(新的InputStreamReader(中));
        串strLine中;
        而((strLine中= br.readLine())!= NULL){
            returnString.append(strLine中);
        }
        附寄();
    }赶上(例外五){//捕获异常,如果任何
        e.printStackTrace();
        的System.out.println(-----在readFromFile()的问题--------);
    }    返回returnString.toString();
}

i want to append a text in an existing file but i can not read it(i can read the first inserted data) i do not know what is the mistake . this is the write code(save in file):

     FileOutputStream fos = openFileOutput("test",MODE_APPEND);        
      ObjectOutputStream oos = new ObjectOutputStream(fos);

      oos.writeObject(text);

      oos.flush();

      oos.close(); 

and this is how to read(read the data from the file):

     FileInputStream fis = openFileInput("test");

      ObjectInputStream ois = new ObjectInputStream(fis);
  String s=(String) ois.readObject();

      while(s != null){
     Toast.makeText(getApplicationContext(),s, Toast.LENGTH_SHORT).show();
      s=(String) ois.readObject();
      Toast.makeText(getApplicationContext(),s, Toast.LENGTH_SHORT).show(); 
      }

pleas help me !! are there a wrong in writing or in the reading code

解决方案

public boolean writeToFile(String filename,String data){
    try {
        FileOutputStream fos = openFileOutput(filename,0);

        OutputStreamWriter out = new OutputStreamWriter(openFileOutput(filename,0));
        out.write(data);
        out.close();
        return true;
    } catch (java.io.IOException e) {
        e.printStackTrace();
        System.out.println("-----problem in writeToFile()--------");
        return false;
    }
}


public String readFromFile(String xxx){
    StringBuffer returnString = new StringBuffer(""); ;
    try{ 
        FileInputStream fstream = openFileInput(xxx);
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        while ((strLine = br.readLine()) != null)   {
            returnString.append(strLine);
        }
        in.close();
    }catch (Exception e){//Catch exception if any
        e.printStackTrace();
        System.out.println("-----problem in readFromFile()--------");
    }

    return returnString.toString();
}

这篇关于追加现有文件中的数据在Android和阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆