java 我想将一个装了对象的arraylist 写入文件,读出文件,请问怎么写代码,改对象的类定义如下
publicclassUser{privateStringname;privateStringcode;publicStringgetName(){returnname;...
public class User {
private String name;
private String code;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public User(String name, String code) {
super();
this.name = name;
this.code = code;
}
} 展开
private String name;
private String code;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public User(String name, String code) {
super();
this.name = name;
this.code = code;
}
} 展开
展开全部
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
public class User implements Serializable{
private String name;
private String code;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public User(String name, String code) {
super();
this.name = name;
this.code = code;
}
public static void writeUser(ArrayList<User> userList,File file){//file 为写入的文件
ObjectOutputStream oos;
try {
if(!file.exists())
file.createNewFile();
oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(userList);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
有疑问可追问或者hi
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
public class User implements Serializable{
private String name;
private String code;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public User(String name, String code) {
super();
this.name = name;
this.code = code;
}
public static void writeUser(ArrayList<User> userList,File file){//file 为写入的文件
ObjectOutputStream oos;
try {
if(!file.exists())
file.createNewFile();
oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(userList);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
有疑问可追问或者hi
TableDI
2024-07-18 广告
2024-07-18 广告
Excel一键自动匹配,在线免费vlookup工具,3步完成!Excel在线免费vlookup工具,点击93步自动完成vlookup匹配,无需手写公式,免费使用!...
点击进入详情页
本回答由TableDI提供
2012-05-02
展开全部
序列化
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询