2021-09-28 JAVA字节流和字符流以及桥的搭建

public class Demo {
    public static void main(String[] args) throws Exception {
               //缓存区字节流
              FileInputStream ji=null;  //字节流输入
              FileOutputStream jo=null; //字节流输出
              BufferedInputStream si=null;  //缓冲区
              BufferedOutputStream so=null;
              try {
                  ji=new FileInputStream("F:\\sss.txt");
                  jo=new FileOutputStream("F:\\xxx.txt");
                  si=new BufferedInputStream(ji);
                  so=new BufferedOutputStream(so);
                  byte[] b=new byte[1024];  //一次读取1kb数据

                  //字节流读取数据
                  int result=0;
                  try {
                      while ((result=si.read(b))!=-1);
                      System.out.println(result);
                      so.write(b,0,result);
                  } catch (IOException e) {
                      e.printStackTrace();
                  }

              } catch (Exception e) {
                  e.printStackTrace();
              }finally {
                  try {
                      si.close();
                      so.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }


    }
    }

public class Demo2 {
    public static void main(String[] args) {
        //缓冲区字符流
        BufferedReader jr=null;
        BufferedWriter jw=null;

        try {
            //输入
            jr=new BufferedReader(new FileReader(new File("F:\\字符流.txt")));
            try {
                //输出
                jw=new BufferedWriter(new FileWriter(new File("F:\\zifuliu.txt")));
                String sc=null;
                while ((sc=jr.readLine())!=null){
                    jw.write(sc); //写入
                    jw.newLine(); //换行
                }
                jw.flush();   //刷新缓冲区
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                jr.close();
                jw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

public class Demo3 {
    public static void main(String[] args) {
        //字节流输出
        FileInputStream ri=null;//输入
        FileOutputStream ro=null;//输出

        try {
            ri=new FileInputStream("F:\\sss.txt");
            try {
                ro=new FileOutputStream("F:\\aaa.txt");
                byte[] tr=new byte[1024];//一次1kb
                //读取数据
                int te=0;
                ri.read(tr);
                ro.write(tr);
                int tw=0;
                while ((te=ri.read(tr))!=-1){
                    System.out.println(te);
                    ro.write(tr,0,te);
                    tw++;

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        finally {
            try {
                ri.close();
                ro.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

public class Demo4 {
    public static void main(String[] args) {
        BufferedReader br =null;//输入
        BufferedWriter bw=null;//输出
        br=new BufferedReader(new InputStreamReader(System.in));
        try {
            bw=new BufferedWriter(new FileWriter(new File("F:\\wer.txt")));
            //读取数据
            String team=null;
            while ((team=br.readLine())!=null){
                if (team.equals("end!")){
                    break;
                }
                bw.write(team);
                bw.newLine();

            }
            bw.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                br.close();
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值