字节流,字节缓冲流,字节流转化为字符流的输入与输出

本文展示了如何使用Java进行字节流、字节缓冲流的读写操作,以及如何将字节流转换为字符流。通过实例代码,详细解释了文件的读取、写入、缓冲区应用以及字节流到字符流的转换过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 private static String path="D:\\work_idea\\Idea_workspace\\FileTextWorkSpace\\HelloFile.txt";
    public static void main(String[] args) {
    // InputFIle();
//     OutputFile();
//        BufferInputFile();
//        BufferOutputFile();
        byteFlowToStringFlowAboutInput();
//        byteFlowToStringFlowAboutOutput();
    }
    //字节流读取(input)
    /*
    * 1:创建一个读取文件的File对象
    * 2:对File对象创建一个读入流
    * 3:创建一个byte类型的数组存储文本内的内容,长度为文本的长度大小
    * 4:读取文件内的信息
    * */
      static   void  InputFIle(){
        File file = new File("D:\\work_idea\\Idea_workspace\\FileTextWorkSpace\\HelloFile.txt");
        try {
            InputStream input=new FileInputStream(file);
            //创建byte类型的数组
            byte[] b =new byte[(int)file.length()];
            //读取文本内容信息并存入数组中
            input.read(b);
            input.close();
            //将其转换成字符串
            System.out.println(new String(b));
            
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
    
    //字节流写入(outPut)
    static  void OutputFile(){
          /*
          * 1:创建一个File对象
          * 2;为这个File对象创建一个输出流
          * 3;从键盘输入需要向File对象输入的信息
          * 4:创建byte【】数组将输入的信息转化为byte类型
          * 5:输入信息
          * */
        File file = new File("D:\\work_idea\\Idea_workspace\\FileTextWorkSpace\\HelloFile.txt");
        //创建输入流
        try {
            //定义append 方法为ture ,允许追加,而不是覆盖
            OutputStream output= new FileOutputStream(file,true);
            //从键盘输入
            //创建Scanner对象
            Scanner scanner=new Scanner(System.in);
            //获取键盘输入的信息
            System.out.println("请输入一个任意长度的字符串");
            String str=scanner.nextLine();
            //创建byte类型的数组,并将String类型的数据转化为字节类型
            byte [] bytes=str.getBytes();
            //输出
            try {
                output.write(bytes);
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        
        
        
    }
    
    
    //缓存区的应用
    static void BufferInputFile(){
          /*
          * 1:创建File对象
          * 2:创建input输入流File对象
          * 3:定义缓存区
          * 4:将读取的信息存储在缓存区中
          * 5:从缓存区读取数据
          * */
        File file =new File("D:\\work_idea\\Idea_workspace\\FileTextWorkSpace\\HelloFile.txt");
        //定义输入流
        try {
            InputStream stream=new FileInputStream(file);
            BufferedInputStream bufferedInputStream=new BufferedInputStream(stream);
            byte[] bytes=new byte[(int)file.length()];
            bufferedInputStream.read(bytes);
            System.out.println(Arrays.toString(bytes));
            System.out.println(new String(bytes));
            bufferedInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    }
    static  void BufferOutputFile(){
          /*
          * 1:定义一个File类的对象
          * 2:为File对象创建一个输出流
          * 3:为输出流创建一个缓存流
          * 4;定义一个Scanner对象,从键盘获取信息
          * 5:String类型的字符串转化为byte类型
          * 6:将获取到的信息写入到缓存流中
          * 7;输出
          * */
        //1
        File file=new File("D:\\work_idea\\Idea_workspace\\FileTextWorkSpace\\HelloFile.txt");
        //2
        try {
            OutputStream outputStream=new FileOutputStream(file,true);
            //3
            BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(outputStream);
            //4
            Scanner scanner=new Scanner(System.in);
            System.out.println("请输入一个字符串");
            String str=scanner.nextLine();
            byte[] bytes=str.getBytes();
            bufferedOutputStream.write(bytes);
            bufferedOutputStream.close();
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //字节流转化为字符流  即转换流
    static  void byteFlowToStringFlowAboutInput(){
          //创建File对象
        File file=new File(path);
        //为File对象创建一个输入流
        try {
            InputStream inputStream=new FileInputStream(file);
            InputStreamReader inputStreamReader=new InputStreamReader(inputStream,"utf-8");
           //定义变量保存字符
            int reads;
            while ((reads=inputStreamReader.read())!=-1){
                System.out.print((char) reads);
            }
            inputStreamReader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    static void byteFlowToStringFlowAboutOutput() {
          //定义FIle
        File file=new File(path);
        //输出流
        try {
            OutputStream outputStream=new FileOutputStream(file,true);
            OutputStreamWriter outputStreamWriter=new OutputStreamWriter(outputStream,"utf-8");
            //定义scanner
            Scanner scanner=new Scanner(System.in);
            System.out.println("请输入一个字符串");
            String str= scanner.nextLine();
            //输出
            outputStreamWriter.write(str);
            outputStreamWriter.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值