javaSE(15)(缓冲流、转换流和标准系统I0流)

字节缓冲流:
package zz.itheima.bufferdstream;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class TestBufferedInputStream {

    public static void main(String[] args) {
        //测试字节缓冲流
        /*java.io.BufferedInputStream 字节缓冲流(读) public BufferedInputStream(InputStream in) 默认缓冲区是8k public BufferedInputStream(InputStream out, int size)可以自己指定缓冲区大小,单位字节 public int read() public int read(byte[] b) public void close()*/
        //架管道
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\2.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

}
字节缓冲流(写):
package zz.itheima.bufferdstream;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class TestBufferedOutputStream {

    public static void main(String[] args) {
        //测试字节缓冲林(写)
        /*java.io.BufferedOutputStream 字节缓冲流(写) * public BufferedOutputStream(OutputStream out) 默认大小是8k public BufferedOutputStream(OutputStream out, int size)可以自己指定缓冲区大小,单位字节 public void write(int b) 参数是ASCII表中的码值,不是普通数字 public void write(byte[] b) public void flush() public void close() */
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream("E:\\2.txt"), 1024*16);
            for (int i = 0; i <1024*16 + 1; i++) {
                bos.write(97);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
字符缓冲流1package zz.itheima.bufferdstream;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class TestBufferedWriter {

    public static void main(String[] args) {
        //测试字符缓冲流
        //架管道
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter("E:\\2.txt"));
            bw.write("hel\r\nlo");
            bw.write("world");
            bw.flush();

            br = new BufferedReader(new FileReader("E:\\2.txt"));
            String val = br.readLine();
            System.out.println(val);
            String val1 = br.readLine();
            System.out.println(val1);
            String val2 = br.readLine();
            System.out.println(val2);
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /** * 测试结果: * hel loworld null * * */

}
字符缓冲流2package zz.itheima.bufferdstream;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class TestBufferedWriter2 {

    public static void main(String[] args) {
        //测试字符缓冲流
        BufferedWriter bw = null;
        try {
            int size = 1024*2;
            bw = new BufferedWriter(new FileWriter("E:\\demo.txt"),size);
            char[] temp = new char[size];
            for (int i = 0; i < size; i++) {
                temp[i] = 'a';
            }
            bw.write(temp);
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                bw.flush();
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

}
使用字节缓冲流(拷贝一个视频文件):
package zz.itheima.bufferdstream;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class BufferedCopy {
    public static void copy(){
        long begin = System.currentTimeMillis();
        //使用字节缓冲流(拷贝一个视频文件)
                BufferedInputStream bis = null;
                BufferedOutputStream bos = null;
                try {
                    bis = new BufferedInputStream(new FileInputStream("E:\\1.avi"));
                    bos = new BufferedOutputStream(new FileOutputStream("D:\\1.avi"));
                    byte[] temp = new byte[1024*1024*2];
                    while (true) {
                        int res = bis.read(temp);
                        if (res==-1) {
                            break;
                        }else {
                            bos.write(temp, 0, res);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally{
                    try {
                        bos.close();
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
        long end = System.currentTimeMillis();
        System.out.println(end - begin);

    }
    public static void main(String[] args) {
        copy();
    }

}
测试字符编码:
package zz.itheima.bufferdstream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class TestEncoding {

    public static void main(String[] args) throws Exception {
        //测试utf-8编码的文本文件的读写
                //在工程中新建一个文本文件
                //点右键通过属性改为utf-8
                //录入几个汉字
        //FileReader默认采用GBK编码,因此对于utf-8格式的文本会出现乱码
        FileReader fr = null;
        File f = null;
        try {
             f = new File("demo.txt");
             fr = new FileReader(f);
            char[] temp = new char[(char)f.length()];
            fr.read(temp);
            System.out.println(new String(temp));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally{
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //InputStreamReader可以自己设置编码,格式要与文本文件的编码格式一致即可,不会出现乱码
        InputStreamReader isr = null;
        try {
            isr = new InputStreamReader(new FileInputStream(f),"utf-8");
            char[] temp = new char[(int)f.length()];
            isr.read(temp);
            System.out.println(new String(temp));
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            isr.close();
        }
        //FileWriter类默认使用GBK进行编码,如果文件是utf-8格式的,就会出现乱码
                FileWriter fw=new FileWriter(f);
                fw.write("张三");
                fw.close();

                //OutputStreamWriter类可以自己指定编码,需要跟文件的编码保持一致,就不会出现乱码
                OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream(f),"utf-8");
                osw.write("张三");
                osw.close();

    }

}
对字符串进行转码:
package zz.itheima.bufferdstream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;

public class TestStringEncoding {

    public static void main(String[] args) throws Exception{
        //对某个字符串进行转码
        //使用基本字节输入流读取utf-8格式的文本
        File f = new File("demo.txt");
        FileInputStream fis = new FileInputStream(f);
        byte[] temp = new byte[(int)f.length()];
        fis.read(temp);
        System.out.println(new String(temp,"utf-8"));
        fis.close();
        //使用基本字符输入流读取utf-8格式的文本
        File f1=new File("demo.txt");
        FileReader fr=new FileReader(f1);
        char[] t=new char[(int)f.length()];
        fr.read(t);
        String val=new String(t);
        System.out.println(new String(val.getBytes(),"utf-8"));
        fr.close();
        //如果得到的字符串乱码怎么处理
        String s = "美好!";
        System.out.println(new String(s.getBytes(),"utf-8"));
    }

}
系统标准的输入输出流:
package zz.itheima.bufferdstream;

import java.io.InputStream;
import java.io.PrintStream;

public class TestSystem {
    //系统标准的输入输出流
    public static void main(String[] args) throws Exception{
        // System.in System.out,
        InputStream is = System.in;
        int val = is.read();
        PrintStream ps = System.out;
        ps.write(val);
        ps.flush();
        ps.close();

    }

}
例子一:
package zz.itheima.bufferdstream;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;


public class Demo1 {

    public static void main(String[] args) throws Exception{
        /*用户在控制台循环录入姓名和年龄,然后存到一个文本文件中,一人占一行, 直到quit退出,然后把文件中的每一行都输出到控制台(字符缓冲流特有方法)*/
        BufferedReader br = new BufferedReader(new FileReader("E:\\2.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\2.txt"));
        String flag ;
        do {

            Scanner sc = new Scanner(System.in);
            System.err.println("请输入姓名");
            String name = sc.nextLine();
            System.out.println("请输入年龄:");
            String  age = sc.nextLine();
            bw.write(name +"\t" +age);
            bw.newLine();
            System.out.println("是否继续?");
            flag = sc.next();
        } while (flag.equalsIgnoreCase("y"));
        bw.close();
        while (true) {
            String res = br.readLine();
            if (res==null) {
                break;
            }else {
                bw.write(res);
            }

        }
    }

}
例子二:
package zz.itheima.bufferdstream;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class Demo2 {

    public static void main(String[] args)throws Exception {
        //练习:读取一个Java源文件,输出代码有多少行?然后把该文件的代码写入到另一个文本文件中
        BufferedReader br = null;
        BufferedWriter bw = null;
        int count = 0;
        try {
            br = new BufferedReader(new FileReader("D:\\1.java"));
            bw = new BufferedWriter(new FileWriter("E:\\2.txt"));
            while (true) {
                String val  = br.readLine();
                if (val==null) {
                    break;
                }else {
                    count++;
                    bw.write(val);
                    bw.newLine();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            bw.close();
            bw.close();
        }
        System.out.println(count);

    }

}

你可能感兴趣的:(java,se,缓冲流-转换流)