流即数据的流向,即数据的输入/输入方向。可以是文件,内存,硬盘的其他的设备。
Java流分为三类:1.按照处理数据大小:字节流和字符流2.按照流的方向:输入流和输出流3.按照功能分为:分为节点流和处理流
字节流和字符流: 1)字节流:读取的数据以字节为单位(byte),8bit,我们要与InputStream,OutputStream(抽象类)相关联。 当我们读取文件然后在写入到其它的文件时候,不用过多的关注读取的内容时,通常使用的是字节流,因为这相当于是在处理二进制文件。读取数据效率高,并且保持数据的完整性。 2)字符流:读取的数据以字符为单位(char),16bit,我们要与InputReader,OutputReader(抽象类)相关联。 当我们读取文件的内容,并要对文件的内容进行加工时(修改,判断等),通常是使用的字符流,因为我们读取的其实是按照一个字符。 读取的,文件的最基本单位是字符,同样保持数据的完整性。
输入流和输出流: 1)输入流:是指从流读入数据。 2)输出流:是指向流写出数据。
处理流和节点流(重点介绍):
节点流:即从特定的数据源读取写入数据(FileReader,PrintWriter)。
处理流:在已经存在的节点流或者处理流上,进行装饰提供更强大的读写功能。
1)缓冲流(Buffered)顾名思义就是带缓冲区.如:在节点流之上进行加工,添加缓冲区(FileInputStream=>BufferedInputStream)这样避免读取文件时候,大量进行对硬盘的读写,而是从缓冲区进行读写,提高读写效率。
2)转换流(StreamReader)即字节流与字符流的相互转换。比如:在进行读写字节流设备时候,我想调用读取字符流的函数,就可以通过转换流。将(字节流=>字符流) InputStreamReader in=new InputStreamReader(new InputStream())注意只能是字节流转换为字符流!!
3) 数据流(Data) 当读取写入具体的数值数据时候(Long Double)就可以采用DataInputStream和DataOutputStream流进行功能更加强大的写入和读取功能。
下面请看代码:(很多东西都写到代码里面请仔细阅读
[java] view plain copy //函数功能:拷贝文件 并在末尾加上helloworld public static void copyTofile(String sorFile,String dirFile) throws IOException{ //拷贝文件一般是用字节流不考虑文件内容 FileInputStream fin=null; //节点流 FileOutputStream fout=null; BufferedInputStream in=null; //处理流 BufferedOutputStream out=null; DataOutputStream dout=null; //处理流 try { //处理流在节点流进行装饰,处理流其是对流的功能的加强实现具体的更强大的功能 /* * BufferedInputStream和BufferedOutputStream但缓冲区的输入输出流 * 将文件读取到缓冲区,读取的文件其实就是再跟硬盘在进行交互 * 读取缓冲区,比硬盘的速度加快,效率提高,且避免进行大量的进行硬盘读写 * */ fin=new FileInputStream(new File(sorFile)); fout=new FileOutputStream(new File(dirFile)); in=new BufferedInputStream(fin); out=new BufferedOutputStream(fout); dout=new DataOutputStream(out); byte [] contentArr = new byte[1024]; int fileSize=0; while((fileSize=in.read(contentArr))!=-1){ out.write(contentArr,0,fileSize); } System.out.println("copy successfully"); dout.writeChars("helloworld"); //写入的是h e l l o w o r l d dout.writeBytes("helloworld"); //写入的helloworld(想一下这里区别 byte char) } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ dout.close(); in.close(); out.close(); fout.close(); fin.close(); } } //函数功能:统计文本中hello个数 public static void readFileStream(String sorFile) throws IOException{ //拷贝文件一般是用字节流不考虑文件内容 //统计文本中hello个数 FileInputStream fin=null; //节点流 BufferedReader in=null; //处理流->缓冲流 InputStreamReader inStremRead=null; //处理流->转换流 try { fin=new FileInputStream(new File(sorFile)); //字节流 inStremRead=new InputStreamReader(fin); //转换流(字节流->字符流) in=new BufferedReader(inStremRead); //缓冲流(转换流) String fileStr=null; int count=0; while((fileStr=in.readLine())!=null){ if(fileStr.contains("hello")) count++; } System.out.println(count); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ } } //函数功能:统计文本中hello个数 public static void countStr(String sorFile) throws IOException{ try { //读取文件内容字节流 FileReader fin=new FileReader(new File(sorFile)); String fileStr=null; int count=0; BufferedReader in=new BufferedReader(fin); while((fileStr=in.readLine())!=null){ if(fileStr.contains("hello")) count++; } System.out.println(count); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } BufferWriter和DataOutputStream都可以实现数据的输出,那么这2种输出流有什么区别,看下api就知道了。 BufferedWriter:可以传int型值,传char数组,传StringDataOutputStream:可以传8种基本数据类型的值,可以传String.如果你要传一个double类型的数,那你得用DateOutputStream传吧。建议不管哪个流最好最后都flush()下