Java——转换流,缓冲流

转换流

之前的FileOutputStream/FileInputStream保存的文件格式为当前系统的编码格式,如果我们想转换成其他的格式就要使用转换流。

一、输出转换流

OutputStreamWriter类

继承于Writer类,接受的是字符数组、字符串、int型,转换成不同形式编码格式的字节(字符==>不同格式的字节),然后存入文件中。

使用格式:

OutputStreamWriter(OutputStream out, String charsetName)

/*
 * 转换流对象OutputStreamWriter写文本
 * 采用UTF-8编码表写入
 */
public static void writeUTF()throws IOException{
	//创建字节输出流,绑定文件
	FileOutputStream fos = new FileOutputStream("c:\utf.txt");
	//创建转换流对象,构造方法保装字节输出流,并指定编码表是UTF-8
	OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
	osw.write("你好");  //可以接受字符数组,字符串,int型数据
	osw.close();
}

二、输入转换流

InputStreamReader类

继承于Reader类,接受的是不同形式编码格式的字节,根据不同的编码表转换成字符格式(不同格式的字节==>字符)

使用格式:

InputStreamReader(InputStream in,String charsetName)

/*
 *  转换流,InputSteamReader读取文本
 *  采用UTF-8编码表,读取文件utf
 */
public static void main(String[] args) throws IOException {
    //创建自己输入流,传递文本文件
    FileInputStream fos = new FileInputStream("/home/x54256/桌面/1.txt");
    //创建转换流对象,构造方法中,包装字节输入流,同时写编码表名
    InputStreamReader isr = new InputStreamReader(fos,"utf-8");
    int len = 0;
    char[] ch = new char[1024];
    while ((len = isr.read(ch))!=-1){
        //System.out.print((char)len);
        System.out.print(new String(ch,0,len));
    }
}

缓冲流

之前的读取文件中数据的操作,当读取数据量大的文件时,读取的速度会很慢,很影响我们程序的效率,为了提高速度,Java中提高了一套缓冲流,它的存在,可提高IO流的读写速度

主要是缓冲一部分数据,然后在调用底层系统(Windows/Linux)进行IO操作

一、字节缓冲输出流

使用方法:

BufferedInputStream(InputStream in)

可以传递任意的字节输入流,传递是谁,就提高谁的效率。

public static void main(String[] args) throws IOException {
    //创建字节输出流
    FileOutputStream fos = new FileOutputStream("/home/x54256/桌面/1.txt");
    //创建字节输出流缓冲流的对象
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bos.write(79);
    bos.write("你好,世界!".getBytes());
    bos.write("你好,世界!".getBytes(),3,3);
    //bos.flush();
    bos.close();
}

二、字节缓冲输入流

使用方法:

BufferedInputStream(InputStream in)

可以传递任意的字节输入流,传递是谁,就提高谁的效率。

public static void main(String[] args) throws IOException {
    //创建输入流对象
    FileInputStream fis = new FileInputStream("/home/x54256/桌面/1.txt");
    //创建字节输入流的缓冲流对象
    BufferedInputStream bis = new BufferedInputStream(fis);

    byte[] b = new byte[10];
    int len = 0;
    while((len = bis.read(b))!=-1){
        System.out.println(new String(b,0,len));
    }
    bis.close();
}

三、字符缓冲输出流

使用方法:

BufferedWriter(Writer w)

可以传递任意的字符输出流,传递是谁,就提高谁的效率。

public static void main(String[] args) throws IOException {
    FileWriter fw = new FileWriter("/home/x54256/桌面/1.txt");
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(99);
    bw.newLine();   //新行,BufferedWriter的特有的方法,具有平台无关性
    bw.flush();     //字符流要刷进去

    bw.write("你好,世界!".toCharArray());
    bw.newLine();
    bw.flush();

    bw.write("你好,世界!");
    bw.newLine();
    bw.flush();

    bw.close();
    
}

四、字符缓冲输出流

使用方法:

BufferedWriter(Writer w)

可以传递任意的字符输入流,传递是谁,就提高谁的效率。

