本文共 3836 字,大约阅读时间需要 12 分钟。
抽象父类:
1. InputStream
2. OutputStream
文件流:
1. FileInputStream
2. FileOutputStream
字节缓冲流:
1. BufferedInputStream
2. BufferedOutputStream
ps:字节流可以操作任意类型的数据
字符流:
抽象基类:
1. Reader
2. Writer
转换流:
1. InputStreamReader
2. OutputStreamWriter
flush()
字符缓冲流:特点:可以按行读写数据
1. BufferedReader
readLine() - String
2. BufferedWriter
PrintWriter
i. 特殊的功能:自动行刷新
ii. print println
ps:字符流只能操作文本类型的数据
对象流:
ObjectInputStream
1. readObject() -Object
ObjectOutputStream
1. writeObject()
序列化:
1. 对象数据à字节数据 序列化
实现:
让对象所属的类型实现Serializable接口
2. 字节数据à对象 反序列化
注意的问题:
1. 反序列化时造成版本不兼容问题的原因以及解决办法:在对象所属的类中生成序列版本号
2. 如果一个对象是可序列化的,这个对象所属的类内部有其他的对象作为它的成员,那么作为成员的这个对象也一定是可序列化的
3. 如果一个对象内部的数据并非全部要持久化,此时可以用transient来修饰不持久化的数据
Java提供了一个可以对文件随机访问的操作,访问包括读和写操作。该类名为RandomAccessFile。该类的读写是基于指针的操作。
RandomAccessFile也叫做随机访问流
RandomAccessFile在对文件进行随机访问操作时有两个模式,分别为只读模式(只读取文件数据),和读写模式(对文件数据读写)。
1.字节流:
//创建输入流/输出流对象FileInputStream fis = new FileInputStream("fis.java");FileOutputStream fos = new FileOutputStream("copy1.txt");//按照单个字节复制int by = -1;while((by=fis.read())!=-1){ //将by写入目标文件中 fos.write(by);}//按照字节数组复制文件byte[] bys = new byte[1024];int len = 0;while((len=fis.read(bys))>0){ //将读取到的字节数组写入目标文件中 fos.write(bys, 0, len);}//关闭流对象fis.close();fos.close();
2.字节缓冲流
//创建流对象BufferedInputStream bis = new BufferedInputStream(new FileInputStream("1.jpg"));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("2.jpg"));//完成复制byte[] bys = new byte[1024];int len = -1;while((len=bis.read(bys))!=-1){ bos.write(bys, 0, len); bos.flush();} bis.close(); bos.close();
3.字符流
//创建输入流对象InputStreamReader isr = new InputStreamReader(new FileInputStream("love.txt"),"UTF-8");//创建输出流对象OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("osw.txt"),"UTF-8");//读取//按照单个字符读取// int ch = -1;// while((ch=isr.read())!=-1){// osw.write(chs, 0, len);// }//按照字符数组读取char[] chs = new char[1024];int len = -1;while((len=isr.read(chs))!=-1){ //char[]-->String osw.write(chs, 0, len);}//释放资源isr.close();osw.close();
4.字符缓冲流
//创建输入,输出流对象BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("fis.java")));PrintWriter pw = new PrintWriter(new FileOutputStream("buffer.txt"),true);//复制String str = null;while((str=br.readLine())!=null){ pw.println(str);}//流关闭br.close();pw.close();
5.对象流
/ //User类成员变量private String name;private String pass;//transient关键字://当一个属性被transient修饰后,该属性在进行对象序列化时其值会被忽略.//忽略不必要的属性值可以达到对象序列化"瘦身"的效果.private transient String desc;
p
* 将User对象序列化到文件中 * 要实现将User对象中的desc数据不写入文件 */User user = new User("starry","123","描述");ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("transient.txt"));oos.writeObject(user);oos.close();//反序列化操作ObjectInputStream ois = new ObjectInputStream(new FileInputStream("transient.txt"));User per = (User)ois.readObject();System.out.println(per.getName()+per.getPass()+per.getDesc());ois.close();
6.RandomAccessFile随机访问流
RandomAccessFile ra = new RandomAccessFile("web/love.txt","rw"); RandomAccessFile ra1 = new RandomAccessFile("web/love-copy.txt","rw"); byte[] by = new byte[1024]; int i= -1; while((i = ra.read(by)) != -1 ){ ra1.write(by,0,i); } ra.close(); ra1.close();
//创建RandomAccessFile对象RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");//移动指针偏移量并读取偏移量之后的内容到临时文件中raf.seek(3);//创建临时文件File tmp = File.createTempFile("copy", ".tmp");//复制RandomAccessFile raftmp = new RandomAccessFile(tmp, "rw");byte[] bys = new byte[1024];int len = -1;while((len=raf.read(bys))!=-1){ raftmp.write(bys, 0, len);}raf.seek(3);//向raf.txt文件中插入数据raf.write("hello".getBytes());//将临时文件中的内容复制到本文件的最后raftmp.seek(0);len = -1;while((len=raftmp.read(bys))!=-1){ raf.write(bys, 0, len);}
//删除临时文件tmp.deleteOnExit();raf.close();raftmp.close();
转载地址:http://mvxvi.baihongyu.com/