android读写文件函数代码

    private void readFromFile() {
        try {            
            FileInputStream file = openFileInput("test.txt");
            byte[] buffer = new byte[file.available()];
            file.read(buffer);
            file.close();
            String name=new String(buffer); 
            TextView text = (TextView) findViewById(R.id.test);
            text.setText(name);
        } catch (IOException e) {
            e.printStackTrace();
        }        
    }

    private void writeToFile() {
        try {
            String str = "测试中文";
            FileOutputStream file = openFileOutput("test.txt", MODE_APPEND);
            file.write(str.getBytes());
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

posted on 2012-08-06 15:27  亭子  阅读(193)  评论(0编辑  收藏  举报

导航