package com.example.tianjia;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class TianjiaActivity extends Activity implements OnClickListener {
private EditText name,phone;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tianjia);
name=(EditText)findViewById(R.id.name);
phone=(EditText)findViewById(R.id.one);
button=(Button)findViewById(R.id.bt1);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt1:
Intent intent=new Intent();
Bundle bundle=new Bundle();
String name1=name.getText().toString();
String phone1=phone.getText().toString();
FileInputStream in;
BufferedReader read = null;
String s1="",s2="";
try {
in = openFileInput("date5");
read = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = read.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line, ";"); //设置数据之间 的分隔符为;
s1 = st.nextToken();//读取的时候是 String 类
s2 = st.nextToken();
if (s1.equals(name1) || s2.equals(phone1) ) {
Toast.makeText(TianjiaActivity.this, "该快捷拨号已存在", Toast.LENGTH_SHORT).show();
//AlertDialog alertDialog=new AlertDialog();
break;
}
}
}catch (IOException e){
e.printStackTrace();
}finally {
try {
if (read != null) {
read.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
if(!s1.equals(name1)&&!s2.equals(phone1)&&!TextUtils.isEmpty(name1)&&!TextUtils.isEmpty(phone1))
{
Toast.makeText(TianjiaActivity.this, "关掉apk", Toast.LENGTH_LONG).show();
bundle.putString("name1",name1);
bundle.putString("phone1",phone1);
intent.putExtras(bundle);
setResult(RESULT_OK,intent);
finish();
}
}
}
@Override
protected void onDestroy() {
Toast.makeText(TianjiaActivity.this, "正在保存数据", Toast.LENGTH_LONG).show();
if(name.equals("")) {
String Input1 = name.getText().toString();
String Input2 = phone.getText().toString();
if(!TextUtils.isEmpty(Input1)&&!TextUtils.isEmpty(Input2))
save(Input1, Input2);
}
super.onDestroy();
}
public void save(String i1,String i2){
FileOutputStream out;
BufferedWriter writer=null;
FileInputStream in;
BufferedReader read=null;
String s1=null,s2=null;
try{
out=openFileOutput("date5", Context.MODE_APPEND);
in=openFileInput("date5");
writer=new BufferedWriter(new OutputStreamWriter(out));
read = new BufferedReader(new InputStreamReader(in));
String line="";
while((line=read.readLine())!=null){
StringTokenizer st = new StringTokenizer(line, ";"); //设置数据之间 的分隔符为;
s1 = st.nextToken();//读取的时候是 String 类
s2 = st.nextToken();
if(s1.equals(i1)||s2.equals(i2)) {
Toast.makeText(TianjiaActivity.this, "该快捷拨号已存在", Toast.LENGTH_SHORT).show();
return;
}
}
if(!s1.equals(i1)&&!s2.equals(i2)&&!TextUtils.isEmpty(i1)&&!TextUtils.isEmpty(i2)) {
writer.write(i1);
writer.write(";");
writer.write(i2);
writer.write(";");
writer.newLine();
}
}catch(IOException e){
e.printStackTrace();
}finally {
try {
if (writer != null) {
writer.close();
}
}catch(IOException 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.tianjia, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这程序怎么无法保存数据
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
6条回答 默认 最新
- 左大星 2017-04-28 13:06关注
当你开了文件流写入数据后,在DDMS里看下相应目录文件夹下有没有写入成功,不过不推荐用XML文件保存,SharePerfrence这么好的持久化API你为啥不用呢...
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
- ¥15 lammps拉伸应力应变曲线分析
- ¥15 C++ 头文件/宏冲突问题解决
- ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
- ¥50 安卓adb backup备份子用户应用数据失败
- ¥20 有人能用聚类分析帮我分析一下文本内容嘛
- ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
- ¥30 python代码,帮调试,帮帮忙吧
- ¥15 #MATLAB仿真#车辆换道路径规划
- ¥15 java 操作 elasticsearch 8.1 实现 索引的重建