Java知识点总结(JavaIO-转换流)

Java IO转换流详解
本文介绍了Java中转换流的基本概念及使用方法。包括如何通过OutputStreamWriter将字节输出流转换为字符输出流,并演示了具体实例;同时,也介绍了如何利用InputStreamReader实现从字节输入流到字符输入流的转变。

Java知识点总结(JavaIO-转换流)

@(Java知识点总结)[Java, JavaIO]

[toc]

clipboard.png

OutputStreamWriter

是Writer的子类,将一个字符流的输出对象变为字节流的输出对象。

clipboard.png

clipboard.png

InputStreamReader

是Reader的子类,将一个字节流的输入对象变为字符流的输入对象。

clipboard.png

clipboard.png

public  class Demo05 {
 
  // 将字节输出流转为字符输出流
  public static void test1() throws IOException {
    File file = new File("E:" + File.separator + "test.txt");
    
    Writer out = new OutputStreamWriter(new FileOutputStream(file)); // 字节流转为字符流
    
    out.write("苹果");
    out.close();
 
  }
 
  // 将字节输入流转为字符输入流
  public static void test2() throws IOException {
    File file = new File("E:" + File.separator + "test.txt");
    
    Reader input = new InputStreamReader(new FileInputStream(file));
    char[] c = new char[1024];
    int len = input.read(c);
    
    input.close();
    System.out.println("内容是:" +new String(c,0,len));
  }
 
  public static void main(String[] args) {
    try {
       test1();
       test2();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值