java把对象数组存入文件中再还原来

定义了一个飞机对象Plane,创建了对象数组Fly,将这个数组存入文件中,再把这个数组从文件中还原出来,求大神解救
能不能遍个简单代码啊提示下啊亲,那些文件流还没学到

你这个只要将数组保存,因此我就没有用序列化的方式直接存储对象,,只要将数组信息保存就可以了,,所以简单写了个例子。。

/**Fly.java*/
public class Fly  {
private String depart;
private String dest;

public Fly() {

}

public Fly(String depart, String dest) {
this();
this.depart = depart;
this.dest = dest;
}

public String getDepart() {
return depart;
}

public String getDest() {
return dest;
}
}

/**Plane.java*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class Plane {
Fly[] flies;

public void init() {
flies = new Fly[3];
flies[0] = new Fly("上海", "香港");
flies[1] = new Fly("上海", "纽约");
flies[2] = new Fly("上海", "北京");
}

public void writeFly(Fly[] flies, File output) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(output));
for (int i = 0; i < flies.length; i++) {
bw.write(flies[i].getDepart() + " " + flies[i].getDest() + "\n");
}
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public Fly[] readFly(File file) {
ArrayList<Fly> f = new ArrayList<Fly>();
try {
@SuppressWarnings("resource")
BufferedReader br = new BufferedReader(new FileReader(file));
String temp = "";
while ((temp = br.readLine()) != null) {
String[] fac = temp.trim().split(" ");
String dep = fac[0];
String des = fac[1];
Fly newF = new Fly(dep, des);
f.add(newF);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Fly[] flyarray = new Fly[f.size()];
for (int i = 0; i < f.size(); i++) {
flyarray[i] = f.get(i);
}
return flyarray;
}

public static void main(String[] args) {
Plane p = new Plane();
p.init();
File file = new File("output.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
p.writeFly(p.flies, file);
Fly[] array = p.readFly(file);
for (int i = 0; i < array.length; i++) {
System.out.println("出发地:" + array[i].getDepart() + " , 目的地:"
+ array[i].getDest());
}
}
}

运行结果:

控制台输出信息:

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-05-18

你这种情况必须用到对象的序列化。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

/**
 * 你这种情况必须用到对象的序列化
 * @author QUAN
 *
 */
public class Test
{
/**
 * 保存对象
 * @param obj
 * @throws IOException
 */
public static void saveObject(Object obj) throws IOException
{
File file = new File("D:/test/file.ser");
if (!file.exists())
{
file.getParentFile().mkdirs();
file.createNewFile();
}
OutputStream os = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(obj);
oos.close();
}

/**
 * 读取对象
 * @return
 * @throws IOException
 * @throws ClassNotFoundException
 */
public static Object readObject() throws IOException,
ClassNotFoundException
{
InputStream is = new FileInputStream("D:/test/file.ser");
ObjectInputStream ois = new ObjectInputStream(is);
Object obj = ois.readObject();
ois.close();
return obj;
}

public static void main(String[] args)
{
Plane p = new Plane();
p.setId(1);
p.setName("MH370");

Plane p1 = new Plane();
p1.setId(2);
p1.setName("Air Plane One");

Plane[] fly = { p, p1 };

try
{
saveObject(fly);

Plane[] result = (Plane[])readObject();

for(Plane e : result)
{
System.out.println("id:" + e.getId() + "\t" + "name:" + e.getName());
}

}
catch (IOException e)
{
e.printStackTrace();

catch (ClassNotFoundException e)
{
e.printStackTrace();
}

}
}

class Plane implements Serializable
{
/**
 * 
 */
private static final long serialVersionUID = 6865564924659901756L;

private int id;

private String name;

public int getId()
{
return id;
}

public void setId(int id)
{
this.id = id;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

}

本回答被网友采纳
第2个回答  2014-06-08
package com.test;

import java.awt.Color;
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;

public class Plane implements Serializable{

String name;
Color color;

public Plane(String name,Color color)
{
this.name=name;
this.color=color;
}

public String toString()
{
return this.name+this.color.toString();
}

public static void main(String[] args) {

File file=new File("d:/example.dat");
try {
//写入飞机
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(file));
Plane plane1=new Plane("plane1",Color.red);
Plane plane2=new Plane("plane2",Color.white);
Plane plane3=new Plane("plane3",Color.YELLOW);
out.writeObject(plane1);
out.writeObject(plane2);
out.writeObject(plane3);
//读取飞机
ObjectInputStream in=new ObjectInputStream(new FileInputStream(file));
Plane in1=(Plane) in.readObject();
Plane in2=(Plane) in.readObject();
Plane in3=(Plane) in.readObject();
System.out.println(in1);
System.out.println(in2);
System.out.println(in3);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}追问

我想把文件中所有对象读取出来并生成一个对象数组,可以给个代码吗?

第3个回答  2014-06-08
ObjectInputStream 只有对象流能做到
第4个回答  2014-06-08
FileInputStream和FileOutputStream可以做到。
相似回答