【问题标题】:Cannot display file in textview无法在文本视图中显示文件
【发布时间】:2013-11-28 04:23:28
【问题描述】:

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();
        }       

}

【问题讨论】:

  • 应该是7行左右
  • 你检查了吗? (对不起,我不小心删除了我上面的评论。)

标签: java android file-io textview


【解决方案1】:

改变

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

【讨论】:

  • Line.append(line);和 line.append("\n");两者都有未定义类型 String 的错误,但不追加采用 String?
  • 调试您的 BufferReader 和 FileReader。检查我的编辑答案。
猜你喜欢
  • 1970-01-01
  • 2019-03-13
  • 1970-01-01
  • 1970-01-01
  • 2019-11-28
  • 1970-01-01
  • 2020-04-11
  • 1970-01-01
  • 2022-01-13
相关资源
最近更新 更多