我在xml 中定义多个edittext,现在把所有输入数据保存在一个文件中。源代码如下,没有报错,也不能保存成功。求问,问题在哪?
public class MainActivity extends Activity {
Button edit ; //xml中定义的Button按钮,“保存”
EditText title ; //xml中第一个edittext内容,其余的省略了
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (Button)findViewById(R.id.edit_id);
title=(EditText)findViewById(R.id.biaoti);
edit.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File file_name = null;
try {
file_name = new File("/sdcard/" + "test.txt");
if (!file_name.exists()) {
file_name.createNewFile();
}
System.out.println(file_name);
} catch (final IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
final OutputStream os = new FileOutputStream(file_name);
os.write(edit.getText().toString().getBytes()); //得到Button的数据信息
os.write(title.getText().toString().getBytes()); //得到第一个TextEdit的数据信息
os.close();
} catch (final Exception e) {
e.printStackTrace();
}
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
------解决思路----------------------
请问你的这个问题解决了吗?
------解决思路----------------------
请问你的这个问题解决了吗?
------解决思路----------------------
先 跟一下 看看 从 Editext上有没有获取到, 如果能获取到 那可能就是输出流有点问题;
看看输出流有没有建立成功, 随便往输出流中写点东西, 看看有文件生成没
建议 : os.flush() 一下;