public static void main(String[] args) throws IOException {
    FileReader fr = new FileReader("/home/x54256/桌面/1.txt");
    BufferedReader br = new BufferedReader(fr);
    //int len = 0;
    //char[] c = new char[10];
    String line = null;
    //BufferedReader的特有方法readline
    while((line=br.readLine())!=null){
        System.out.println(line);
    }

//    while ( (len = br.read(c))!=-1 ){
//        System.out.print(new String(c,0,len));
//    }
//    while ( (len = br.read())!=-1 ){
//        System.out.print((char)len);
//    }
    br.close();
}

Copy文件

 1 import java.io.BufferedInputStream;
 2 import java.io.BufferedOutputStream;
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 
 8 /*
 9  *  文件复制方式,字节流,一共4个方式
10  *  1. 字节流读写单个字节                    125250 毫秒
11  *  2. 字节流读写字节数组                    193    毫秒  OK
12  *  3. 字节流缓冲区流读写单个字节     1210   毫秒
13  *  4. 字节流缓冲区流读写字节数组     73     毫秒  OK
14  */
15 public class Copy {
16     public static void main(String[] args)throws IOException {
17         long s = System.currentTimeMillis();
18         copy_4(new File("c:\q.exe"), new File("d:\q.exe"));
19         long e = System.currentTimeMillis();
20         System.out.println(e-s);
21     }
22     /*
23      * 方法,实现文件复制
24      *  4. 字节流缓冲区流读写字节数组
25      */
26     public static void copy_4(File src,File desc)throws IOException{
27         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
28         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
29         int len = 0 ;
30         byte[] bytes = new byte[1024];
31         while((len = bis.read(bytes))!=-1){
32             bos.write(bytes,0,len);
33         }
34         bos.close();
35         bis.close();
36     }
37     /*
38      * 方法,实现文件复制
39      *  3. 字节流缓冲区流读写单个字节
40      */
41     public static void copy_3(File src,File desc)throws IOException{
42         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
43         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
44         int len = 0 ;
45         while((len = bis.read())!=-1){
46             bos.write(len);
47         }
48         bos.close();
49         bis.close();
50     }
51     
52     /*
53      * 方法,实现文件复制
54      *  2. 字节流读写字节数组
55      */
56     public static void copy_2(File src,File desc)throws IOException{
57         FileInputStream fis = new FileInputStream(src);
58         FileOutputStream fos = new FileOutputStream(desc);
59         int len = 0 ;
60         byte[] bytes = new byte[1024];
61         while((len = fis.read(bytes))!=-1){
62             fos.write(bytes,0,len);
63         }
64         fos.close();
65         fis.close();
66     }
67     
68     /*
69      * 方法,实现文件复制
70      *  1. 字节流读写单个字节
71      */
72     public static void copy_1(File src,File desc)throws IOException{
73         FileInputStream fis = new FileInputStream(src);
74         FileOutputStream fos = new FileOutputStream(desc);
75         int len = 0 ;
76         while((len = fis.read())!=-1){
77             fos.write(len);
78         }
79         fos.close();
80         fis.close();
81     }
82 }
字节类竞速
 1 import java.io.BufferedReader;
 2 import java.io.BufferedWriter;
 3 import java.io.FileReader;
 4 import java.io.FileWriter;
 5 import java.io.IOException;
 6 
 7 /*
 8  *  使用缓冲区流对象,复制文本文件
 9  *  数据源  BufferedReader+FileReader 读取
10  *  数据目的 BufferedWriter+FileWriter 写入
11  *  读取文本行, 读一行,写一行,写换行
12  */
13 public class Copy_1 {
14     public static void main(String[] args) throws IOException{
15         BufferedReader bfr = new BufferedReader(new FileReader("c:\w.log"));    
16         BufferedWriter bfw = new BufferedWriter(new FileWriter("d:\w.log"));
17         //读取文本行, 读一行,写一行,写换行
18         String line = null;
19         while((line = bfr.readLine())!=null){
20             bfw.write(line);
21             bfw.newLine();
22             bfw.flush();
23         }
24         bfw.close();
25         bfr.close();
26     }
27 }
字符缓冲区复制
原文地址:https://www.cnblogs.com/x54256/p/8438597.html