先来看一下流的概念:java
在程序中全部的数据都是以流的方式进行传输或保存的,程序须要数据的时候要使用输入流读取数据,而当程序须要将一些数据保存起来的时候,就要使用输出流完成。数组
程序中的输入输出都是以流的形式保存的,流中保存的实际上全都是字节文件。app
字节流与字符流函数
在java.io包中操做文件内容的主要有两大类:字节流、字符流,两类都分为输入和输出操做。在字节流中输出数据主要是使用OutputStream完成,输入使的是InputStream,在字符流中输出主要是使用Writer类完成,输入流主要使用Reader类完成。(这四个都是抽象类)编码
java中提供了专用于输入输出功能的包Java.io,其中包括:
InputStream,OutputStream,Reader,Writer
InputStream 和OutputStream,两个是为字节流设计的,主要用来处理字节或二进制对象,
Reader和 Writer.两个是为字符流(一个字符占两个字节)设计的,主要用来处理字符或字符串.spa
字符流处理的单元为2个字节的Unicode字符,分别操做字符、字符数组或字符串,而字节流处理单元为1个字节,操做字节和字节数组。因此字符流是由Java虚拟机将字节转化为2个字节的Unicode字符为单位的字符而成的,因此它对多国语言支持性比较好!若是是音频文件、图片、歌曲,就用字节流好点,若是是关系到中文(文本)的,用字符流好点
全部文件的储存是都是字节(byte)的储存,在磁盘上保留的并非文件的字符而是先把字符编码成字节,再储存这些字节到磁盘。在读取文件(特别是文本文件)时,也是一个字节一个字节地读取以造成字节序列设计
字节流可用于任何类型的对象,包括二进制对象,而字符流只能处理字符或者字符串; 字节流提供了处理任何类型的IO操做的功能,但它不能直接处理Unicode字符,而字符流就能够
字节流是最基本的,全部的InputStrem和OutputStream的子类都是,主要用在处理二进制数据,它是按字节来处理的 但实际中不少的数据是文本,又提出了字符流的概念,它是按虚拟机的encode来处理,也就是要进行字符集的转化 这两个之间经过 InputStreamReader,OutputStreamWriter来关联,其实是经过byte[]和String来关联 在实际开发中出现的汉字问题实际上都是在字符流和字节流之间转化不统一而形成的 。code
字节流和字符流的操做过程: 对象
以文件操做为例,主要的操做流程以下:blog
1 使用File类打开一个文件
2 经过字节流或字符流的子类,指定输出的位置
3 进行读/写操做
4 关闭输入/输出
IO操做属于资源操做,必定要记得关闭
字节流的操做:
字节流主要是操做byte类型数据,以byte数组为准,主要操做类就是OutputStream、InputStream
字节输出流:OutputStream
OutputStream是整个IO包中字节输出流的最大父类,此类的定义以下:
public abstract class OutputStream extends Object implements Closeable,Flushable
从以上的定义能够发现,此类是一个抽象类,若是想要使用此类的话,则首先必须经过子类实例化对象,那么若是如今要操做的是一个文件,则可使用:FileOutputStream类。经过向上转型以后,能够为OutputStream实例化
Closeable表示能够关闭的操做,由于程序运行到最后确定要关闭
Flushable:表示刷新,清空内存中的数据
FileOutputStream类的构造方法以下:
public FileOutputStream(File file)throws FileNotFoundException
写数据:
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; public class Test11 { public static void main(String[] args) throws IOException { File f = new File("d:" + File.separator+"test.txt"); OutputStream out=new FileOutputStream(f);//若是文件不存在会自动建立 String str="Hello World"; byte[] b=str.getBytes(); out.write(b);//由于是字节流,因此要转化成字节数组进行输出 out.close(); } } 也能够一个字节一个字节进行输出,以下: import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; public class Test11 { public static void main(String[] args) throws IOException { File f = new File("d:" + File.separator+"test.txt"); OutputStream out=new FileOutputStream(f);//若是文件不存在会自动建立 String str="Hello World"; byte[] b=str.getBytes(); for(int i=0;i<b.length;i++){ out.write(b[i]); } out.close(); } }
以上输出只会进行覆盖,若是要追加的话,请看FileOutputStream类的另外一个构造方法:
public FileOutputStream(File file,boolean append)throws FileNotFoundException
在构造方法中,若是将append的值设置为true,则表示在文件的末尾追加内容。
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; public class Test11 { public static void main(String[] args) throws IOException { File f = new File("d:" + File.separator+"test.txt"); OutputStream out=new FileOutputStream(f,true);//追加内容 String str="\r\nHello World"; byte[] b=str.getBytes(); for(int i=0;i<b.length;i++){ out.write(b[i]); } out.close(); } }
字节输入流:InputStream
既然程序能够向文件中写入内容,则就能够经过InputStream从文件中把内容读取进来,首先来看InputStream类的定义:
public abstract class InputStream extends Object implements Closeable
与OutputStream类同样,InputStream自己也是一个抽象类,必须依靠其子类,若是如今是从文件中读取,就用FileInputStream来实现。
观察FileInputStream类的构造方法:
public FileInputStream(File file)throws FileNotFoundException
读文件:
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class Test12 { public static void main(String[] args) throws IOException { File f = new File("d:" + File.separator+"test.txt"); InputStream in=new FileInputStream(f); byte[] b=new byte[(int) f.length()]; //根据文件的大小来定义字节数组的大小int len=in.read(b); in.close(); System.out.println(new String(b,0,len)); } }
换种方式,一个字节一个字节读入
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class Test14 { public static void main(String[] args) throws IOException { File f = new File("d:" + File.separator+"test.txt"); InputStream in=new FileInputStream(f); byte[] b=new byte[(int) f.length()]; for(int i=0;i<b.length;i++){ b[i]=(byte) in.read(); } in.close(); System.out.println(new String(b)); } }
但以上状况只适合知道输入文件的大小,不知道的话用以下方法:
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class Test15 { public static void main(String[] args) throws IOException { File f = new File("d:" + File.separator+"test.txt"); InputStream in=new FileInputStream(f); byte[] b=new byte[1024]; int temp=0; int len=0; while((temp=in.read())!=-1){//-1为文件读完的标志 b[len]=(byte) temp; len++; } in.close(); System.out.println(new String(b,0,len)); } }
字符流
在程序中一个字符等于两个字节,那么java提供了Reader、Writer两个专门操做字符流的类。
字符输出流:Writer
Writer自己是一个字符流的输出类,此类的定义以下:
public abstract class Writer extends Object implements Appendable,Closeable,Flushable
此类自己也是一个抽象类,若是要使用此类,则确定要使用其子类,此时若是是向文件中写入内容,因此应该使用FileWriter的子类。
FileWriter类的构造方法定义以下:
public FileWriter(File file)throws IOException
字符流的操做比字节流操做好在一点,就是能够直接输出字符串了,不用再像以前那样进行转换操做了。
写文件:
import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; public class Test16 { public static void main(String[] args) throws IOException { File f = new File("d:" + File.separator+"test.txt"); Writer out=new FileWriter(f); String str="Hello World"; out.write(str); out.close(); } } 在默认状况下再次输出会覆盖,追加的方法也是在构造函数上加上追加标记 import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; public class Test17 { public static void main(String[] args) throws IOException { File f = new File("d:" + File.separator+"test.txt"); Writer out=new FileWriter(f,true);//追加 String str="\r\nHello World"; out.write(str); out.close(); } }
字符输入流:Reader
Reader是使用字符的方式从文件中取出数据,Reader类的定义以下:
public abstract class Reader extends Objects implements Readable,Closeable
Reader自己也是抽象类,若是如今要从文件中读取内容,则能够直接使用FileReader子类。
FileReader的构造方法定义以下:
public FileReader(File file)throws FileNotFoundException
以字符数组的形式读取出数据:
import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.Reader; public class Test18 { public static void main(String[] args) throws IOException { File f = new File("d:" + File.separator+"test.txt"); Reader input=new FileReader(f); char[] c=new char[1024]; int len=input.read(c); input.close(); System.out.println(new String(c,0,len)); } } 也能够用循环方式,判断是否读到底 import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.Reader; public class Test19 { public static void main(String[] args) throws IOException { File f = new File("d:" + File.separator+"test.txt"); Reader input=new FileReader(f); char[] c=new char[1024]; int temp=0; int len=0; while((temp=input.read())!=-1){ c[len]=(char) temp; len++; } input.close(); System.out.println(new String(c,0,len)); } }
字节流与字符流的区别
字节流和字符流使用是很是类似的,那么除了操做代码的不一样以外,还有哪些不一样呢?
字节流在操做的时候自己是不会用到缓冲区(内存)的,是与文件自己直接操做的,而字符流在操做的时候是使用到缓冲区的
字节流在操做文件时,即便不关闭资源(close方法),文件也能输出,可是若是字符流不使用close方法的话,则不会输出任何内容,说明字符流用的是缓冲区,而且可使用flush方法强制进行刷新缓冲区,这时才能在不close的状况下输出内容
那开发中究竟用字节流好仍是用字符流好呢?
在全部的硬盘上保存文件或进行传输的时候都是以字节的方法进行的,包括图片也是按字节完成,而字符是只有在内存中才会造成的,因此使用字节的操做是最多的。
若是要java程序实现一个拷贝功能,应该选用字节流进行操做(可能拷贝的是图片),而且采用边读边写的方式(节省内存)。
虽然Java支持字节流和字符流,但有时须要在字节流和字符流二者之间转换。InputStreamReader和OutputStreamWriter,这两个为类是字节流和字符流之间相互转换的类。
InputSreamReader用于将一个字节流中的字节解码成字符:
有两个构造方法:
InputStreamReader(InputStream in);
功能:用默认字符集建立一个InputStreamReader对象
InputStreamReader(InputStream in,String CharsetName);
功能:接收已指定字符集名的字符串,并用该字符建立对象
OutputStream用于将写入的字符编码成字节后写入一个字节流。
一样有两个构造方法:
OutputStreamWriter(OutputStream out);
功能:用默认字符集建立一个OutputStreamWriter对象;
OutputStreamWriter(OutputStream out,String CharSetName);
功能:接收已指定字符集名的字符串,并用该字符集建立OutputStreamWrite对象
为了不频繁的转换字节流和字符流,对以上两个类进行了封装。
BufferedWriter类封装了OutputStreamWriter类;
BufferedReader类封装了InputStreamReader类;
封装格式:
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader in= new BufferedReader(new InputStreamReader(System.in);
利用下面的语句,能够从控制台读取一行字符串:
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String line=in.readLine();