字节缓冲流
- BufferedOutputStream类,该类实现缓冲输出流,通过设置这样的输出六,应用程序可以向底层输出流写入字节。而不必为写入的每个字节导致底层系统的调用。
- 他可以向FileOutputStream这样的底层输出流写数据,通过一个缓冲区,可以一次性把数据写到文件中。底层调用次数就减少,提高了效率
- 构造方法
- 和他对应的事BufferedInputStream,他是一个具体类。创建BufferedInputStream将创建一个内部缓冲区数组,当从流中读取或跳过字节时,内部缓冲区需要从所包含的输入流中重新填充,一次很多个字节,也提高了读数据的效率。
- 构造方法
- 字节缓冲流他们的构造方法
字节缓冲输出流:BufferedOutputStream(OutputStream out)
字节缓冲输入流:BufferedInputStream(InputStream in)
- 为什么构造方法需要的是字节流,而不是具体的文件或者路径呢?这是因为字节缓冲流仅仅提供缓冲区,而真正读写数据还得依靠基本的字节流对象进行操作。
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferStreamDemo {
public static void main(String[] args) throws IOException {
//字节缓冲输出流
FileOutputStream fos = new FileOutputStream("Study/src/com/itheima/file/bos.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
}
}
- 其中字节缓冲输出流这两步可以把他合并成一步
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("Study/src/com/itheima/file/bos.txt"));
- 先来看看他的源码,他用this调用了本类的构造方法,给了8192
- 8192给了这里,我们最后把值给了数组buf
- 然后我们来写数据,写数据要靠底层的输出流去做
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("Study/src/com/itheima/file/bos.txt"));
bos.write("hello\n".getBytes());
bos.write("world\n".getBytes());
bos.close();
- 读数据,首先创建字节缓冲流。
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("/home/ding/IdeaProjects/Study/src/com/itheima_02/file/bos.txt"));
int by;
while((by=bis.read())!=-1){
System.out.print((char)by);
}
bis.close();
- 来看一下源码,size最后传递给buf数组
- 来看一下buf默认的范围
- 他的值默认大小也是8192,默认也是封装一个8192字节的数组
- 然后我们用另外一种方法来读取数据,一次读取一个字节数组。
byte[] bys = new byte[1024];
int len;
while((len=bis.read(bys))!=-1){
System.out.println(new String(bys,0,len));
}
bis.close();
复制视频
- 将1.avi复制为2.avi
- 思路:
- 根据数据源创建字节输入流对象
- 根据目的地创建字节输出流对象
- 读写数据,复制视频
- 释放资源
- 第一种方法,基本字节流一次读写一个字节
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyAviDemo {
public static void main(String[] args) throws IOException {
//记录开始时间
long startTime = System.currentTimeMillis();
//复制视频
method1();
//记录结束时间
long endTime = System.currentTimeMillis();
System.out.println("共耗时:"+(endTime - startTime) + "毫秒");
}
// /home/ding/IdeaProjects/Study/src/com/itheima_02/file/
//基本字节流一次读写一个字节
public static void method1() throws IOException {
FileInputStream fis = new FileInputStream("/home/ding/IdeaProjects/Study/src/com/itheima_02/file/1.avi");
FileOutputStream fos = new FileOutputStream("/home/ding/IdeaProjects/Study/src/com/itheima_02/file/2.avi");
int by;
while((by=fis.read())!=-1){
fos.write(by);
}
fos.close();
fis.close();
}
}
- 方法2,接本字节流一次读写一个字节数组
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyAviDemo02 {
public static void main(String[] args) throws IOException{
//记录开始时间
long startTime = System.currentTimeMillis();
//复制视频
method2();
//记录结束时间
long endTime = System.currentTimeMillis();
System.out.println("共耗时:"+(endTime - startTime) + "毫秒");
}
public static void method2() throws IOException {
FileInputStream fis = new FileInputStream("/home/ding/IdeaProjects/Study/src/com/itheima_02/file/1.avi");
FileOutputStream fos = new FileOutputStream("/home/ding/IdeaProjects/Study/src/com/itheima_02/file/3.avi");
byte[] bys = new byte[1024];
int len;
while((len=fis.read(bys))!=-1){
fos.write(bys,0,len);
}
fos.close();
fis.close();
}
}
- 第三种方法,字节缓冲流一次读写一个字节
public static void method3() throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("/home/ding/IdeaProjects/Study/src/com/itheima_02/file/1.avi"));
BufferedOutputStream bos = new BufferedOutputStream(new FileInputStream("/home/ding/IdeaProjects/Study/src/com/itheima_02/file/4.avi"));
int by;
while((by=bis.read())!=-1){
bos.write(by);
}
bos.close();
bis.close();
}
- 第四种方法,字节缓冲流一次读写一个字节数组
public static void method4() throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("/home/ding/IdeaProjects/Study/src/com/itheima_02/file/1.avi"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("/home/ding/IdeaProjects/Study/src/com/itheima_02/file/5.avi"));
byte[] bys = new byte[1024];
int len;
while((len=bis.read(bys))!=-1){
bos.write(bys,0,len);
}
bos.close();
bis.close();
}
- 这几种方式耗时不一样。基本字节流一次读写一个字节最慢不要使用。
编码表
- 字符流等于字节流加编码表
- 基础知识:计算机中存储的信息都是用二进制数表示的,我们在屏幕上看到的英文、汉子等自负都是二进制数转换之后的结果。
- 十进制的666对应二进制的001010011010
- 按照某种规则,将字符存储到计算机中,称为编码。反之,将存储在计算机中的二进制数按照某种规则解析显示出来,称为解码。按照A编码解析,这样才能显示正确的文本符号。否则就会导致乱码现象。
- 字符编码:就是一套自然语言的字与二进制数之间的对应规则(A,65)。
- 字符集:是一个系统支持的所有字符的集合,包括各国家文字、标点符号、图形符号、数字等。常见的字符集有ASCII字符集、GBXXX字符集、Unicode字符集等。
- ASCII 美国信息交换标准代码:是基于拉丁字母的一套电脑编码系统,用于显示现代英语,主要包括控制字符(回车键、退格、换行键等)和可显示字符(英文大小写自负、阿拉伯数字和西文符号)
- 基本的ASCII字符集,使用7位标识一个字符,共128字符。ASCII的扩展字符集使用8位标识一个字符,共256字符,方便支持欧洲常用租房。是一个系统支持的所有字符的集合,包括个国家文字、标点符号、图片符号、数字等。
- GB2312 :简体中文码表,小于127字符的意义与原来相同,但大于127的字符连在一起时,就表示一个汉字,这样大约可以组合了包含7000多个简体汉字,此外数学符号、罗马希腊的字母、日文的假名都编进去了,连载ASCII里本来就有的数字、标点、字母都统统重新编了两个字节长的编码,这就是常说的“全角”字符,而原来在127号以下的那些就叫半角字符了。
- GBK:最常用的中文码表。是在GB2312标准基础上的扩展规范,使用了双字节编码方法,共收录了21003个汉字,完全兼容GB2312标准,同时支持繁体汉字和日韩汉字。
- Unicode字符集:为表达任意语言的任意字符而设计的,是业界的一种标准,也称为统一码、标准万国码。他最多使用4个字节的数字来表达每个字母、符号或者文字。有三种编码方案,UTF-8、UTF-16和UTF32.最常用的位UTF-8编码。
- UTF-8编码:可以用来表示Unicode标准中任意字符,他是电子邮件、网页及其他存储或传送文字的应用,优先采用的编码。互联网工程工作小组(IETF)要求所有互联网协议都支持UTF-8编码,他使用一至四个字节为每个字符编码。
- 编码规则:128个US-ASCII字符,只需一个字节编码,拉丁文等字符需要两个字节编码。大部分常用字(含中文),使用三个字节编码。其他极少使用的Unicode辅助字符,使用四个字节编码。
字符串中的编码解码问题
- 编码的方法:
- 使用平台的默认字符集将该String编码为一系列字节,将结果存储到新的字节数组中
byte[] getBytes()
- 使用指定的字符集将该String编码为一系列字节,将结果存储到新的字节数组中
byte[] getBytes(String charsetName)
- 解码使用的方法:
- 通过使用平台的默认字符集解码指定的字节数组来构造新的String
String(byte[] bytes)
- 通过指定的字符集解码指定的字节数组来构造新的String
String(byte[] bytes,String charsetName)
- 看一下平台默认的字符集输出的是什么
String s = "中国";
byte[] bys = s.getBytes();
System.out.println(Arrays.toString(bys));
- 上图是采用3个字节表示一个汉字,所以默认字符集是utf-8 。接下来我们用指定编码的方式来做动作。
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
public class StringDemo {
public static void main(String[] args) throws UnsupportedEncodingException {
String s = "中国";
// byte[] bys = s.getBytes();
byte[] bys = s.getBytes("UTF-8");
System.out.println(Arrays.toString(bys));
}
}
- 使用GBK的话采用两个字节表示一个汉字
byte[] bys = s.getBytes("GBK");
- 我们做解码,先用带一个参数的构造方法。
String s = "中国";
byte[] bys = s.getBytes();
System.out.println(Arrays.toString(bys));
String ss = new String(bys);
System.out.println(ss);
- 上面使用了平台默认字符集编码,然后用平台默认字符集解码所以没问题。接下来我们来指定字符集。
String ss = new String(bys,"GBK");
字符流中的编码解码问题
- 字符流抽象基类:Reader 字符输入流的抽象类 ,Writer:字符输出流的抽象类。
- 字符流中和编码解码问题相关的两个类:InputStreamReader ,OutputStreamWrite。
- InputStreamReader,是从字符流到字节流的桥梁,他读取字节,并使用指定的字符集并把它解码为字符。他使用的字符集可以由名称指定,也可以被明确指定,或者可以接受平台的默认字符集。
- OutputStreamWriter ,是从字符流到字节流的桥梁,使用指定的字符集将写入的字符编码为字节,他使用的字符集可以由名称指定或明确指定,又或者可以接收平台默认的字符集。
- 我们使用这两个构造方法
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class ConversionStreamDemo {
public static void main(String[] args) throws IOException {
// FileOutputStream fos = new FileOutputStream("基础语法/src/io/osw.txt");
// OutputStreamWriter osw = new OutputStreamWriter(fos);
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("基础语法/src/io/osw.txt"));
osw.write("中国");
osw.close();
}
}
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("基础语法/src/io/osw.txt"),"GBK");
- 虽然我们读不懂GBK乱码,但是可以通过InputStreamReader来读懂。他只要按照字符编码GBK来读就能读懂。而他的构造方法
- 字符流读数据有两种方法:一次读取一个数据,一次读取一个字符数组的数据。
- 这里我们采用GBK来写,通过默认字符编码读取。所以这样读取会有问题
InputStreamReader isr = new InputStreamReader(new FileInputStream("基础语法/src/io/osw.txt"));
int ch;
while ((ch=isr.read())!=-1){
System.out.println((char)ch);
}
isr.close();
- 所以要指定GBK的方法来读取数据
InputStreamReader isr = new InputStreamReader(new FileInputStream("基础语法/src/io/osw.txt"), "GBK");
字符流写数据的5种方式
方法名 | 说明 |
---|---|
void write(int c) | 写一个字符 |
void write(char[] cbuf) | 写入一个字符数组 |
void write(char[] cbuf, int off, int len) | 写入字符数组的一部分 |
void write(String str) | 写一个字符串 |
void write(String str, int off, int len) | 写一个字符串的一部分 |
- 字符流写数据并不能直接写数据到文件里面来,因为他最终要通过字节流来写的。所以他还在缓冲区里面。如何让数据直接进入文件里面来呢?要用刷新流。
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class OutputStreamWriterDemo {
public static void main(String[] args) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("基础语法/src/io/osw2.txt"));
osw.write(97);
osw.flush();
}
}
- 在close() 释放资源之前,会做一次flush。
- 写入一个字符数组
char[] chs = {'a','b','c','d','e'};
osw.write(chs);
osw.close();
- 写入索引位置开始的字符
osw.write(chs,0,chs.length);
- 一次写一个字符串
osw.write("abcdefg");
osw.write("abcde",0,"abcde".length());
字符流读数据的2种方式
方法名 | 说明 |
---|---|
int read() | 一次读一个字符数据 |
int read(char[] cbuf) | 一次读一个字符数组数据 |
- 一次读一个字符数据
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputStreamReaderDemo {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("基础语法/src/io/osw.txt"));
int ch;
while((ch=isr.read())!=-1){
System.out.print((char)ch);
}
isr.close();
}
}
- 一次读一个字符数组
char[] chs = new char[1024];
int len;
while((len=isr.read(chs))!=-1){
System.out.print(new String(chs,0,len));
}
复制java文件
- 把ConversionStreamDemo.java复制到Copy.demo。
- 思路:
- 根据数据源创建字符输入流对象
- 根据目的地创建字符输出流对象
- 读写数据、复制文件
- 释放资源
import java.io.*;
public class CopyJavaDemo01 {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("基础语法/src/io/ConversionStreamDemo.java"));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("基础语法/src/io/CopyJava.java"));
int ch;
while((ch=isr.read())!=-1){
osw.write(ch);
}
osw.close();
isr.close();
}
}
import java.io.*;
public class CopyJavaDemo01 {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("基础语法/src/io/ConversionStreamDemo.java"));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("基础语法/src/io/CopyJava.java"));
char[] chs = new char[1024];
int len;
while((len=isr.read(chs))!=-1){
osw.write(chs,0,len);
}
osw.close();
isr.close();
}
}
字符流复制Java文件改进版
- 需求:把ConversionStreamDemo.java复制到Copy.java
- 分析:
- 转换流的名字比较长,而我们常见的操作都是按照本地默认编码实现的,所以,为了简化书写,转换流提供了对应的子类。InputStreamReader有一个子类叫做FileReader,是用来读取原始字节流
构造方法使用如下的方法
对应的还有一个Filewriter。是用来写字符文件的一个流,他的构造方法想也是选用带String的这个
- FileReader:用于读取字符文件的便捷类 FileReader(String fileName)
- FileWriter: 用于写入字符文件袋便捷类 FileWriter(String fileName)
- 数据源和目的地分析 。数据源 /home/ding/IdeaProjects/Study/src/com/itheima_02/file/ConversionStreamDemo.java – 读数据 – Reader – InputStreamReader – FileReader 目的地:/home/ding/IdeaProjects/Study/src/com/itheima_02/file/Copy.java–写数据–Writer—OutputStreamWriter—FileWriter
- 思路
1.根据数据源创建字符输入流对象
2.根据目的地创建字符输出流对象
3.读写数据,复制文件
4.释放资源
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyJavaDemo02 {
public static void main(String[] args) throws IOException {
//根据数据源创建字符输入流对象
FileReader fr = new FileReader("/home/ding/IdeaProjects/Study/src/com/itheima_02/file/ConversionStreamDemo.java");
FileWriter fw = new FileWriter("/home/ding/IdeaProjects/Study/src/com/itheima_02/file/Copy.java");
int ch;
while((ch=fr.read())!=-1){
fw.write(ch);
}
fr.close();
fw.close();
}
}
char[] chs = new char[1024];
int len;
while((len=fr.read(chs))!=-1){
fw.write(chs,0,len);
}