问题描述
- 写入文件然后读取,然后再像一个字符串一样保存内容
-
我想写进一个文件,然后,我想把内容保存在一个 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); } }
解决方案
读写文件的路径改为SD卡中,并加上写SD卡权限试试
时间: 2024-12-02 08:11:12