package byteStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class IO实验 {
public static void main(String[] args) throws IOException {
BufferedReader bos=null;
try {
bos=new BufferedReader(new FileReader("C:\\Users\\12345678\\Desktop\\read.txt"));
// 速度为1kb
byte [] b=new byte[100000];
int len=0;
// read不能你等与-1
while ((len=bos.read())!=-1){
System.out.println(new String(b,0,len));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(bos!=null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Collection c = new ArrayList();
//创建学生对象
Student st1 = new Student("张三", 30,2019,"计算机原理");
Student st2 = new Student("张三", 30,2019,"计算机原理");
Student st3 = new Student("麻子", 30,2019,"计算机原理");
Student st4 = new Student("王二", 30,2019,"计算机原理");
将学生对象,添加到集合里
c.add(st1);
c.add(st2); //向上转型
c.add(st3);
c.add(st4);
System.out.println(c);
for (Iterator iterator = c.iterator(); iterator.hasNext();) {
Object object = (Object) iterator.next();
System.out.println(object);
}
把集合转换成数组
// Object[] obj = c.toArray();
开始遍历
// for(int i=0;i<obj.length;i++){
// Student s = (Student)obj[i]; //向下转型
//
// System.out.println(s.getName()+"--->"+s.getAge());
//}
//System.out.println("===============================");
// for(int i=0;i<obj.length;i++){
// Student s = (Student)obj[i]; //转型时,要注意,
//
//System.out.println(s);
//System.out.println(st2);
//序列化一个对象
ObjectOutputStream objOutputStream = new ObjectOutputStream(new FileOutputStream("C:\\Users\\12345678\\Desktop\\IO\\src\\byteStream\\Student.java"));
//写入字面值常量
objOutputStream.writeObject("将对象序列化");
//写入匿名对象
// objOutputStream.writeObject(new Student());
//写入对象
objOutputStream.writeObject(c);
}
}
}
IO流实验
最新推荐文章于 2023-03-06 13:56:35 发布