六、字符输出流(Writer)
public abstract class Writer extends Object implements Appendable, Closeable, Flushable表示字符输出流的所有类的超类。
6.1、常用方法
常用方法:
public abstract void flush() throws IOException:刷新此输出流并强制写出所有缓冲的输出字符。
public abstract void close() throws IOException:关闭此输入流并释放与该流关联的所有系统资源。
public void write(int c) throws IOException:将指定的一个字符数据 c 写入到输出流中。
public void write(char[] cbuf) throws IOException:把数组 cbuf 中 cbuf.length 个字符数据写入到输出流中。
public abstract void write(char[] cbuf, int off,int len) throws IOException:把数组 cbuf 中从索引 off 开始的 len 个字符写入此输出流中。
public void write(String str) throws IOException:将 str 字符串数据写入到输出流中。
package day17_IO.classing;
import java.io.File;import java.io.FileWriter;import java.io.IOException;
/** * @author Xiao_Lin * @date 2020/12/22 20:44 */public class WriterDemo {
public static void main(String[] args) throws IOException { // 准备File对象 找绝对路径 File file = new File("123.txt"); // 准备字符输出流 FileWriter writer = new FileWriter(file); // 调用API 写数据 writer.write(49); writer.write("丹丹"); writer.write(new char[]{'B','C'}); // 刷新 writer.flush(); // 关闭资源 writer.close(); }}
复制代码
七、综合练习
7.1、文件拷贝操作
private static void copy() throws Exception { //1):创建源或者目标对象 File src = new File("file/copy_before.txt"); File dest = new File("file/copy_after.txt"); //2):创建IO流对象 FileReader in = new FileReader(src); FileWriter out = new FileWriter(dest); //3):具体的IO操作 int len = -1;//记录以及读取了多个字符 char[] buff = new char[1024];//每次可以读取1024个字符 len = in.read(buff);//先读取一次 while(len > 0) { //边读边写 out.write(buff, 0, len); len = in.read(buff);//再继续读取 } //4):关闭资源(勿忘) out.close(); in.close();}
复制代码
7.2、处理异常
private static void copy2() { //1):创建源或者目标对象 File src = new File("file/copy_before.txt"); File dest = new File("file/copy_after.txt");
//把需要关闭的资源,声明在try之外 FileReader in = null; FileWriter out = null; try { //可能出现异常的代码 //2):创建IO流对象 in = new FileReader(src); out = new FileWriter(dest); //3):具体的IO操作 int len = -1;//记录以及读取了多个字符 char[] buff = new char[1024];//每次可以读取1024个字符 len = in.read(buff);//先读取一次 while (len > 0) { out.write(buff, 0, len); len = in.read(buff);//再继续读取 } } catch (Exception e) { e.printStackTrace(); } finally { //4):关闭资源(勿忘) try { if (out != null) { out.close(); } } catch (Exception e) { e.printStackTrace(); } try { if (in != null) { in.close(); } } catch (Exception e) { e.printStackTrace(); } } //-------------------------------------------- //ctrl+alt+t //Java7的新特性 //如果资源对象实现了Closeable接口,那么可以不用手动关闭资源了 //开发者只需要指定需要关闭的资源对象即可 try ( //定义需要关闭的资源对象 FileReader reader = new FileReader("files/before.txt"); FileWriter writer = new FileWriter("files/after.txt"); ){ //从指定文件中读取数据,立即将读到的数据写到指定的文件中 char[] buffer = new char[1024]; int len = 0; while((len = reader.read(buffer)) != -1){ //将读到的数据(buffer)写到指定的文件中 writer.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); }}
复制代码
八、文件流
当程序需要读取文件中的数据或者把数据保存到文件中去,此时就得使用文件流,但是注意只能操作纯文本文件(txt 格式),不要使用 Word、Excel。文件流比较常用。
8.1、文件字节输出流(FileOutputStream)
private static void test1() throws Exception { //1):创建源或者目标对象 File dest = new File("file/result1.txt"); //2):创建IO流对象 FileOutputStream out = new FileOutputStream(dest); //3):具体的IO操作 out.write(65);//输出A out.write(66);//输出B out.write(67);//输出C String str = "to be or not to be"; out.write(str.getBytes());//输出str字符串中所有内容 //4):关闭资源(勿忘) out.close();}
复制代码
8.2、文件字节输入流(FileInputStream)
private static void test2() throws Exception { //1):创建源或者目标对象 File src = new File("file/result1.txt"); //2):创建IO流对象 FileInputStream in = new FileInputStream(src); //3):具体的IO操作 System.out.println((char)in.read());//读取A字节 System.out.println((char)in.read());//读取B字节 System.out.println((char)in.read());//读取C字节 byte[] buff = new byte[5];//准备容量为5的缓冲区 int len = in.read(buff);//读取5个字节数据,并存储到buff数组中 System.out.println(Arrays.toString(buff));//[116, 111, 32, 98, 101] System.out.println(len);//返回读取了几个字节 //4):关闭资源(勿忘) in.close();}
复制代码
8.3、文件字符输出流(FileWriter)
private static void test3() throws Exception { //1):创建源或者目标对象 File dest = new File("file/result2.txt"); //2):创建IO流对象 FileWriter out = new FileWriter(dest); //3):具体的IO操作 out.write('辛');//输出A out.write('弃');//输出B out.write('疾');//输出C String str = "众里寻他千百度,蓦然回首,那人却在,灯火阑珊处。"; out.write(str.toCharArray()); out.write(str);//String的本质就是char[] //4):关闭资源(勿忘) out.close();}
复制代码
8.4、文件字符输入流(FileReader)
private static void test4() throws Exception { //1):创建源或者目标对象 File src = new File("file/result2.txt"); //2):创建IO流对象 FileReader in = new FileReader(src); //3):具体的IO操作 System.out.println(in.read());//读取辛字符 System.out.println(in.read());//读取弃字符 System.out.println(in.read());//读取疾字符 char[] buff = new char[5];//准备容量为5的缓冲区 int len = in.read(buff);//读取5个字符数据,并存储到buff数组中 System.out.println(Arrays.toString(buff));//[众, 里, 寻, 他, 千] System.out.println(len);//返回读取了几个字节 //4):关闭资源(勿忘) in.close();}
复制代码
九、缓冲流
节点流的功能都比较单一,性能较低。处理流,也称之为包装流,相对于节点流更高级,这里存在一个设计模式——装饰设计模式。有了包装流之后,我们只关心包装流的操作即可,比如只需要关闭包装流即可,无需在关闭节点流。
四大基流都有自己的包装流
BufferedInputStream / BufferedOutputStream / BufferedReader / BufferedWriter
复制代码
缓冲流内置了一个默认大小为 8192 个字节或字符的缓存区,缓冲区的作用用来减少磁盘的 IO 操作,拿字节缓冲流举例,比如一次性读取 8192 个字节到内存中,或者存满 8192 个字节再输出到磁盘中,操作数据量比较大的流,都建议使用上对应的缓存流。
private static void copy3() throws Exception { //1):创建源或者目标对象 File src = new File("file/郭德纲-报菜名.mp3"); File dest = new File("file/郭德纲-报菜名2.mp3"); //2):创建IO流对象 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src), 8192); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest), 8192); //3):具体的IO操作 int len = -1;//记录以及读取了多个字符 byte[] buff = new byte[1024];//每次可以读取1024个字符 len = bis.read(buff);//先读取一次 while (len > 0) { //边读边写 bos.write(buff, 0, len); len = bis.read(buff);//再继续读取 } //4):关闭资源(勿忘) bos.close(); bis.close();}
复制代码
十、字节流和字符流如何选用
使用记事本打开某个文件,如果可以看到内容的就是文本文件,否则可以暂时认为是二进制格式的。
一般的,操作二进制文件(图片、音频、视频等)必须使用字节流。操作文本文件使用字符流,尤其是操作带有中文的文件,使用字符流不容易导致乱码,因为使用字节流可能出现读取半个汉字的尴尬(汉字由两个或三个字节组成)。当然,如果不清楚属于哪一类型文件,都可以使用字节流。
十一、对象序列化
序列化是指把 Java 堆内存中的对象数据,通过某种方式把对象数据存储到磁盘文件中或者传递给给网络上传输。序列化在分布式系统在应用非常广泛。
相反的也有反序列化:把磁盘文件中的对象的数据或者把网络节点上的对象数据恢复成 Java 对象的过程。
如果想做序列化的类必须实现序列化接口java.io.Serializable(这是标志接口,里面并没有抽象方法),如果字段使用 transient 修饰则不会被序列化。
评论