在android中创建文本文件并保存数据



我是android应用程序的新手。

在下面的代码中,我想通过一些edittext从用户那里获取输入,然后将它们保存到一个文件中。我使用安卓工作室,并给予输入后,我检查了文件,它总是空的。我尝试显示内容,但仍然没有得到输出。代码有什么问题?

请帮帮我。

public class user extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user);
    nextClick();
}
public void nextClick()
{
    final Context context = this;
    Button next =(Button) findViewById(R.id.userNext);
    next.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           try {
               EditText e1 = (EditText) findViewById(R.id.name);
               EditText e2 = (EditText) findViewById(R.id.designation);
               EditText e3 = (EditText) findViewById(R.id.orgname);
               EditText e4 = (EditText) findViewById(R.id.address);
               EditText e5 = (EditText) findViewById(R.id.city);
               EditText e6 = (EditText) findViewById(R.id.state);
               EditText e7 = (EditText) findViewById(R.id.zip);
               FileOutputStream fos = openFileOutput("data.txt", Context.MODE_WORLD_WRITEABLE);
               OutputStreamWriter osw = new OutputStreamWriter(fos);
               osw.write(e1.toString());
               fos.flush();
               fos.close();
           }
           catch(Exception e) {
           }
            try
                  {
              FileInputStream fis = new FileInputStream("data.txt");
               while(fis.read()!=-1)
               {
                   System.out.println((char)fis.read());
               }
           }
           catch(Exception e)
           {
           }
           Intent intent3 = new Intent(context,com.example.cheruvu.rs.user2.class);
           startActivity(intent3);
       }
   });
}
}

当您写入文件时,需要从EditText字段中读取getText(),如下所示

osw.write(e1.getText().toString());

这个怎么样?这将在你的手机(不是sd卡)上创建文件

File myFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "MyFileName");
FileOutpuStream fos = new FileOutputStream(myFile);

试着用这个替换你的代码,看看它是否有效

public class user extends Activity {
EditText e1;
EditText e2;
EditText e3;
EditText e4;
EditText e5;
EditText e6; 
EditText e7; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user);
    e1 = (EditText) findViewById(R.id.name);
    e2 = (EditText) findViewById(R.id.designation);
    e3 = (EditText) findViewById(R.id.orgname);
    e4 = (EditText) findViewById(R.id.address);
    e5 = (EditText) findViewById(R.id.city);
    e6 = (EditText) findViewById(R.id.state);
    e7 = (EditText) findViewById(R.id.zip);
    nextClick();
}
public void nextClick()
{
    final Context context = this;
    Button next =(Button) findViewById(R.id.userNext);
    next.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           try {
               FileOutputStream fos = openFileOutput("data.txt", Context.MODE_WORLD_WRITEABLE);
               OutputStreamWriter osw = new OutputStreamWriter(fos);
               osw.write(e1.getText().toString());
               fos.flush();
               fos.close();
           }
           catch(Exception e) {
           }
            try
                  {
              FileInputStream fis = new FileInputStream("data.txt");
               while(fis.read()!=-1)
               {
                   System.out.println((char)fis.read());
               }
           }
           catch(Exception e)
           {
           }
           Intent intent3 = new Intent(context,com.example.cheruvu.rs.user2.class);
           startActivity(intent3);
       }
   });
}
}

相关内容

  • 没有找到相关文章

最新更新