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 org.junit.Testimport java.io.*
import java.util.ArrayList
import java.util.List
public class TestIo {
//路径
private static final String PATH = "D:\\demo1.txt"
@Test
public void testIo() throws IOException {
List<String> list = new ArrayList<>()
list.add("1")
list.add("2")
list.add("3")
list.add("4")
list.add("5")
File file = new File(PATH)
//如果没有文件就创建
if (!file.isFile()) {
file.createNewFile()
}
//
BufferedWriter writer = new BufferedWriter(new FileWriter(PATH))
for (String l:list){
writer.write(l)
}
writer.close()
System.out.println("完成")
}
}
结果
希望对你有帮助
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)