JavaSE IO

package com.atweihai.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;

import org.junit.Test;


----------
 --原作者:尚硅谷-佟刚  
    IO 体系   (常用)
    InputStream               OutputStream      
    FileInputStream           FileOutputStream    
    BufferedInputStream       BufferedOutputStream 


    ObjectInputStream         ObjectOutputStream


    Reader                    Writer
    FileReader                FileWriter 
    BufferedReader            BufferedWriter

    InputStreamReader          OutputStreamWriter


----------


public class IOTest {
    @Test
    public void testInputStreamReaderAndOutputStreamWriter() throws   IOException{
         //获取项目根路径
         String rootPath=System.getProperty("user.dir");

         //创建指向指定位置的输入流
         InputStream in=new FileInputStream(rootPath+"/src/buffer.txt");
         Reader reader=new InputStreamReader(in);
         BufferedReader bufferedReader=new BufferedReader(reader);

         //创建输出到指定位置的输出流
         OutputStream out=new FileOutputStream(rootPath+"/buffer1.txt");
         Writer   writer =new OutputStreamWriter(out);
         BufferedWriter bufferedWriter=new BufferedWriter(writer);

         String str=null;
         int i=0;
         //循环写入到指定文件
         while((str=bufferedReader.readLine())!=null){
             if(i!=0){
                 bufferedWriter.write("\n");
             }

             bufferedWriter.write(str);
             i++;
         }


         //关闭流资源
         in.close();
         reader.close();
         bufferedReader.close();

         bufferedWriter.close();
         writer.close();
         out.close();
    }


    @Test
    public void testBufferedInputStreamAndBufferedOutputStream() throws IOException{
        //获取项目根路径的两种方式:
        //1.
        String rootPath=new File("").getCanonicalPath();

        //2.
        String rootPath1=System.getProperty("user.dir");

        //创建指向指定文件的输入流
        InputStream in=new FileInputStream(rootPath1+"/src/buffer.txt");
        //创建输出到指定文件的输出流
        OutputStream out=new FileOutputStream(rootPath1+"/buffer1.txt");

        //缓冲流 对输入输出流进行包装
        BufferedInputStream bufferedInputStream=new BufferedInputStream(in);
        BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(out);

        //创建字节缓冲数组
        byte[] buffer=new byte[1024*10];
        int len=0;

        //循环写入到指定文件
        while((len=bufferedInputStream.read(buffer))!=-1){
            bufferedOutputStream.write(buffer, 0, len);
        }
        //关闭缓冲流  缓冲流内部关闭节点流
        bufferedInputStream.close();
        bufferedOutputStream.close();

    }

}

你可能感兴趣的:(JavaSE)