zoukankan      html  css  js  c++  java
  • Java基础知识强化之IO流笔记33:转换流之InputStreamReader的使用

    1. InputStreamReader的使用

    InputStreamReader(InputStream is):用默认的编码读取数据

    InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据

    2. 代码示例:

    (1) InputStreamReader读方法下面两种经常用到两种方法:(继承自父类Reader

    1 public int read()
    1 public int read(char[] cbuf)

    (2)

     1 package cn.itcast_02;
     2 
     3 import java.io.FileInputStream;
     4 import java.io.IOException;
     5 import java.io.InputStreamReader;
     6 
     7 /*
     8  * InputStreamReader(InputStream is):用默认的编码读取数据
     9  * InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据
    10  */
    11 public class InputStreamReaderDemo {
    12     public static void main(String[] args) throws IOException {
    13         // 创建对象
    14         // InputStreamReader isr = new InputStreamReader(new FileInputStream(
    15         // "osw.txt"));
    16 
    17         // InputStreamReader isr = new InputStreamReader(new FileInputStream(
    18         // "osw.txt"), "GBK");
    19 
    20         InputStreamReader isr = new InputStreamReader(new FileInputStream(
    21                 "osw.txt"), "UTF-8");
    22 
    23         // 读取数据
    24         // 一次读取一个字符
    25         int ch = 0;
    26         while ((ch = isr.read()) != -1) {
    27             System.out.print((char) ch);
    28         }
    29 
    30         // 释放资源
    31         isr.close();
    32     }
    33 }

    运行结果:

  • 相关阅读:
    windows 杀进程
    tool
    转:TestLink1.9.3测试用例:Excel转换XML工具<二>实现代码
    转:Excel转换XML工具<一>
    testlink 下载地址
    testng xml 示例
    eclipse中使用loadrunner java api步骤
    mybatis入门例子
    myBatis的引出
    maven
  • 原文地址:https://www.cnblogs.com/hebao0514/p/4863621.html
Copyright © 2011-2022 走看看