我需要使用文件输出流从编辑文本保存到文件或列表,但是每次我使用编辑文本中写的名称保存新文件时,如何只创建一个文件,每次我在编辑文本中输入数据,仅更新同一文件。
另外,如何将计数器添加到文件
Button save;
EditText Username, ID;
String FILENAME, JOUR;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.internal);
save = (Button) findViewById(R.id.button1);
save.setOnClickListener(this);
Username = (EditText) findViewById(R.id.editText1);
ID = (EditText) findViewById(R.id.editText2);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
FILENAME = Username.getText().toString();
if (FILENAME.contentEquals("")){
finish();
}
JOUR = ID.getText().toString();
Username.setText("");
ID.setText("");
try {
FileOutputStream fos = openFileOutput(FILENAME , Context.MODE_PRIVATE);
fos.write(JOUR.getBytes());
// fos.write(FILENAME.getBytes());
fos.close();
Toast.makeText(
InternalStore.this,
FILENAME + " saved",
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
试试这个:
BufferedWriter out=new BufferedWriter(new FileWriter(getFilesDir()+File.separator+fileName.getText().toString()+".txt"));
out.write(text.getText().toString());
out.close();