简体   繁体   中英

Why doesn't my viewtext display anything?

I'm writing a program that needs to be able to write to text files and read them again later. To read them I am setting them to a viewtext. However the view text never displays anything. I've narrowed down that myReader.readLine() returns NULL which either means that I'm not writing anything to the file or the more likely, I'm not reading the file properly.

Would really appreciate any help with this, thanks :)

public class openTable extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_opentable);
        File file = new File(getFilesDir()+"/table.xml");

        try {
            file.createNewFile();
            FileOutputStream fOut = new FileOutputStream(file);
            OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
            myOutWriter.append("Test\n");   
            fOut.close();
            Toast.makeText(openTable.this,"File Written",Toast.LENGTH_SHORT).show();}
        catch (Exception e) {
            Toast.makeText(openTable.this,"File Not Written",Toast.LENGTH_SHORT).show();
        }

        Log.d("FILEDIR",getFilesDir().toString());
        TextView displayFile = (TextView) findViewById(R.id.displayFile);

        try {
            FileInputStream fIn = new FileInputStream(file);
            BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
            String aDataRow = "";
            String aBuffer = "";

            if(myReader.readLine() != null) {
                Log.d("TEST","YES");
            } else{
                Log.d("TEST","NO");
            }

            while ((aDataRow = myReader.readLine()) != null) {
                Log.d("ADATAROW",aDataRow);
                aBuffer += aDataRow + "\n";
                    Log.d("ABUFFER",aBuffer);
            }

            displayFile.setText(aBuffer);
            myReader.close();       
            Toast.makeText(openTable.this,"File Read",Toast.LENGTH_SHORT).show();}
        catch (Exception e) {
            Toast.makeText(openTable.this,"File Not Read",Toast.LENGTH_SHORT).show();
        }
    }
}

There are a few things you should change.

  1. Before reading the data into your buffer you check if myReader.readLine() is null . The problem with this if-else block is that you read the first line of your file and throw away the result. Remember that readLine() returns a String . Since you only write one line to your file in your example you will never get it since you simply throw it away. So either remove your if-else block or save the result in a variable.

  2. When you write data to your file you should call myOutWriter.flush(); right after myOutWriter.append("Test\\n"); . This is good practice when working with streams and will ensure that you data gets written correctly.

Note : Just as a side note since this was mentioned in the comments: In the example that you provided you are not writing any data to the device storage. Therefore you won't need any permissions in your app since all your data is temporary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM