I/O流(总结)
I/O流
1.流的分类
(抽象基类) | 字节流 | 字符流 |
---|---|---|
输入流 | InputStream | Reader |
输出流 | OutputStream | Writer |
2.FileReader
文件输入流
import org.junit.Test;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;public class FileReadWriteTest { @Test public void filereadertest() throws IOException { //1.实例化file类的对象 File file = new File("hello.txt"); //2.提供具体的流 FileReader reader = new FileReader(file); //3.数据的读入 int i = reader.read(); while (i!=-1){ System.out.print((char) i); i = reader.read(); } reader.close(); } @Test public void filereadertest1() { FileReader fileReader = null; try { //file类的实例化 File file = new File("hello.txt"); //filereader流的实例化 fileReader = new FileReader(file); //读入操作 char[] chars = new char[5]; int len; while ((len=fileReader.read(chars))!=-1){// 方式一// for (int i = 0; i < len; i++) {// System.out.print(chars[i]);// } // 方式二 String str = new String(chars,0,len); System.out.print(str); } } catch (IOException e) { throw new RuntimeException(e); }finally { if (fileReader!=null){ try { fileReader.close(); } catch (IOException e) { throw new RuntimeException(e); } } } }}
3.FileWriter
输出操作,对应的File可以不存在的。如果不存在,再输出的时候自动创建。如果存在,如果流使用的构造器是:FileWriter(file,false)或者FileWriter(file),是对原有文件的覆盖,如果是FileWriter(file,true)则不会对其进行覆盖,在其内容后添加
@Testpublic void filewritertest() throws IOException { //1.实例化file类的对象 File file = new File("hello.txt"); //2.filewriter流的实例化 FileWriter writer = new FileWriter(file,true); //3.写出的操作 writer.write("混的人\n"); writer.write("是不是打了\n"); //4.流的关闭 writer.close();}
练习(实现文本的复制)
@Testpublic void filereaderwritertest(){ FileReader fileReader = null; FileWriter fileWriter = null; try { //1.实例化file类的对象 File in = new File("hello.txt"); File out = new File("yyh.txt"); //2.创建输入流和输出流的对象 fileReader = new FileReader(in); fileWriter = new FileWriter(out); //3.数据的读入和写出操作 char[] chars = new char[5]; int len; while ((len = fileReader.read(chars))!=-1){ fileWriter.write(chars,0,len); } } catch (IOException e) { throw new RuntimeException(e); }finally { //4.关闭流资源 if (fileReader!=null&&fileWriter!=null){ try { fileReader.close(); fileWriter.close(); } catch (IOException e) { throw new RuntimeException(e); } } }}
图片不能实现复制
4.InputStream
使用字节流处理文本文件可能会出现乱码
@Testpublic void inputstreamfile() throws IOException { File file = new File("yyh.txt"); FileInputStream inputStream = new FileInputStream(file); byte[] bytes = new byte[10]; int len;//每次读取的字节的个数 while ((len = inputStream.read(bytes))!=-1){ String str = new String(bytes,0,len); System.out.print(str); } inputStream.close();}
练习(对图片进行复制)
/** * 对图片的复制操作 */@Testpublic void inputstreamfile1(){ FileInputStream inputStream = null; FileOutputStream outputStream = null; try { File in = new File("微信.jpg"); File out = new File("头像.jpg"); inputStream = new FileInputStream(in); outputStream = new FileOutputStream(out); byte[] bytes = new byte[5]; int len; while ((len=inputStream.read(bytes))!=-1){ outputStream.write(bytes,0,len); } } catch (IOException e) { throw new RuntimeException(e); }finally { if (inputStream!=null){ try { inputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } else if (outputStream!=null) { try { outputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } }}
import org.junit.Test;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class Copy { public void copyfile(String srcpath , String destpath){ FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { File in = new File(srcpath); File out = new File(destpath); fileInputStream = new FileInputStream(in); fileOutputStream = new FileOutputStream(out); byte[] bytes = new byte[5]; int len; while ((len=fileInputStream.read(bytes))!=-1) { fileOutputStream.write(bytes,0,len); } } catch (IOException e) { throw new RuntimeException(e); }finally { if (fileInputStream!=null&&fileOutputStream!=null){ try { fileInputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } try { fileOutputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } } } @Test public void test01(){ long start = System.currentTimeMillis(); String srcpath="C:\\Users\\dell\\Desktop\\微信.jpg"; String destpath="C:\\Users\\dell\\Desktop\\hunao5.jpg"; copyfile(srcpath,destpath); long end = System.currentTimeMillis(); System.out.println("复制的时间为:"+(end-start)); }}
5.BufferedInputStream和BufferedOutputStream的使用
读写速度比inputstrean和outputstream更快
import org.junit.Test;import java.io.*;public class Bufferedtest { /** * 缓冲流的使用 * bufferedinputstream * bufferedoutputstream * bufferedreader * bufferedwriter * */ @Test public void bufferedstream() throws FileNotFoundException { FileInputStream inputStream = null; FileOutputStream outputStream = null; BufferedInputStream bufferedInputStream = null; BufferedOutputStream bufferedOutputStream = null; try { //1.造文件 File in = new File("2fd5dcc1a7f86d24834628d8b4715fd.jpg"); File out = new File("yyh.jpg"); //造流 inputStream = new FileInputStream(in); outputStream = new FileOutputStream(out); bufferedInputStream = new BufferedInputStream(inputStream); bufferedOutputStream = new BufferedOutputStream(outputStream); //读取和写入的过程 byte[] bytes = new byte[1024]; int len; while ((len=bufferedInputStream.read(bytes))!=-1){ bufferedOutputStream.write(bytes,0,len); } } catch (IOException e) { throw new RuntimeException(e); }finally { //资源关闭(先关闭外层的流) //关闭外层流的同时,内层流也会自动关闭 if (bufferedInputStream!=null){ try { bufferedInputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } if (bufferedOutputStream!=null){ try { bufferedOutputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } } }}
6.BufferedReader和BufferedWriter的使用
读写速度比reader和writer更快
import org.junit.Test;import java.io.*;public class Bufferedwriterreadertest { @Test public void bufferedwriterreader(){ BufferedReader bufferedReader = null; BufferedWriter bufferedWriter = null; try { File in = new File("test.txt"); File out = new File("jxy.txt"); FileReader reader = new FileReader(in); FileWriter writer = new FileWriter(out); bufferedReader = new BufferedReader(reader); bufferedWriter = new BufferedWriter(writer); char[] chars = new char[1024]; int len; while ((len=bufferedReader.read(chars))!=-1){ bufferedWriter.write(chars,0,len); } } catch (IOException e) { throw new RuntimeException(e); }finally { if (bufferedReader!=null){ try { bufferedReader.close(); } catch (IOException e) { throw new RuntimeException(e); } } if (bufferedWriter!=null){ try { bufferedWriter.close(); } catch (IOException e) { throw new RuntimeException(e); } } } }}
7.转换流
提供了字节流和字符流之间的转换
javaAPI提供了两个转换流:(都属于字符流)
InputStreamReader:将InputStream转换为Reader
OutputStreamWrite:将Write转换为Outputstream
7.1InputStreamReader的使用
实现字节的输入流到字符的输入流的转换
import org.junit.Test;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;public class Inputstreamreadertest { @Test public void inputstreamreader(){ InputStreamReader reader = null; try { FileInputStream inputStream = new FileInputStream("test.txt"); reader = new InputStreamReader(inputStream); char[] chars = new char[1024]; int len; while ((len=reader.read(chars))!=-1){ String str = new String(chars,0,len); System.out.println(str); } } catch (IOException e) { throw new RuntimeException(e); }finally { try { reader.close(); } catch (IOException e) { throw new RuntimeException(e); } } }}
7.2InputStreamReader和OutputStreamWrite的使用
@Testpublic void test01(){ InputStreamReader reader = null; OutputStreamWriter writer = null; try { FileInputStream inputStream = new FileInputStream("test.txt"); FileOutputStream outputStream = new FileOutputStream("wendu666.txt"); reader = new InputStreamReader(inputStream,"UTF-8"); writer = new OutputStreamWriter(outputStream,"UTF-8"); char[] chars = new char[1024]; int len; while ((len=reader.read(chars))!=-1){ writer.write(chars,0,len); } } catch (IOException e) { throw new RuntimeException(e); }finally { if (reader!=null){ try { reader.close(); } catch (IOException e) { throw new RuntimeException(e); } } if (writer!=null){ try { writer.close(); } catch (IOException e) { throw new RuntimeException(e); } } }}
8.字符集
8.1编码表的由来
计算机只能识别二进制数据,早期由来是电信号。为了方便应用计算机,让它可以识别各个国家的文字。就将各个国家的文字用数字来表示,并一一对应,形成一张表,这就是编码表
8.2常见的编码表
-
ASCII码 —ASCII编码表由一个字节表示,128个字符,实际上一个字节可以表示256个字符,但是老外的英文只有那么24个字母,用不了那么多的字符。但是随着计算机的普及,全球都在使用计算机,原本的编码不能够满足全球各国语言,所以出现了Unicode。
-
Unicode—Unicode编码表是固定大小的编码,使用两个字节来表示字符,字母和汉字统一都是占用两个字节,容易造成空间浪费。
-
utf-8—大小可变的编码,字母使用一个字节,汉字使用3个字节。
-
gbk—gbk编码可以表示汉字,而且范围广,字母使用一个字节,汉字2个字节。
-
gb2312—可以表示汉字,最多两个字节编码所有字符