当前位置: 首页 > 科技观察

java中缓冲流、转换流、序列化流详解

时间:2023-03-12 10:06:18 科技观察

1.缓冲流bufferstream的基本原理是在创建流对象时,会创建一个默认大小的内置缓冲数组,并进行读取和读取通过缓冲区写入,减少系统IO次数,从而提高读写效率。字节缓冲流构造方法创建字节缓冲输入流:BufferedInputStreambis=newBufferedInputStream(newFileInputStream("bis.txt"));创建字节缓冲输出流:BufferedOutputStreambos=newBufferedOutputStream(newFileOutputStream("bos.txt"));代码演示:publicclassDemo{publicstaticvoidmain(String[]args)throwsIOException{//记录开始时间longstart=System.currentTimeMillis();//创建流对象newBufferedStreamnewFileOutputStream("d:\\ChromeSetup_copy.exe"));){//读写数据intlen;byte[]bytes=newbyte[8*1024];while((len=bis.read(bytes))!=-1){bos.write(bytes,0,len);}}catch(IOExceptione){e.printStackTrace();}//记录结束时间longend=System.currentTimeMillis();System.out.println("bufferstreamUsearraycopytime:"+(end-start)+"milliseconds");}}输出结果为:BufferedstreamUsearraycopytime:10millisecondsCharacterbufferstream构造函数创建字符缓冲输入流BufferedReaderbr=newBufferedReader(newFileReader("br.txt"));创建字符缓冲输出流BufferedWriterbw=newBufferedWriter(newFileWriter("bw.txt"));独特的方法BufferedReader:publicStringreadLine():读取一行文本BufferedWriter:publicvoidnewLine():写入一行分隔符,由系统属性定义的符号。readLinemethoddemopublicclassDemo{publicstaticvoidmain(String[]args)throwsIOException{//创建流对象BufferedReaderbr=newBufferedReader(newFileReader("a.txt"));//定义一个字符串,保存一行文本读取Stringline=null;//循环读取,读取到最后返回nullwhile((line=br.readLine())!=null){System.out.println(line);}//释放资源br.close();}}输出结果For:aaaaabbbbbcccccpublicclassDemo{publicstaticvoidmain(String[]args)throwsIOException{//创建流对象BufferedWriterbw=newBufferedWriter(newFileWriter("a.txt"));//写出数据bw.write("ccccc");//写出换行符bw.newLine();bw.write("bbbbb");bw.newLine();bw.write("aaaaa");bw.newLine();//释放资源bw.close();}}output结果为:2.转换流InputStreamReader类转换流java.io.InputStreamReader是Reader的子类,读取字节并使用指定的字符集解码为字符。其字符集可以自定义,也可以使用平台默认的字符集。构造函数InputStreamReader(InputStreamin):使用默认字符集创建一个字符流。InputStreamReader(InputStreamin,StringcharsetName):创建指定字符集的字符流。OutputStreamWriter类将流java.io.OutputStreamWriter转换为Writer的子类,以使用指定的字符集将字符编码为字节。其字符集可以自定义,也可以使用平台默认的字符集。构造函数OutputStreamWriter(OutputStreamin):使用默认字符集创建一个字符流。OutputStreamWriter(OutputStreamin,StringcharsetName):创建指定字符集的字符流。代码演示:publicclassDemo{publicstaticvoidmain(String[]args)throwsIOException{//1。定义文件路径Stringa="a.txt";Stringb="b.txt";//2.创建流对象//2.1转换输入流,指定GBK编码InputStreamReaderisr=newInputStreamReader(newFileInputStream(a),"GBK");//2.2转换输出流,默认utf8编码OutputStreamWriterosw=newOutputStreamWriter(newFileOutputStream(b));//3.读写数据//3.1定义数组char[]c=newchar[1024];//3.2定义长度intlen;//3.3循环读取while((len=isr.read(c))!=-1){//循环写入osw.write(c,0,len);}//4.释放资源osw.close();isr.close();}}3.序列化流ObjectOutputStream类java.io.ObjectOutputStream类,将Java对象Output的原始数据类型写入文件,实现对象的持久化存储。构造函数publicObjectOutputStream(OutputStreamout):创建一个具有指定OutputStream的ObjectOutputStream。关于对象序列化的注意事项:此类必须实现java.io.Serializable接口。Serializable是一个标记接口。不实现此接口的类将不会序列化或反序列化任何状态,并且会抛出NotSerializableException。此类的所有属性都必须是可序列化的。如果有不需要序列化的属性,必须将该属性标记为transient,写入对象的方法用transient关键字修饰:publicfinalvoidwriteObject(Objectobj):将指定对象写出ObjectInputStream类ObjectInputStream反序列化流将使用ObjectOutputStream序列化的原始数据恢复到一个对象。构造函数publicObjectInputStream(InputStreamin):创建一个指定InputStream的ObjectInputStream。对象反序列化注意:必须是能找到类文件的类。如果找不到此类的类文件,则会抛出ClassNotFoundException。可以找到class文件,但是对象序列化后class文件被修改了,那么反序列化操作就会失败,抛出InvalidClassException。如果能找到一个对象的class文件,我们就可以反序列化,调用ObjectInputStream读取对象的方法:publicfinalObjectreadObject():读取一个对象代码演示:publicclassDemo{publicstaticvoidmain(String[]args)throwsException{//创建一个学生对象Studentsstudent=newStudent("张三","zahgnsan");Studentsstudent2=newStudent("李四","lisi");Studentstudent3=newStudent("王舞","wagnwu");ArrayListarrayList=newArrayList<>();arrayList.add(student);arrayList.add(student2);arrayList.add(student3);//序列化操作serializ(arrayList);//反序列化ObjectInputStreamois=newObjectInputStream(newFileInputStream("list.txt"));//读取对象强制为ArrayList类型ArrayListlist=(ArrayList)ois.readObject();for(inti=0;iarrayList)抛出Exception{//创建序列化流ObjectOutputStreamoos=newObjectOutputStream(newFileOutputStream("list.txt"));//写出对象oos.writeObject(arrayList);//释放资源oos.close();}}四、打印streamPrintStream类是java.io.PrintStream类,可以方便的打印各种数据类型的值。构造方法publicPrintStream(StringfileName):使用指定的文件名创建一个新的打印流System.out是PrintStream类型的,只是,但是它的流向是由系统指定的,打印在控制台上代码演示:publicclassDemo{publicstaticvoidmain(String[]args)throwsIOException{//控制台直接输出张三System.out.println("张三");//创建打印流,指定文件名PrintStreamzs=newPrintStream("zs.txt");//设置系统的打印流向,输出到zs.txtSystem.setOut(zs);//调用系统的打印流,zs.txt输出张三System.out.println(《张三》);}}【小编推荐】Linux终端如何连接WiFi?Netflix产品Spinnaker微服务实践分析如何在Docker容器中运行Docker“3种方式”美国“禁令”周日生效:TikTok坚持诉讼,微信企业海外版悄然改名使用用于管理SSH连接的开源工具nccm

最新推荐
猜你喜欢