/*
***(可选,综合)用两种方式保存对象。
有Worker 对象,部分代码如下:
class Worker{
private String name;
private int age
private double salary;
//构造方法
…
//get/set 方法
…
//toString 方法
…
}
1) 完善Worker 对象,并使其能使用对象序列化机制。
2) 利用ObjectOutputStream 存入两个Worker 对象, 然后使用
ObjectInputStream 读出这两个对象,并打印这两个对象的信息。
3) 写一个方法saveWorkerToFile(Worker w, File file),该方法完成下面的
功能:
假设有一个Worker 对象w1,File 对象f1 如下:
Worker w1 = new Worker(“Tom”, 30, 5000);
File f1 = new File(“test.txt”);
则调用saveWorkerToFile(w1, f1),会在test.txt 中增加一行:
Tom/30/5000
4) 写一个方法List<Worker> readWorkerFromFile(File file),该方法读某
个文件,从文件信息中创建一个Worker 类型的List。
例如,假设文件内容如下:
Tom/30/5000
Jim/25/3000
Terry/33/4500
则返回一个包含三个Worker 对象的List
*/
import java.io.*;
import java.util.*;
class Worker implements Serializable {
private static final long serialVersionUID = 1l;
private String name;
private int age;
private double salary;
Worker(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge() {
}
public int getAge() {
return age;
}
public void setSalary() {
}
public double getSalary() {
return salary;
}
// toString 方法
public String toString() {
return getName() + "/" + getAge() + "/" + getSalary();
}
// rewrite hashCode()
public int hashCode() {
return getName().hashCode() + getAge() + (int) getSalary();
}
// rewrite equals()
public boolean equals(Worker obj) {
return this.getName().equals(obj.getName())
&& this.getAge() == obj.getAge()
&& this.getSalary() == obj.getSalary();
}
}
public class D5 {
static List<Worker> readWorkerFromFile(File file) throws IOException {
// 要求4的功能 将文本中的内容封装成对象后放入List容器
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));
String str = null;
String strarr[];
List<Worker> list = new ArrayList<Worker>();
Worker temp;
while ((str = br.readLine()) != null) {
strarr = str.split("/", 3);
temp = new Worker(strarr[0], Integer.parseInt(strarr[1]), Double
.parseDouble(strarr[2]));
list.add(temp);
}
return list;
}
// 要求3的功能 对象信息写入文件
static void saveWorkerToFile(Worker w1, File f1) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(f1, true)));
bw.write(w1.toString());
bw.newLine();
bw.close();
}
public static void main(String args[]) throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
"test.dat", true));
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
"test.dat"));
Worker w1 = new Worker("Tom", 30, 5000);
Worker w2 = new Worker("Tim", 22, 2220);
Worker nw = null;
oos.writeObject(w1);
oos.writeObject(w2);
oos.flush();
Set<Worker> set = new HashSet<Worker>(); // 找到元素个数
set.add(w1);
set.add(w2);
for (int i = 0; i < set.size(); i++) {
nw = (Worker) ois.readObject();
System.out.println(nw.toString());
}
// ---------------------------
File f1 = new File("test.txt");
saveWorkerToFile(w1, f1);
// ---------------------------
for (Iterator it = readWorkerFromFile(f1).iterator(); it.hasNext();)// 需求(4)
{
System.out.println("*****" + it.next()); // 测试是否取到所有对象
}
}
}
***(可选,综合)用两种方式保存对象。
有Worker 对象,部分代码如下:
class Worker{
private String name;
private int age
private double salary;
//构造方法
…
//get/set 方法
…
//toString 方法
…
}
1) 完善Worker 对象,并使其能使用对象序列化机制。
2) 利用ObjectOutputStream 存入两个Worker 对象, 然后使用
ObjectInputStream 读出这两个对象,并打印这两个对象的信息。
3) 写一个方法saveWorkerToFile(Worker w, File file),该方法完成下面的
功能:
假设有一个Worker 对象w1,File 对象f1 如下:
Worker w1 = new Worker(“Tom”, 30, 5000);
File f1 = new File(“test.txt”);
则调用saveWorkerToFile(w1, f1),会在test.txt 中增加一行:
Tom/30/5000
4) 写一个方法List<Worker> readWorkerFromFile(File file),该方法读某
个文件,从文件信息中创建一个Worker 类型的List。
例如,假设文件内容如下:
Tom/30/5000
Jim/25/3000
Terry/33/4500
则返回一个包含三个Worker 对象的List
*/
import java.io.*;
import java.util.*;
class Worker implements Serializable {
private static final long serialVersionUID = 1l;
private String name;
private int age;
private double salary;
Worker(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge() {
}
public int getAge() {
return age;
}
public void setSalary() {
}
public double getSalary() {
return salary;
}
// toString 方法
public String toString() {
return getName() + "/" + getAge() + "/" + getSalary();
}
// rewrite hashCode()
public int hashCode() {
return getName().hashCode() + getAge() + (int) getSalary();
}
// rewrite equals()
public boolean equals(Worker obj) {
return this.getName().equals(obj.getName())
&& this.getAge() == obj.getAge()
&& this.getSalary() == obj.getSalary();
}
}
public class D5 {
static List<Worker> readWorkerFromFile(File file) throws IOException {
// 要求4的功能 将文本中的内容封装成对象后放入List容器
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));
String str = null;
String strarr[];
List<Worker> list = new ArrayList<Worker>();
Worker temp;
while ((str = br.readLine()) != null) {
strarr = str.split("/", 3);
temp = new Worker(strarr[0], Integer.parseInt(strarr[1]), Double
.parseDouble(strarr[2]));
list.add(temp);
}
return list;
}
// 要求3的功能 对象信息写入文件
static void saveWorkerToFile(Worker w1, File f1) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(f1, true)));
bw.write(w1.toString());
bw.newLine();
bw.close();
}
public static void main(String args[]) throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
"test.dat", true));
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
"test.dat"));
Worker w1 = new Worker("Tom", 30, 5000);
Worker w2 = new Worker("Tim", 22, 2220);
Worker nw = null;
oos.writeObject(w1);
oos.writeObject(w2);
oos.flush();
Set<Worker> set = new HashSet<Worker>(); // 找到元素个数
set.add(w1);
set.add(w2);
for (int i = 0; i < set.size(); i++) {
nw = (Worker) ois.readObject();
System.out.println(nw.toString());
}
// ---------------------------
File f1 = new File("test.txt");
saveWorkerToFile(w1, f1);
// ---------------------------
for (Iterator it = readWorkerFromFile(f1).iterator(); it.hasNext();)// 需求(4)
{
System.out.println("*****" + it.next()); // 测试是否取到所有对象
}
}
}