SLaks 2013-08-12 09:24 采纳率: 0%
浏览 5060

写入文件然后读取,然后再像一个字符串一样保存内容

我想写进一个文件,然后,我想把内容保存在一个 StringBuffer 里面。最后再通过一个 TextView显示,但是什么也不显示。

public class MainActivity extends Activity {
String finall;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_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 (FileNotFoundException e) { e.printStackTrace(); } 
    catch (IOException e) { e.printStackTrace(); }



    FileInputStream in = null;
    try
    {
        in = openFileInput("hello_file.txt");
        StringBuffer fileContent = new StringBuffer("");

        byte[] buffer = new byte[1024];

        while(in.read(buffer) != -1)
        {
            fileContent.append(new String(buffer));
        }
        finall = fileContent.toString();
    }
    catch (FileNotFoundException e) { e.printStackTrace(); } 
    catch (IOException e) { e.printStackTrace(); }

    TextView text = (TextView)findViewById(R.id.mehmet);
    text.setText(finall);
}
}
  • 写回答

1条回答

  • ll_0520 2013-08-12 13:53
    关注

    读写文件的路径改为SD卡中,并加上写SD卡权限试试

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog