输入输出流:InputStreamReader:字节字符流 基础复习

在这里插入图片描述

package imooc.readerdemo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * InputStreamReader: InputStream字节输入流的父类,Reader:字符输入流的父类
 *
 * @author lixw
 * @date created in 22:39 2019/1/25
 */
public class ReaderDemo {
    public static void main(String[] args) {
        try {
            //读入数据
            FileInputStream fis = new FileInputStream("src/imooc.txt");
            InputStreamReader isr = new InputStreamReader(fis);
            //
            int n = 0;
            char [] chuf = new char[10];
            while ((n = isr.read())!=-1){
                System.out.print((char)n);
            }

//            while ((n = isr.read(chuf))!=-1){
//                String s = new String(chuf);
//                System.out.println(s);
//            }

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

原样输出:
在这里插入图片描述

package imooc.readerdemo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * InputStreamReader: InputStream字节输入流的父类,Reader:字符输入流的父类
 *
 * @author lixw
 * @date created in 22:39 2019/1/25
 */
public class ReaderDemo {
    public static void main(String[] args) {
        try {
            //读入数据
            FileInputStream fis = new FileInputStream("src/imooc.txt");
            InputStreamReader isr = new InputStreamReader(fis);
            //
            int n = 0;
            char [] chuf = new char[10];
//            while ((n = isr.read())!=-1){
//                System.out.print((char)n);
//            }

            while ((n = isr.read(chuf))!=-1){
                String s = new String(chuf);
                System.out.println(s);
            }

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

输出格式:有问题
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_42664961/article/details/86653037