javaIO流学习 2019.2.26

本文介绍字节缓冲流BufferedInputStream和BufferedOutputStream的使用,展示如何通过它们提高文件读写效率,尤其在纯文本处理上的应用。同时,文章提供了文件拷贝的具体步骤和代码实现,以及如何利用转换流进行网络数据的下载和保存。

字节缓冲流: BufferedInputStream和BufferOutputStream

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class BuffInput {
    public static void main(String[] args) {
        File src=new File("a.txt");
        InputStream is=null;
        
        try {
            is=new BufferedInputStream(new FileInputStream(src));
            
            byte[] flush=new byte[1024];
            int len=-1;
            while((len=is.read(flush))!=-1) {
                String str=new String(flush,0,len);
                System.out.println(str);
            }
        }  catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(is!=null) {
                try {
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

}
用法就是将FileInputStream嵌套如Buffer中,可以起到提高性能缓冲的作用,但仅限于纯文本

/*文件拷贝步骤+buffer  仅限于纯文本
 * 1.创建源(输入和输出)
 * 2.选择流
 * 3.操作
 * 4.释放资源
 */
public class BufferCopy {
    public static void main(String[] args) {
        copy("a.txt","b.txt");
    }
    public static  void copy(String srcPath,String destPath) {
        //源
                File a=new File(srcPath);
                File b=new File(destPath);
                //输出流和输入流
                
                try (BufferedReader br=new BufferedReader(new FileReader(a));
                        BufferedWriter bw=new BufferedWriter(new FileWriter(b));){        
                    //操作:逐行读取
                String line=null;
                    while((line=br.readLine())!=null) {
                        bw.write(line);
                        bw.newLine();
                    }
                    bw.flush();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    }
}

三个过程  IuputStream->InputStreamReader->BufferInputStream(OutputStream也差不多)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;


/*
 * 转换流:InputStreamReader和OutputStreamWriter
 * 1.以字符流形式操作字节流(纯文本)
 * 2.指定字符集
 */
public class ConvertTest2 {
    public static void main(String[] args) {
        //操作网络流:下载百度的网络流
        try(BufferedReader is=new BufferedReader
                (new InputStreamReader
                        (new URL("http://www.baidu.com").openStream(),"UTF-8"));
                BufferedWriter os=new BufferedWriter
                        (new OutputStreamWriter
                                (new FileOutputStream("baidu.html"),"UTF-8"));){
            String line=null;
            while((line=is.readLine())!=null) {
                //System.out.print(line);
                os.write(line);//字符集不统一乱码
                os.newLine();
            }
        }catch(IOException e) {
            System.out.println("操作异常");
        }
}
    public static void test(String[] args) {
        //操作网络流:下载百度的网络流
        try(InputStream is=new URL("http://www.baidu.com").openStream();){
            int temp;
            while((temp=is.read())!=-1)
                System.out.print((char)temp);//字节数不够乱码
        }catch(IOException e) {
            System.out.println("操作异常");
        }
        
}
    public static void test2(String[] args) {
    //操作网络流:下载百度的网络流
            try(InputStreamReader is=new InputStreamReader(new URL("http://www.baidu.com").openStream(),"UTF-8");){
                int temp;
                while((temp=is.read())!=-1)
                    System.out.print((char)temp);
            }catch(IOException e) {
                System.out.println("操作异常");
            }
    }    
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值