按照流向的不同分为:输入流输出流按照处理数据单位的不通分为:字节流字符流(处理的文本文件)按照角色的不通分为节点流(直接作用于文件的)处理流
关于处理流的说明:(处理流作用在节点流上)
demo1:从硬盘存在的一个文件中,读取其内容到程序中,使用FileInputStream,显示在控制台上
1@Test23publicvoidtestStream1(){45//2.提供具体的流6FileReaderfileReader=null;7try{89//1.创建一个File类的对象,指明要操作的文件1011Filefile1=newFile("hello.txt");//文件位置为当前工程下面,不是当前类下12fileReader=newFileReader(file1);13//3.数据的读入14//read()返回读入的一个字符,文件达到末尾,则返回-115intdata=fileReader.read();16while(data!=-1){17System.out.print((char)data);18data=fileReader.read();19}20}catch(FileNotFoundExceptione){21//TODOAuto-generatedcatchblock22e.printStackTrace();23}catch(IOExceptione){24//TODOAuto-generatedcatchblock25e.printStackTrace();26}finally{27//4.关闭相应的流28if(fileReader!=null){29try{30fileReader.close();31}catch(IOExceptione){32e.printStackTrace();33}34}3536}3738}注意:要读取的文件一定要存在,否则空指针异常
说明:
1/2reader方法升级版(错误版)34@Description5@authorlixiuming6@date2021年5月5日上午11:22:377@throwsIOException89/10@Test11publicvoidtestReaderUpgrade2()throwsIOException{12//1.File类的实例化13Filefile1=newFile("hello.txt");//文件位置为当前工程下面,不是当前类下14//2.流的实例化15FileReaderfileReader=newFileReader(file1);16//3.流的写出17char[]charArr=newchar[5];//一次读入多个数据来提高效率18intlen=fileReader.read(charArr);//一次读入多个数据,返回每次读入的charArr数组中字符的个数,如果达到文件末尾,返回-119while(len!=-1){20for(inti=0;i 若最后取的数组长度不足5,那么前一个的数组是: 最后一组数组则是: 没有覆盖掉原来的数据; 1/2reader方法的升级版(正确版)34@Description5@authorlixiuming6@throwsIOException7@date2021年5月5日上午10:55:5389/10@Test11publicvoidtestReaderUpgrade()throwsIOException{12//1.File类的实例化13Filefile1=newFile("hello.txt");//文件位置为当前工程下面,不是当前类下14//2.流的实例化15FileReaderfileReader=newFileReader(file1);16//3.流的写出17char[]charArr=newchar[5];//一次读入多个数据来提高效率18intlen=fileReader.read(charArr);//一次读入多个数据,返回每次读入的charArr数组中字符的个数,如果达到文件末尾,返回-119while(len!=-1){20for(inti=0;i demo:从内存中写出数据到硬盘的文件里的简单说明 1@Test2publicvoidtestFileWirter()throwsIOException{3//1、提供File类的对象,指明写出到的文件4Filefile=newFile("hello2.txt");5//2.提供FileWrite的对象,用于数据的写出6FileWriterfw=newFileWriter(file);7//3.写出的具体操作89fw.write("Ihaveadream!");10fw.write("Youneedhaveadream!");11//4.关闭流12fw.close();1314}ViewCode说明: 输出操作,对应File可以不存在,并不会报异常; 如不存在,在输出过程中自动穿件文件 如果存在,a.如果流构造器使用的是FileWriter(file,false)/FileWriter(file),对象会对原有的文件进行覆盖; b.如果流构造器使用的是FileWriter(file,true)不会对原有的文件覆盖,而是在原有的文件内追加; 1/2文件的复制34@Description5@authorlixiuming6@throwsIOException7@date2021年5月5日上午10:19:4489/10@Test11publicvoidtestFileCopy(){12//2.创建输入和出入流的对象13FileReaderfr=null;14FileWriterfw=null;15try{16//1.创建File类的对象,指明读入和写出的文件17Filecopy=newFile("hello.txt");18FilecopyTo=newFile("hello2.txt");19fr=newFileReader(copy);20fw=newFileWriter(copyTo);21//3.数据的读入和写出22char[]charArr=newchar[5];23intlen=fr.read(charArr);24while(len!=-1){25for(inti=0;i 使用demo:用字节流读取文本文件 说明:不能使用字符流复制照片,但是字节流可以复制文本文件,但是,如果在复制的过程中,想在控制台查看文件,可能会有中文乱码; 1/2缓冲流实现的文件复制34@Description5@authorlixiuming6@date2021年5月5日下午2:58:3878/9@Test10publicvoidtestCopyFileByBuffer(){11BufferedInputStreambi=null;12BufferedOutputStreambo=null;13//3.复制14byte[]arr;15intlen;16try{17//1.造文件18Filecopy=newFile("hello.txt");19FilecopyTo=newFile("hello3.txt");20//2.造流21FileInputStreamfi=newFileInputStream(copy);22FileOutputStreamfo=newFileOutputStream(copyTo);23bi=newBufferedInputStream(fi);24bo=newBufferedOutputStream(fo);25arr=newbyte[1024];26len=bi.read(arr);27while(len!=-1){28for(inti=0;i 通途:可以编码和解码; demo:使用utf-8读取文件,使用gbk写文件; demo1:将基本类型数据写出到文件 1@Test2publicvoidtestData(){3FileOutputStreamfos=null;4DataOutputStreamdos=null;5try{6fos=newFileOutputStream("data.txt");7dos=newDataOutputStream(fos);8dos.writeUTF("ksdflskdfhslkdf");9dos.writeBoolean(true);10dos.writeLong(123123);11}catch(Exceptione){12//TODO:handleexception13}finally{14try{15if(dos!=null){16dos.close();17}18}catch(IOExceptione){19//TODOAuto-generatedcatchblock20e.printStackTrace();21}22}2324}ViewCodedemo2:将文件读入到内存 1//将基本类型和字符串从文件中读入到内存中,读入的顺序需要和写入时的顺序一致2@Test3publicvoidtestReadData(){4DataInputStreamdos=null;5try{6Filefile=newFile("data.txt");7FileInputStreamfos=newFileInputStream(file);8dos=newDataInputStream(fos);9byte[]arr=newbyte[1024];10Stringstr=dos.readUTF();11Booleanbo=dos.readBoolean();12longlo=dos.readLong();13System.out.println(str+"||"+bo+"||"+bo);14}catch(Exceptione){15//TODO:handleexception16}finally{17try{18if(dos!=null){19dos.close();20}21}catch(IOExceptione){22//TODOAuto-generatedcatchblock23e.printStackTrace();24}25}26}ViewCode注意:读入内存过程中,读入的顺序需要和写入时的顺序一致 字节流:PrintStream 字符流:printWriter提供了一系列重载的print()和println()改变输出位置 12@Test34publicvoidprintSrtreamWriter(){5//设置打印位置6PrintStreamps=null;7try{8Filefile=newFile("hello.txt");9FileOutputStreamfos=newFileOutputStream(file);10ps=newPrintStream(fos,true);11System.setOut(ps);12for(inti=0;i<255;i++){13System.out.print((char)i);14if(i%50==0){15System.out.println();//换行16}17}18}catch(Exceptione){19//TODO:handleexception20}finally{21if(ps!=null){22ps.close();23}24}25}ViewCode4.标准的输入,输出流demo:把输入内容转化成大写,输出,当直接输入“e”或者“exit”时,退出程序 1@Test2publicvoidtestSystemIn(){3InputStreamis=null;45InputStreamReaderisr=null;6BufferedReaderbr=null;78try{9is=System.in;10isr=newInputStreamReader(is);11br=newBufferedReader(isr);12Stringstr=null;13while(true){1415System.out.print("请输入字符串:");1617str=br.readLine();1819if(str.equalsIgnoreCase("e")||str.equalsIgnoreCase("exit")){2021break;2223}else{2425Stringstr1=str.toUpperCase();2627System.out.println(str1);2829}3031}32}catch(Exceptione){33//TODO:handleexception34}finally{35if(br!=null)36try{37br.close();38}catch(IOExceptione){39//TODOAuto-generatedcatchblock40e.printStackTrace();41}42}4344}ViewCode5.对象流①作用:用于存储和读取基本数据类型数据或者对象的处理流; 序列化:用ObjectOutputStream类保存基本类型数据或对象的机制反序列化:用ObjectInputStream类读取基本类型数据或对象 ②对象的序列化机制:允许把内存中的Java对象转换成与平台无关的二进制流,从而允许把这种二进制流持久的保存到磁盘上,或者通过网络将这种二进制流传输到另一个网络节点,当其他程序获取了这种二进制流,就可以恢复成原来的java对象;③序列化的好处:可以将任意实现了Serializable接口的对象转换为字节数据,使其在保存和传输时可被还原;序列化是RMI过程的传输和返回值都必须实现的机制,而RMI是javaEE的基础,因此序列化是javaEE平台的基础④注意: 如果需要让某个对象支持序列化机制:则必须让对象所属的类及其属性是可序列化的,为了让某个类是可序列化的,该类必须实现如下两个接口之一否则会抛出NoSerializableException的异常 ObjectInputStream和ObjectOutputStream不能序列化static和transient修饰的成员; ⑤demo示例(只是为了展示基本使用步骤) 1/**2*对象的序列号(简单版)3*4*@Description5*@authorlixiuming6*@throwsIOException7*@date2021年5月15日下午2:47:128*9*/10@Test11publicvoidtestObjectStream()throwsIOException{12Filefile=newFile("object.dat");13FileOutputStreamfi=newFileOutputStream(file);14ObjectOutputStreambos=newObjectOutputStream(fi);15bos.writeObject(newString("我爱北京天安门"));16bos.close();17}1819/**20*反向过程21*22*@Description23*@authorlixiuming24*@date2021年5月15日下午3:01:5925*26*/27@Test28publicvoidtestInput(){29ObjectInputStreambis=null;30try{31Filefile=newFile("object.dat");32FileInputStreamfi=newFileInputStream(file);33bis=newObjectInputStream(fi);34byte[]arr=newbyte[1024];35Stringstr=(String)bis.readObject();36System.out.println(str);37}catch(Exceptione){38//TODO:handleexception39}finally{40try{41if(bis!=null){42bis.close();43}44}catch(IOExceptione){45//TODOAuto-generatedcatchblock46e.printStackTrace();47}48}4950}ViewCode升级版: 注意:对类的要求: 补充:ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员 1publicclassPersionimplementsSerializable{23/**4*5*/6privatestaticfinallongserialVersionUID=-5081496688118179383L;7privateStringname;8privateintage;9privateAccountaccount;//必须实现Serializable接口1011publicStringgetName(){12returnname;13}1415publicvoidsetName(Stringname){16this.name=name;17}1819publicintgetAge(){20returnage;21}2223publicvoidsetAge(intage){24this.age=age;25}2627publicAccountgetAccount(){28returnaccount;29}3031publicvoidsetAccount(Accountaccount){32this.account=account;33}3435}3637classAccountimplementsSerializable{38/**39*40*/41privatestaticfinallongserialVersionUID=-3229470228130872968L;42privatedoublebanlence;4344publicdoublegetBanlence(){45returnbanlence;46}4748publicvoidsetBanlence(doublebanlence){49this.banlence=banlence;50}5152}ViewCode6.RandomAccessFile说明:1.RdomAccessFile直接继承于java.lang.Object类,实现了DataInput和DataOutput接口2.RandomAccessFile既可以作为输入流,又可以作为输出流 RandomAccessFile的访问模式: demo1以照片赋值为例 1@Test2publicvoidtestAccessFileStream(){3RandomAccessFileaf=null;4RandomAccessFileaf2=null;5try{6Filefile=newFile("collection.png");7Filefile2=newFile("collection22.png");8af=newRandomAccessFile(file,"r");9af2=newRandomAccessFile(file2,"rw");10byte[]arr=newbyte[1024];11intlen=af.read(arr);12while(len!=-1){13for(inti=0;i 1//对文件的操作2@Test3publicvoidtestAccessFileStream2(){4RandomAccessFileaf2=null;5try{6Filefile2=newFile("accessfile.txt");7af2=newRandomAccessFile(file2,"rw");8af2.seek(12);//指定写入位置(从该位置进行覆盖)9af2.write("1234567890".getBytes());//默认情况:文件不存在,则创建,存在,则覆盖(默认从头开始覆盖)1011}catch(Exceptione){12//TODO:handleexception13}finally{14try{15if(af2!=null){1617af2.close();18}19}catch(IOExceptione){20//TODOAuto-generatedcatchblock21e.printStackTrace();22}23}2425}