无法在textview中显示文件

时间:2013-11-28 04:23:28

标签: java android file-io textview

writefile()写了一个txt文件并通过测试似乎是成功的,但当我尝试读取文件并在textview上显示它时,它不会显示

public class WriteRead extends Activity implements OnClickListener {
    Button btnSave;
    Button btnNext;
    String nametxt;
    String healthCardtxt; 
    String dobtxt;
    String arrivaltxt;
    String heartRatetxt;
    String Temptxt;
    String bptxt;       

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_write_read);

        Button btnSave = (Button) findViewById(R.id.btnSave);
        Button btnNext = (Button) findViewById(R.id.btnNext);
        EditText Name = (EditText) findViewById(R.id.editText1); 
        EditText DOB = (EditText) findViewById(R.id.editText2); 
        EditText HealthNum = (EditText) findViewById(R.id.editText3); 
        EditText Arrival = (EditText) findViewById(R.id.editText4); 
        EditText HeartRate = (EditText) findViewById(R.id.editText5); 
        EditText Temp = (EditText) findViewById(R.id.editText6); 
        EditText BPressure = (EditText) findViewById(R.id.editText7); 
        nametxt = Name.getText().toString();
        healthCardtxt = HealthNum.getText().toString();
        dobtxt = DOB.getText().toString();
        arrivaltxt = Arrival.getText().toString();
        heartRatetxt = HeartRate.getText().toString();
        Temptxt = Temp.getText().toString();; 
        bptxt = BPressure.getText().toString();

        btnNext.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v) {
                readfile();                 
            }

        });

        btnSave.setOnClickListener(new View.OnClickListener(){

            public void onClick(View arg0) {
                writefile();
                readfile();
            }

        });

    }

    protected void readfile() {
        // TODO Auto-generated method stub
        String FILENAME = (nametxt + ".txt"); 

这是我尝试过的第一个方法,我从youtube上的教程中学到了这个方法,但它似乎没有用。

    /*try {
        FileInputStream reader = openFileInput(FILENAME);
        InputStreamReader isr = new InputStreamReader(reader);
        BufferedReader br = new BufferedReader(isr);

        String sLine = null;
        String out = "";

        while((sLine = br.readLine()) != null){

            out += sLine;

        }
        Toast.makeText(this,out, Toast.LENGTH_SHORT).show();
    } catch (FileNotFoundException e) {
         //TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }*/

这是我试图在textview上阅读和显示但由于某种原因没有显示的方法,我不确定为什么没有错误。此方法读取文件中的每一行并附加到fileDisplay。

    TextView fileDisplay = (TextView)findViewById(R.id.display);

    try{

        FileReader fr = new FileReader(FILENAME);
        BufferedReader br = new BufferedReader(fr);
        String line = null;
        try{
            while(br.readLine() != null)
            {
                line = br.readLine();
                fileDisplay.append(line);
                fileDisplay.append("\n"); 
            }
        }catch(IOException e){
            e.printStackTrace();
        }
    }catch (FileNotFoundException e){
        e.printStackTrace();
    }

}

protected void writefile() {



    String FILENAME = (nametxt + ".txt"); 
    String nameContent = nametxt; 
    String dobContent = dobtxt; 
    String healthContent = healthCardtxt; 
    String arrivalContent = arrivaltxt; 
    String heartContent = heartRatetxt; 
    String tempContent = Temptxt; 
    String bpContent = bptxt; 




    FileOutputStream fos;
    try {
        fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
        fos.write(nameContent.getBytes());
        fos.write('\n');
        fos.write(dobContent.getBytes());
        fos.write('\n');
        fos.write(healthContent.getBytes());
        fos.write('\n');
        fos.write(arrivalContent.getBytes());
        fos.write('\n');
        fos.write(heartContent.getBytes());
        fos.write('\n');
        fos.write(tempContent.getBytes());
        fos.write('\n');
        fos.write(bpContent.getBytes());
        fos.close();

        Toast.makeText(this, "File saved.", Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {                   
            e.printStackTrace();
        }       

}

1 个答案:

答案 0 :(得分:0)

更改

while(br.readLine() != null)
            {
                line = br.readLine();
                fileDisplay.append(line);
                fileDisplay.append("\n"); 
            }

while(br.readLine() != null)
            {
                line = br.readLine()+"\n";
                System.out.println(line);
            }
 fileDisplay.setText(line);