2个回答
展开全部
用对象流读取,大多数向量、集合都实现了序列化,所以实现起来很简单
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.TreeSet;
import java.util.Vector;
public class outStream {
static TreeSet<String> v = new TreeSet<腊漏String>();
// static Vector<String> v = new Vector<String>();
static File fdata = new File("hello/data");
public static void main(String[] args) {
boolean flag = false; //读、存标志
if(flag){
for(int i = 9; i > 0; i--){
v.add("name" + i);
}
saveVector(); // 存储数据
System.out.println("已经存储完毕,修改flag值可读取数据");
}
if (!flag){
readVector(); //读亮局陪取操作
}
}
private static void saveVector() {
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fdata));
oos.writeObject(v);
oos.close();
} catch (Exception e) 敬蠢{
e.printStackTrace();
}
}
private static void readVector() {
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fdata));
// v = (Vector<String>) ois.readObject();
v = (TreeSet<String>) ois.readObject();
ois.close();
for (String e : v) {
System.out.println(e);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
引用hitzsf的回答:
用对象流读取,大多数向量、集合都实现了序列化,所以实现起来很简单import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.util.TreeSet;import java.util.Vector;public class outStream { static TreeSet<String> v = new TreeSet<String>();// static Vector<String> v = new Vector<String>(); static File fdata = new File("hello/data"); public static void main(String[] args) { boolean flag = false; //读、存标志 if(flag){ for(int i = 9; i > 0; i--){ v.add("name" + i); } saveVector(); // 存储数据 System.out.println("已经存储完毕,修改flag值可读取数据"); } if (!flag){ readVector(); //读取操作 } } private static void saveVector() { try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fdata)); oos.writeObject(v); oos.close(); } catch (Exception e) { e.printStackTrace(); } } private static void readVector() { try { ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fdata));// v = (Vector<String>) ois.readObject(); v = (TreeSet<String>) ois.readObject(); ois.close(); for (String e : v) { System.out.println(e); } } catch (Exception e) { e.printStackTrace(); } }}
用对象流读取,大多数向量、集合都实现了序列化,所以实现起来很简单import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.util.TreeSet;import java.util.Vector;public class outStream { static TreeSet<String> v = new TreeSet<String>();// static Vector<String> v = new Vector<String>(); static File fdata = new File("hello/data"); public static void main(String[] args) { boolean flag = false; //读、存标志 if(flag){ for(int i = 9; i > 0; i--){ v.add("name" + i); } saveVector(); // 存储数据 System.out.println("已经存储完毕,修改flag值可读取数据"); } if (!flag){ readVector(); //读取操作 } } private static void saveVector() { try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fdata)); oos.writeObject(v); oos.close(); } catch (Exception e) { e.printStackTrace(); } } private static void readVector() { try { ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fdata));// v = (Vector<String>) ois.readObject(); v = (TreeSet<String>) ois.readObject(); ois.close(); for (String e : v) { System.out.println(e); } } catch (Exception e) { e.printStackTrace(); } }}
展开全部
你刚开始的flag应该是true吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询