java 节点流(字符流,字节流)和包装流(缓冲流,转换流)

结点流:直接对File类进行操作的文件流

package stream;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.junit.jupiter.api.Test;



/*
 *     流的体系结构:     抽象基类             节点流(或文件流)            缓冲流(处理流的一种)
 *     字节输入流            InputStream        FileInputStream        BufferedInputStream
 *     字节输出流             OutputStream    FileOutputStream     BufferedOutputStream
 *     字符输入流           Reader            FileReader            BufferedReader
 *     字符输出流             Writer            FileWriter            BufferedWriter
 * 
 *     字符流只能处理字符,字节流能处理图片,二进制文件
 * */
public class FileReaderWriterTest {
    
    @Test
    public void test() throws IOException {
        //1.实例化File类的对象
        //2.提供具体的流
        FileReader fr = null;
        try {
            File file = new File("hello .txt");
            System.out.println(file.getAbsolutePath());
            
            File file1 = new File("C:\\Users\\ASUS\\Desktop\\JAVAEE\\practice\\IO_FIle\\hello.txt");
            System.out.println(file1.getAbsolutePath());
            
            fr = new FileReader(file);
            
            //3.数据的读入:
            //read()方法:return一个读入的字符,如果读到结尾则输出-1
            int data;
            while((data = fr.read())!=-1)
                System.out.println((char)data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            //4.流的关闭操作
            try {
                if(fr != null)
                    fr.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    //对read()操作升级:使用read的重载方法
    @Test
    public void testFileReader1() {
        //2.FileReader流的实例化
        FileReader fr = null;
        try {
            //1.File
            File file = new File("hello.txt");
            fr = new FileReader(file);
            //3.读入的操作
            //read(buf):返回每次读入buf的字符的个数,如果达到文件尾,返回-1
            char [] buf = new char[5];
            int len;
            while((len = fr.read(buf)) != -1) {
                String s = new String(buf,0,len);
                System.out.println(s);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            try {
                //4.资源的关闭
                if(fr!=null)
                    fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    /*
     *     输出操作:对应的File可以不存在的
     *         如果不存在,在输出的过程中会自动创建此文件
     *         如果存在,则会覆盖此文件
     *             但是可以增加第二个参数 true 进行追加
     */
    @Test
    public void testFileWriter() {
        FileWriter fw = null;
        try {
            //1.提出File类的对象,指明写出到的文件
            File file = new File("hello1.txt");
            //2.提供FileWriter的对象,用于数据的写出
            fw = new FileWriter(file);
            //3.写出的操作
            fw.write("i have a dream.\n");
            fw.write("you have a dream too");
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            //4.关闭流
            try {
                if(fw != null)
                    fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    /*
     *      进行文件复制
     * */
    @Test
    public void testFileReaderFileWriter() {
        FileReader fr = null;
        FileWriter fw = null;
        try {
            File sfile = new File("hello.txt");
            File ttfile = new File("hello2.txt");
            
            fr = new FileReader(sfile);
            fw = new FileWriter(ttfile);
            
            char [] buf = new char[5];
            int len;
            while((len = fr.read(buf))!=-1) {
                fw.write(buf,0,len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            //4.关闭资源 
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

包装流:用来修饰节点流

缓冲流加速

package stream;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.junit.jupiter.api.Test;

/*
 * 1.缓冲流
 * BufferedInputStream
 * BufferedOutputStream
 * BufferedReader
 * BufferedWriter
 * 
 * 2.作用:提高流的读取写入速度
 * 
 * */

public class BufferedTest {
    
    /*
     *     实现非文本文件的赋值
     * */
    @Test
    public void BufferedStreamTest(){
        BufferedInputStream brs = null;
        BufferedOutputStream bos = null;
        try {
            File sfile = new File("zsben.jpg");
            File tfile = new File("zsben3.jpg");
            
            FileInputStream fis = new FileInputStream(sfile);
            FileOutputStream fos = new FileOutputStream(tfile);

            brs = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            
            byte[] buffer = new byte[10];
            int len;
            while((len = brs.read(buffer))!=-1) {
                bos.write(buffer,0,len);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            try {
                //关闭外层流的同时, 内层流也会自动被关闭
                brs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                bos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
    }
}
  

转换流进行编码和解码

package stream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.junit.jupiter.api.Test;

/*
 *     处理流之二:转换流 
 *         InputStreamWriter:字节输入流->字符输入流  
 *         OutputStreamWriter:字符输出流->字节输出流
 *         字节->字符 (97->'a'):即一个解码过程
 *         字符->字节 ('a'->97):即一个编码过程
 * */
 
public class InputStreamReaderITest {
    @Test
    public void test1() throws IOException {
        //第二个参数:file保存时使用的编码方式
        InputStreamReader isr= null;
        try {
            File file = new File("hello.txt");
            FileInputStream fis = new FileInputStream(file);
            
            isr = new InputStreamReader(fis,"gbk");//原来使用gbk编码存的,这儿换成UTF-8就会使中文字符乱码
            
            char [] buf = new char[20];
            int len;
            while((len = isr.read(buf))!=-1) {
                
                String s = new String(buf,0,len);
                System.out.println(s);
                
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        isr.close();
    }
}

猜你喜欢

转载自www.cnblogs.com/zsben991126/p/11874832.html