注册 登录
编程论坛 Android开发

关于从文件中读取数据的问题

yu1543054075 发布于 2015-12-02 21:33, 3485 次点击
package com.example.wenjiancunchu;

import
import
import
import

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    private static final String FILE_NAME="file io.txt";
    private EditText mEdtIn, mEdtFileContent;
    private Button mBtnAdd, mBtnRead, mBtnClear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setupViewComponent();
    }
   
    public void setupViewComponent() {
        mEdtIn = (EditText) findViewById(R.id.edtIn);
        mEdtFileContent = (EditText) findViewById(R.id.edtFileContent);
        
        mBtnAdd =(Button) findViewById(R.id.btnAdd);
        mBtnRead = (Button) findViewById(R.id.btnRead);
        mBtnClear = (Button ) findViewById(R.id.btnClear);
        
        mBtnAdd.setOnClickListener(listener1);
        mBtnRead.setOnClickListener(listener2);
        mBtnClear.setOnClickListener(listener3);
    }
   
    private Button.OnClickListener listener1 = new Button.OnClickListener() {
        public void onClick(View v) {
            FileOutputStream fileOut = null; //写入文件
            BufferedOutputStream bufFileOut = null;
            try {
                fileOut = openFileOutput(FILE_NAME, MODE_APPEND);
                bufFileOut = new BufferedOutputStream(fileOut);//这个步骤可以提升大型文件的读写效率,但不是必要的步骤
                bufFileOut.write(mEdtIn.getText().toString().getBytes());//将数据写入文件中,但是要写入的数据必须存储在byte类型的数组中,利用String类的getBytes()方法取得字符串的byte数组
                bufFileOut.close();//关闭文件
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
    };
   
    private Button.OnClickListener listener2 = new Button.OnClickListener() {
        public void onClick(View v) {
            FileInputStream fileIn = null; //读取文件
            BufferedInputStream bufFileIn = null;
            
            try {
                fileIn = openFileInput("file io.txt"); //调用openFileInput()方法从Android系统取得一个FileInputStream类型的对象
                bufFileIn = new BufferedInputStream(fileIn);
                byte[] bufBytes = new byte[10];
                mEdtFileContent.setText("");
                do {
                  
                    int c = bufFileIn.read(bufBytes);//读取数据
                    if(c==-1) //如果传回-1表示数据已经读取完毕
                        break;
                    else
                        mEdtFileContent.append(new String(bufBytes), 0, c);//EditText继承了TextView,而append是TextView中的方法
                }while(true);
                bufFileIn.close();
            }
            catch(Exception e){
                e.printStackTrace(); }
            }
    };
   
    private Button.OnClickListener listener3 = new Button.OnClickListener() {
        public void onClick(View v) {
            FileOutputStream fileOut = null;//向文件中写入
            try {
                fileOut = openFileOutput(FILE_NAME, MODE_PRIVATE);// MODE_PRIVATE或者0,它会将原来文件中的数据清除后再写入新的数据,如果是MODE_APPEND,是附加模式,它会将原来的数据加在文件的最后
                fileOut.close();
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
    };
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
只有本站会员才能查看附件,请 登录

为什么我存入文件中的 数据为my data,而从文件中读取出来的就只有data,还请大家帮忙看看,谢谢了
0 回复
1