我正在創建一個android應用程序,它記錄五個EditText輸入並將它們存儲在一個文件中。我需要知道的是如何單獨檢索每個文件並將結果輸出到另一個EditText字段。如何從文件中檢索特定的字符串? (android)
MyExampleCode:
package chuck.com;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
import android.content.Intent;
public class FillFile extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fill_file);
Button submit = (Button) findViewById(R.id.button1);
submit.setOnClickListener(sub);
Button disp = (Button) findViewById(R.id.button2);
disp.setOnClickListener(dis);
}
private OnClickListener sub = new OnClickListener(){
public void onClick(View v){
try {
EditText one = (EditText) findViewById(R.id.editText1);
EditText two = (EditText) findViewById(R.id.editText2);
EditText three = (EditText) findViewById(R.id.editText3);
EditText four = (EditText) findViewById(R.id.editText4);
EditText five = (EditText) findViewById(R.id.editText5);
FileOutputStream fos;
fos = openFileOutput("XML.txt", MODE_APPEND);
String uno = one.getText().toString();
String dos = two.getText().toString();
String tres = three.getText().toString();
String quatro = four.getText().toString();
String cinco = five.getText().toString();
fos.write(uno.getBytes());
fos.write(dos.getBytes());
fos.write(tres.getBytes());
fos.write(quatro.getBytes());
fos.write(cinco.getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
private OnClickListener dis = new OnClickListener(){
public void onClick(View v){
Intent transf = new Intent(v.getContext(), MainActivity.class);
startActivityForResult(transf, 0);
}
};
}
的XML佈局,我想將文件導出到:
package chuck.com;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File fileDir = getFilesDir();
String fileName = ("XML.txt");
File backup = new File(fileDir, fileName);
boolean content = backup.length() == 0;
try {
backup.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TextView one = (TextView) findViewById(R.id.textView1);
TextView two = (TextView) findViewById(R.id.textView2);
TextView three = (TextView) findViewById(R.id.textView3);
TextView four = (TextView) findViewById(R.id.textView4);
TextView five = (TextView) findViewById(R.id.textView5);
EditText eOne = (EditText) findViewById(R.id.editText1);
EditText eTwo = (EditText) findViewById(R.id.editText2);
EditText eThree = (EditText) findViewById(R.id.editText3);
EditText eFour = (EditText) findViewById(R.id.editText4);
Button input = (Button) findViewById(R.id.input_button);
input.setOnClickListener(insert);
if (content)
{
one.setVisibility(View.GONE);
two.setVisibility(View.GONE);
three.setVisibility(View.GONE);
four.setVisibility(View.GONE);
eOne.setVisibility(View.GONE);
eTwo.setVisibility(View.GONE);
eThree.setVisibility(View.GONE);
eFour.setVisibility(View.GONE);
}
else
{
five.setVisibility(View.GONE);
FileInputStream fis;
fis = openFileInput("XML.txt");
eOne.setText(uno);
eTwo.setText(dos);
eThree.setText(tres);
eFour.setText(quatro, cinco);
}
}
private OnClickListener insert = new OnClickListener(){
public void onClick(View v) {
Intent trans = new Intent(v.getContext(), FillFile.class);
startActivityForResult(trans, 0);
}
};
}
當你開始爲變量名使用數字時,你應該考慮使用一個數組或'List'。這些工具以及適當的循環將使您的代碼更短,更容易閱讀。 –
由於您正在使用'FileOutputStream'來寫入文件,因此嘗試使用'FileInputStream'來讀取文件是有意義的。 –