IO流

IO流具体就是大家对各种基本流以及装饰流的组合,这里给大家配几种经典组合:

1  不管是字节流还是字符流都用上缓冲流性能会好也就是带有"Buffered"字样的流,这里用的是BufferedReader嵌套FileReader:

public class FirstCombination {

    public static void main(String[] args) throws FileNotFoundException {
        File f=new File("E:/hello.txt");
        BufferedReader br=null;
        if(f.exists()){
            try {
                br=new BufferedReader(new FileReader(f));
                String s=null;
                LinkedList<String> lists=new LinkedList<>();
                    while((s=br.readLine())!=null){
                        lists.add(s);
                    }
                ListIterator<String>  listIterator=lists.listIterator();
                while(listIterator.hasNext()){
                    listIterator.next();
                }
                while(listIterator.hasPrevious()){
                    System.out.println(listIterator.previous().toUpperCase());
                }
                
            }  catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                if(br!=null){
                    try {
                        br.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    

}

2 这是Android中的一段代码,注意读取与写入时的操作很经典

private void initAddressDB(String DBname){
        InputStream in=null;
        FileOutputStream fos=null;
        File files=getFilesDir();
        File file=new File(files,DBname);
        if(file.exists()){
            return;
        }
        try {
            in=getAssets().open(DBname);
            fos=new FileOutputStream(file);
            byte[] bytes=new byte[1024];
            int temp=-1;
            while((temp=in.read(bytes))!=-1){
                fos.write(bytes, 0, temp);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(in!=null)
                    in.close();
                if(fos!=null)
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

3 看注释即可

public static void main(String[] args) {
        try {
            BufferedReader br=new BufferedReader(new FileReader(new File("E:/hello.txt")));
//            传统的装饰工作,略显冗余  用缓存流会显著提高效率  
//            PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter("E:/out.txt")));
//            只有PrintWriter有这种快捷操作哦  与第一种效率一样的
            PrintWriter pw=new PrintWriter(new File("E:/1out.txt"));
            String s=null;
            while((s=br.readLine())!=null){
                pw.println(s);
            }
            pw.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
原文地址:https://www.cnblogs.com/zzl521/p/8885119.html