JavaIO流读取文本文件教程

介绍

字节输入流(InputStream

  • FileInputStream
  • BufferedInputStream

InputStream

字节输入流的父类是 InputStream 实现的子类有着很多比如ByteArrayInputStream、FilterInputStream 等等

FileInputStream

文件字节输入流,常用于操作文本文件等字节文件

常用方法
  • int read() 直接读取字节内容
  • int read(byte[] byte) 根据字节数组进行读取字节内容
使用场景

功能需求

假设我们现在需要操作 D盘一个名为text.txt 的文本文件,内容为”:CSDN YYDS。"此时我们密切的需要得到里面的文本内容进行打印。

代码实例
read()方法实现
    /** 字节输入流方法
     * 
     * @param file 文件
     */
private static void fileReadText(File file){
     //FileInputStream
     try {
     //文件字节输入流
     FileInputStream inputStream = new FileInputStream(file);   
     int num = 0;
     String readText;
     int i = 0;
     //根据文件大小获取相应数量
     char[] chars = new char[inputStream.available()];
     //进入循环 只要num == -1 那就是读取结束了
     while( (num = reader.read()) != -1){
     //通过char来获取数据内容
     chars[i] =  (char)num;
     i++;
	}
     //通过 string内置构造方法转换char[] - >String 类型
     readText = new String(chars);
     System.out.println("字符串:" + readText);
     inputStream.close();
    }catch (IOException e){
     	e.printStackTrace();
    }
}

Main

    private static void load() {
        File file = new File("D://text.txt");
        readText(file);
    }
    
    public static void main(String[] args) {
        load();
    }

在这里插入图片描述

read(byte[] byte)方法实现

    /** 字节输入流方法
     *
     * @param file 文件
     */
        private static void fileReadText(File file){
            //FileInputStream
            try {
                //文件字节输入流
                FileInputStream inputStream = new FileInputStream(file);
                //定义byte数组
                byte[] bytes = new byte[inputStream.available()];
                int num = 0;
                //定义一个StringBuilder来进行读存
                StringBuilder sb = new StringBuilder();
                //根据文件大小获取相应数量
                char[] chars = new char[inputStream.available()];
                //进入循环 只要num == -1 那就是读取结束了
                while( (num = inputStream.read(bytes)) != -1){
                    //可以直接用String(byte[] byte)的构造方法来读取
                    String resultText = new String(bytes);
                    sb.append(resultText);
                }
                System.out.println("字符串:" + sb.toString());
                inputStream.close();
            }catch (IOException e)
            {
                e.printStackTrace();
            }
    }

在这里插入图片描述

BufferedInputStream

read()方法实现

代码实例

    /** 缓冲输入字节流
     *
     * @param file
     */

	private static void readBufferedReader(File file) {
				try {
                    //定义缓冲字节流
                    BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
                    int i = 0;
                    StringBuilder sb = new StringBuilder();
                    int num = 0;
                    String resultString = "";
                    char[] chars = new char[new FileInputStream(file).available()];
                    byte [] bytes = new byte[new FileInputStream(file).available()];
                    while((num = bufferedInputStream.read()) !=-1){
                        chars[i] = (char)num;
                        i++;
                    }
                    resultString = new String(chars);
                    System.out.println("字符串:"  + resultString);

                    bufferedReader.close();
                }

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

在这里插入图片描述

read(byte[] byte)方法实现

代码实例

    /** 缓冲输入字节流
     *
     * @param file
     */

		private static void readBufferedReader(File file) {
                try {
                    //定义缓冲字节流
                    BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
                    int i = 0;
                    StringBuilder sb = new StringBuilder();
                    int num = 0;
                    String resultString = "";
                    char[] chars = new char[new FileInputStream(file).available()];
                    byte [] bytes = new byte[new FileInputStream(file).available()];

                    while((num = bufferedInputStream.read(bytes)) !=-1){
                        resultString = new String(bytes);
                        sb.append(resultString);
                    }
                    System.out.println("字符串:"  + sb.toString());

                    bufferedReader.close();
                }

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

在这里插入图片描述

温馨提示

直接打印中文数据有可能导致乱码。那么我们需要一个InputStreamReader来进行编码转换,是后话了关于字符流的内容。

字符输入流(Reader

  • FileInputStream
  • BufferedInputStream

InputReader

字节输入流的父类是 Reader 实现的子类有着很多比如FileReader、BufferedReader等等

常用方法
  • int read() 直接读取字节内容
  • int read(char[] char) 根据字节数组进行读取字节内容
使用场景

功能需求

还是用上面 text.txt 的文本文件,内容为”:欢迎来到CSDN,我是Marinda_Speed,这是关于IO流的教程哦!"我们既然用字符流,那就大方的使用中文,注意转码就可以。

代码实例
read()方法实现
 /** 文件读取
     *
     * @param file 文件
     */

    private static void readFileReader(File file)
    {
        try {
            //定义
            FileReader fr = new FileReader(file);
            int num = 0;
            //定义字符组
            char[] chars = new char[new FileInputStream(file).available()];
            int i = 0;
            String resultText = "";
            while((num = fr.read()) != -1){
                chars[i] = (char)num;
                i++;
            }
            resultText = new String(chars);
            System.out.println("读取的信息:" + resultText);
            fr.close();

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

在这里插入图片描述

read(char[] char)方法实现
    /** 文件读取
     *
     * @param file 文件
     */

    private static void readFileReader(File file)
    {
        try {
            //定义
            FileReader fr = new FileReader(file);
            int num = 0;
            //定义字符组
            char[] chars = new char[new FileInputStream(file).available()];
            StringBuilder sb = new StringBuilder();
            int i = 0;
            String resultText = "";
            while((num = fr.read(chars)) != -1){
                resultText = new String(chars);
                sb.append(resultText);
            }
            System.out.println("读取的信息:" + sb.toString());
            fr.close();

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

在这里插入图片描述

BufferedReader

文件字节输入流,常用于操作文本文件等字符文件

read()方法实现
    /** 文件读取
     *
     * @param file 文件
     */

    private static void readBufferedReaders(File file)
    {
        try {
            //定义
            BufferedReader fr = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
            int num = 0;
            //定义字符组
            char[] chars = new char[new FileInputStream(file).available()];
            StringBuilder sb = new StringBuilder();
            int i = 0;
            String resultText = "";
            while((num = fr.read()) != -1){
                chars[i] = (char)num;
                i++;
            }
            resultText = new String(chars);
            System.out.println("读取的信息:" + resultText);
            fr.close();

        }catch (IOException e)
        {
            e.printStackTrace();
        }
    }
read(char[] char)方法实现
  /** 文件读取
     *
     * @param file 文件
     */

    private static void readBufferedReaders(File file)
    {
        try {
            //定义
            BufferedReader fr = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
            int num = 0;
            //定义字符组
            char[] chars = new char[new FileInputStream(file).available()];
            StringBuilder sb = new StringBuilder();
            int i = 0;
            String resultText = "";
            while((num = fr.read(chars)) != -1){
                resultText = new String(chars);
                sb.append(resultText);
            }
            System.out.println("读取的信息:" + sb.toString());
            fr.close();

        }catch (IOException e)
        {
            e.printStackTrace();
        }
    }
readLine()方法实现
    /** 文件读取
     *
     * @param file 文件
     */

    private static void readBufferedReaders(File file)
    {
        try {
            //定义
            BufferedReader fr = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
            int num = 0;
            //定义字符组
            char[] chars = new char[new FileInputStream(file).available()];
            StringBuilder sb = new StringBuilder();
            int i = 0;
            String resultText = "";
            while((resultText = fr.readLine()) != null){
                sb.append(resultText);
            }
            System.out.println("读取的信息:" + sb.toString());
            fr.close();

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

在这里插入图片描述

结束语

以上是关于IO流中常见的字节流字符流读取文件文本方式,关于写入的操作就更简单了,有兴趣的小伙伴可以去百度或者CSDN搜索学习

祝大家学习愉快、工作顺利,有不足之处或修改建议欢迎留言评论

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值