有没有什么方法能在一个文件当中获取两组数据呢,
看完这个视频自己做了个小demo发现获取是能获取到,但是数据被覆盖了,最后得到的只有密码的数据,
package com.example.nete.myapplication; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText usernameET; private EditText passwordET; private Button loginBT; private TextView nameTV; private TextView passTV; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化需要使用到的控件 loginBT = (Button) findViewById(R.id.loginBT); passwordET = (EditText) findViewById(R.id.passwordET); usernameET = (EditText) findViewById(R.id.usernameET); nameTV = (TextView) findViewById(R.id.name); passTV = (TextView) findViewById(R.id.pass); loginBT.setOnClickListener(this); } @Override public void onClick(View view) { //最后获取输出框字符串 WriteFile(usernameET.getText().toString()); nameTV.setText("用户名:" + ReadFile()); WriteFile(passwordET.getText().toString()); passTV.setText("密 码:" + ReadFile()); } public void WriteFile(String content) { FileOutputStream fos = null; try { fos = openFileOutput("login.txt", MODE_PRIVATE); fos.write(content.getBytes());//写入获得到的用户名密码 fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public String ReadFile() { FileInputStream fis = null; ByteArrayOutputStream baos = null; //新建局部变量用于保存读取到的数据 String content = null; byte[] buffer = null; try { fis = openFileInput("login.txt");//获取文件输入流 buffer = new byte[1000];//定义一次读取的字节数,不要太大 baos = new ByteArrayOutputStream();//定义用于存放数据的写入内容 int len = 0; //添加一个循环语句,当len==-1,就说明此时的数据已经读取完毕 while ((len = fis.read(buffer)) != -1) { baos.write(buffer, 0, len); } content = baos.toString(); //将获取到的数据传到content fis.close();//关闭文件输入流 baos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return content; } }