文件读写操作查询

时间:2018-05-20 10:39:01

标签: android file file-io fileinputstream fileoutputstream

package com.example.vibhor.myfile;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileInputStream;


public class MainActivity extends AppCompatActivity {

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

        /*final EditText ed1=(EditText)findViewById(R.id.editText);
        final EditText ed2=(EditText)findViewById(R.id.editText2);*/
        Button bn1=(Button) findViewById(R.id.button);
        Button bn2=(Button)findViewById(R.id.button2);
        //final TextView tx1=(TextView) findViewById(R.id.textView);

            bn1.setOnClickListener(new View.OnClickListener() {

                EditText ed1=(EditText)findViewById(R.id.editText);
                EditText ed2=(EditText)findViewById(R.id.editText2);
                String a=ed1.getText().toString();
                String b=ed2.getText().toString();


                @Override
                public void onClick(View v) {

                    if (a.equalsIgnoreCase("")||b.equalsIgnoreCase(""))
                    {
                        Toast.makeText(getApplicationContext(),"Kindly fill the fields first",Toast.LENGTH_SHORT).show();
                    }

                    else {
                        try {
                            FileOutputStream fos = openFileOutput("mydata.txt", MODE_APPEND);
                            fos.write(a.getBytes());
                            //fos.write("/t".getBytes());
                            fos.write(b.getBytes());
                            //fos.write("/n".getBytes());
                            fos.close();
                            //Toast.makeText(getApplicationContext(), a, Toast.LENGTH_SHORT).show();
                            //Toast.makeText(getApplicationContext(), b, Toast.LENGTH_SHORT).show();
                            Toast.makeText(getApplicationContext(), "Data added in the file", Toast.LENGTH_SHORT).show();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            });


            bn2.setOnClickListener(new View.OnClickListener() {

                TextView tx1=(TextView) findViewById(R.id.textView);

                @Override
                public void onClick(View v) {
                    try {
                        FileInputStream fis = openFileInput("mydata.txt");
                        byte[] reader=new byte[fis.available()];
                        while(fis.read(reader)!=-1)
                        {
                            String c=(new String(reader));
                            tx1.setText(c);
                            fis.close();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
    }
}

我已插入两个EditText框,我希望用户在其中添加一些字符串,该字符串应添加到我创建的文件中。

但是用户在EditText字段中输入的String在我创建的文件中显示为空白。因此,当我获取数据时它没有显示任何内容,因为它在EditText中没有任何字段。

在这种情况下可以做些什么?

0 个答案:

没有答案