2013-11-28 64 views
0

writefile()寫一個txt文件,並通過試驗似乎是成功的,但是當我嘗試讀取該文件,並在TextView的顯示,它不會顯示無法顯示在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(); 
     }  

} 
+0

應該是7行左右 –

+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); 
+0

Line.append(line);和line.append(「\ n」);都有錯誤的未定義類型字符串,但不附加採取字符串? –

+0

調試您的BufferReader和FileReader。檢查我的編輯答案。 –