Androidファイルにいくつかの文字列を読み書きしたい
だから私はツリーEditTextsから情報を書きます
public void save(Person p) {
try {
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(p.getName().getBytes());
fos.write(p.getSurname().getBytes());
fos.write(p.getAge().getBytes());
fos.close();
Toast toast = Toast.makeText(getApplicationContext(),
"Info was saved!", Toast.LENGTH_SHORT);
toast.show();
} catch (FileNotFoundException e){
e.printStackTrace();
Toast toast = Toast.makeText(getApplicationContext(),
"File not found exeption!", Toast.LENGTH_SHORT);
toast.show();
} catch (IOException e) {
e.printStackTrace();
Toast toast = Toast.makeText(getApplicationContext(),
"Input/Output Exeption!", Toast.LENGTH_SHORT);
toast.show();
}
}
メソッドで情報を読み戻そうとします
private void load(){
String value = "";
FileInputStream fis;
try {
fis = openFileInput(FILENAME);
byte[] input = new byte[fis.available()];
while(fis.read(input) != -1 ) {
value += new String(input);
}
edName.setText(value);
while(fis.read(input) != -1 ) {
value += new String(input);
}
edSurname.setText(value);
while(fis.read(input) != -1 ) {
value += new String(input);
}
edAge.setText(value);
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
Toast toast = Toast.makeText(getApplicationContext(),
"Input/Output Exeption!", Toast.LENGTH_SHORT);
toast.show();
}
}
情報をロードしますが、すべてのデータが 1 つの行に結合されます。行ごとにデータを読み取るにはどうすればよいですか?
または、フィールドを含むクラスを読み書きできますか? (私の例では、名前、姓、年齢、男性のフィールドを持つクラス Person があります)