安卓中如何将list中的数据写入文件里

凡心-不葬 2016-02-17 07:23:40
package com.example.edittext.readandwrite;
public class Student {
private String name;
private String age;
public Student(String name, String age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}

public class MainActivity11 extends Activity {
private EditText ed1;
private EditText ed11;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main11);

ed11 = (EditText) findViewById(R.id.ed11);
ed1 = (EditText) findViewById(R.id.ed1);

write.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

String str1 = ed1.getText().toString();
String str2 = ed11.getText().toString();

List<Student> list = new ArrayList<Student>();
list.add(new Student(str1,str2));

???????


if (!str1.equals("")) {
write(??????);
Toast.makeText(getApplicationContext(), "+", Toast.LENGTH_SHORT).show();
}
}
});



}
}

...全文
652 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_27195835 2017-06-05
  • 打赏
  • 举报
回复
207255537@qq.com,能给小弟一份吗,感激不尽啊
凡心-不葬 2016-02-18
  • 打赏
  • 举报
回复
引用 8 楼 liyuanjinglyj 的回复:
项目已经发到你的邮箱
好的,已经收到,十分感谢你的帮助
李元静 2016-02-18
  • 打赏
  • 举报
回复
项目已经发到你的邮箱
李元静 2016-02-18
  • 打赏
  • 举报
回复
/*** * 读取存储的数据 */ public void readData(){ FileInputStream fis=null; try { fis=openFileInput("student.txt"); byte[] outByte=new byte[fis.available()]; fis.read(outByte); contentTxt.setText(new String(outByte)); } catch (Exception e) { Log.i(this.getClass().getName(),"读取失败"); e.printStackTrace(); }finally { try { Log.i(this.getClass().getName(),"读取成功"); fis.close(); } catch (IOException e) { e.printStackTrace(); } } } /*** * 写入要存储的数据到/data/data/com.example.liyuanjing.myapplication/files目录下student.txt文件 */ public void storageData(){ FileOutputStream fos = null; try { fos=openFileOutput("student.txt", Context.MODE_PRIVATE); fos.write(jsonStr.getBytes()); } catch (Exception e) { Log.i(this.getClass().getName(),"存储失败"); e.printStackTrace(); }finally { try { fos.close(); Log.i(this.getClass().getName(), "存储成功"); } catch (IOException e) { e.printStackTrace(); } } } /*** * 模拟存储的数据 */ public void initData(){ Student xiaoMing=new Student("xiaoMing",15,"boy"); Student xiaoHone=new Student("xiaoHone",21,"girl"); studentList.add(xiaoMing); studentList.add(xiaoHone); Gson gson=new Gson(); jsonStr=gson.toJson(studentList);//gson Log.i(this.getClass().getName(),jsonStr); }
凡心-不葬 2016-02-17
  • 打赏
  • 举报
回复
引用 4 楼 liyuanjinglyj 的回复:
刚看到你说非sd卡中,那就应用程序files目录了。这个我是手机,明天白天上电脑将代码发给你。
保存在默认新建的一个文本中,数组吧,write(?) ?=list转成String类型保存 好呀,等你方便的时候传给我,谢谢
凡心-不葬 2016-02-17
  • 打赏
  • 举报
回复
安卓项目中默认建立一个文本
引用
zrf1335348191
李元静 2016-02-17
  • 打赏
  • 举报
回复
首先你问的问题有两个地方没有申明,1.你保存数据在哪里?2.你保存数据为何格式。对于你存储的数据,是用类的形式展示的,所以如果存储为文本格式,那么就要将数据转换成json格式,那么存储与取出就非常方便了。
李元静 2016-02-17
  • 打赏
  • 举报
回复
刚看到你说非sd卡中,那就应用程序files目录了。这个我是手机,明天白天上电脑将代码发给你。
zrf1335348191 2016-02-17
  • 打赏
  • 举报
回复
非sd卡,你要写在哪里??网络存储?数据库?SP?
凡心-不葬 2016-02-17
  • 打赏
  • 举报
回复
非sd卡中。。。。。。。。。

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