本文共 8980 字,大约阅读时间需要 29 分钟。
按照流向可以分为:输入流(如:键盘,麦克风),输出流(如:显示器,音箱)
按照传输单位可以分为:字节流和字符流字节流: 它处理单元为1个字节(byte),操作字节和字节数组,存储的是二进制文件,如果是音频文件、图片、歌曲,就用字节流好点(1byte = 8位);
字符流: 它处理的单元为2个字节的Unicode字符,分别操作字符、字符数组或字符串,字符流是由Java虚拟机将字节转化为2个字节的Unicode字符为单位的字符而成的,如果是关系到中文(文本)的,用字符流好点(1Unicode = 2字节 = 16位);
所有文件的储存是都是字节(byte)的储存,在磁盘上保留的并不是文件的字符而是先把字符编码成字节,再储存这些字节到磁盘。在读取文件(特别是文本文件)时,也是一个字节一个字节地读取以形成字节序列。
字节流可用于任何类型的对象,包括二进制对象,而字符流只能处理字符或者字符串; 2. 字节流提供了处理任何类型的IO操作的功能,但它不能直接处理Unicode字符,而字符流就可以。
字节流是最基本的,所有的InputStrem和OutputStream的子类都是,主要用在处理二进制数据,它是按字节来处理的 但实际中很多的数据是文本,又提出了字符流的概念,它是按虚拟机的encode来处理,也就是要进行字符集的转化 这两个之间通过 InputStreamReader,OutputStreamWriter来关联,实际上是通过byte[]和String来关联 在实际开发中出现的汉字问题实际上都是在字符流和字节流之间转化不统一而造成的
最简单的区分字节流和字符流
万物皆文件,那就将文件在记事本里面打开,如果打开后能看的懂的就是字符流,如果看不懂那就是字节流
问题:
1.word.doc 数据字节流还是字符流? 答:.doc数据字节流。 2.Excel 数据字节流还是字符流? 答:要根据保存的格式进行判断,如果是保存为.csv那么他就是字符流,如果是其他的则数据字节流。字节流主要是操作byte类型数据,以byte数组为准,主要操作类就是OutputStream、InputStream。
字节输出流:OutputStream,OutputStream是整个IO包中字节输出流的最大父类
Closeable表示可以关闭的操作
示例为:图片的复制
步骤:
1.读入二进制文件(输入流,FileInputStream) 2.写入二进制文件(输出流,FileOutputStream)前请了解:try-with-resource,这是JDK1.7提供的新方法,让代码的可读性更高,但是并不是所有的对象都可以这样,当调用的类实现了closable接口就可以使用此种方式
代码案例
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;/** * 复制D:\io_test下的加菲猫文件到D:\io_test这个目录下 */public class IoDemo4 { public static void main(String[] args) throws IOException { //输入文件目录 String srcFilePath = "D:\\io_test\\加菲猫.jpg"; //输出文件目录(拷贝的路径) String destFilePath = "D:\\io_test\\加菲猫2.jpg"; FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { //输入流,传入路径 fileInputStream = new FileInputStream(srcFilePath); //输出流 fileOutputStream = new FileOutputStream(destFilePath); byte[] bytes = new byte[1024]; try { int count = 0; //如果不等于-1说明还没有读取完成,要继续读取 while((count = fileInputStream.read(bytes)) != -1){ //还有内容要读取,然后从0开始读取,上面count有读了多少个,那么就写多少个 fileOutputStream.write(bytes,0,count); } } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); }finally { //用完之后一定要关闭流,但是得判断不是空的时候才需要去关闭流 if(fileInputStream != null){ fileInputStream.close(); } if(fileOutputStream != null){ fileOutputStream.close(); } } }}
执行结果
注意:
1.因为字节流主要操作byte类型数据,注意里面参数的含义,如果是读取的话,比较常用的是第二种(FileOutputStream)也是一样,读取的时候一般用第一个
2.读取数据的返回结果(FileInputStream)
3.最后一定要关闭流
什么是缓存区?
定义:缓存区相当于缓存,它是存在内存中的 写操作: 没有使用缓存区:CPU读取每个字节之后直接操作磁盘(性能比较底)进行写完,写操作的瓶颈就会出现,因为每个字节都会操作一次磁盘 使用缓冲区:那么每次会将字符放入缓存区(内存),等缓冲区满了之后,才一次性写入磁盘 因为内存的操作速度远远大于磁盘,因此带缓冲区的输入流和输出流实现的效率就非常高(比如扔垃圾,一次性扔完和一次次扔肯定消耗的时间是有很大差距的)案例实现:复制D:\io_test\下的加菲猫—>D:\io_test\下变成加菲猫3
代码实现
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;/** * 带有缓冲区的字节流操作:图片复制 * */public class IoDemo5 { public static void main(String[] args) { //输入文件目录 String srcFilePath = "D:\\io_test\\加菲猫.jpg"; //输出文件目录(拷贝的路径) String destFilePath = "D:\\io_test\\加菲猫3.jpg"; try { //因为带有缓存区的是基于原始的类进行操作的 BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(srcFilePath)); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destFilePath)); { byte[] bytes = new byte[1024]; int count = 0; while ((count = bufferedInputStream.read(bytes)) != -1){ bufferedOutputStream.write(bytes,0,count); } } }catch (Exception e){ e.printStackTrace(); } }}
执行结果
在程序中一个字符等于两个字节,那么java提供了Reader、Writer两个专门操作字符流的类。
字符输出流:Writer。
字符输入流:Reader示例一:内容的写入:D:\io_test\1下创建一个text.txt文件,并写入内容“欢迎来到Java~”
import java.io.FileWriter;import java.io.IOException;/** * 内容的写入:D:\io_test\1下创建一个text.txt文件,并写入内容 */public class IoDemo7 { public static void main(String[] args) throws IOException { //定义文件地址 String filePath = "D:\\io_test\\1\\text.txt"; //写入内容 String content = "欢迎来到Java~"; //因为是写入文件,所以要使用FileWriter方法 FileWriter fileWriter = null; try { fileWriter = new FileWriter(filePath); fileWriter.write(content); } catch (IOException e) { e.printStackTrace(); }finally { //关闭流 fileWriter.close(); } }}
**注意:**如果不关闭流,虽然也能创建文件成功,不报错,但是可能存在内容无法写入的情况
示例二:但是上面这种操作如果是对于多行输入的话那么就回被覆盖因为这里的参数append会被默认成false,所以如果是多行写入的话需要手动设置成true
代码实现:那么来看看将append设置成true
public class IoDemo7 { public static void main(String[] args) throws IOException { //定义文件地址 String filePath = "D:\\io_test\\1\\text.txt"; //写入内容 String content = "欢迎来到java~,"; //因为是写入文件,所以要使用FileWriter方法 FileWriter fileWriter = null; try { fileWriter = new FileWriter(filePath,true); fileWriter.write(content); } catch (IOException e) { e.printStackTrace(); }finally { fileWriter.close(); } }}
执行结果
示例三:文件的读取,比如将文件下的内容读取到控制台
文件下的内容
import java.io.File;import java.io.FileNotFoundException;import java.util.Scanner;/** * 读取文件内容 */public class IoDemo8 { public static void main(String[] args) throws FileNotFoundException { String filePath = "D:\\io_test\\1\\tt.txt"; Scanner scanner = new Scanner(new File(filePath)); //因为读取的内容可能不止一条,所以加上while循环 while (scanner.hasNext()){ System.out.println(scanner.nextLine()); } }}
执行效果
示例四:将内容读取到内存中,如:将一个路径下的内容复制到另一个路径下
代码实现
import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;/** *将一个文件的内容写入到另一个文件下 * 如:D:\io_test\1\text.txt -> text2.txt */public class IoDemo9 { public static void main(String[] args) throws IOException { //读取文件的地址 String srcFilePath = "D:\\io_test\\1\\text.txt"; //目标文件的地址 String destFilePath = "D:\\io_test\\1\\text2.txt"; FileReader fileReader = null; FileWriter fileWriter = null; //先读后写 try{ fileReader = new FileReader(srcFilePath); fileWriter = new FileWriter(destFilePath); { //读操作 char[] chars = new char[1024]; while (true){ int count = fileReader.read(chars); //判断是否等于-1,如果等于-1,说明已经读取完了,跳出循环 if(count == -1){ break; } //写操作 fileWriter.write(chars,0,count); } } }catch (Exception e){ e.printStackTrace(); }finally { fileReader.close(); fileWriter.close(); } }}
执行结果
示例:如上述的示例四
代码实现
import java.io.*;/** * 带有缓冲区的字符流操作 * 如:内容的复制 */public class IoDemo10 { public static void main(String[] args) throws IOException { String srcFilePath = "D:\\io_test\\1\\tt.txt"; String destFilePath = "D:\\io_test\\1\\tt2.txt"; BufferedReader bufferedReader = null; BufferedWriter bufferedWriter = null; //先读取,后写入 try { bufferedReader = new BufferedReader(new FileReader(srcFilePath)); bufferedWriter = new BufferedWriter(new FileWriter(destFilePath)); { char[] chars = new char[1024]; while (true){ int count = bufferedReader.read(chars); if(count == -1){ break; } bufferedWriter.write(chars,0,count); } } }catch (Exception e){ e.printStackTrace(); }finally { bufferedReader.close(); bufferedWriter.close(); } }}
执行结果
字节流操作的基本单元是字节;字符流操作的基本单元为Unicode码元。
字节流在操作的时候本身不会用到缓冲区的,是与文件本身直接操作的;而字符流在操作的时候使用到缓冲区的。
所有文件的存储都是字节(byte)的存储,在磁盘上保留的是字节。
在使用字节流操作中,即使没有关闭资源(close方法),也能输出;而字符流不使用close方法的话,不会输出任何内容
转载地址:http://xgtaz.baihongyu.com/