Java如何读取文件文本内容的几种方式汇总

本文为joshua317原创文章,转载请注明:转载自joshua317博客 Java如何读取文件文本内容的几种方式汇总 - joshua317的博客

package com.joshua317;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Stream;

public class FileDemo {
    static String filePath = "E:\\1.txt";
    public static void main(String[] args) throws IOException {
        readFile1();
//        readFile1_1();
//        readFile2();
//        readFile3();
//        readFile4();
//        readFile5();
//        readFile6();
//        readFile6_1();
//        readFile7();
//        readFile7_1();
    }

    /**
     * 字节流>字符流>字符串
     * 使用File,FileInputStream,InputStreamReader,BufferedReader
     * 使用FileInputStream,InputStreamReader,BufferedReader
     * @throws IOException
     */
    public static void readFile1() throws IOException {
//        使用File
//        File file = new File(filePath);
//        FileInputStream fileInputStream = new FileInputStream(file);

//        FileInputStream(字节流) 实现了InputStream接口,用来读取文件中的字节流,参数是文件或者文件路径+文件名称
        FileInputStream fileInputStream = new FileInputStream(filePath);
//        将 fileInputStream(字节流) 流作为参数,转为InputStreamReader(字符流)
        InputStreamReader reader = new InputStreamReader(fileInputStream);

//      将 字符流(参数)转为字符串流,带缓冲的流读取,默认缓冲区8k
        BufferedReader bufferedReader = new BufferedReader(reader);
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        fileInputStream.close();
        bufferedReader.close();
    }

    public static void readFile1_1() throws IOException {
//        使用File
//        File file = new File(filePath);
//        FileInputStream fileInputStream = new FileInputStream(file);

//        FileInputStream(字节流) 实现了InputStream接口,用来读取文件中的字节流,参数是文件或者文件路径+文件名称
        FileInputStream fileInputStream = new FileInputStream(filePath);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int i;
        while ((i = fileInputStream.read()) != -1) {
            byteArrayOutputStream.write(i);
        }
        System.out.println(byteArrayOutputStream.toString());
        fileInputStream.close();
        byteArrayOutputStream.close();
    }


    /**
     * 使用File,FileReader,BufferedReader
     * 使用FileReader,BufferedReader
     * @throws IOException
     */
    public static void readFile2() throws IOException {
//        使用File
//        File file = new File(filePath);
//        FileReader fileReader = new FileReader(file);

        FileReader fileReader = new FileReader(filePath);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        fileReader.close();
        bufferedReader.close();
    }

    /**
     * 使用FileReader
     * @throws IOException
     */
    public static void readFile3() throws IOException {
//        使用File
//        File file = new File(filePath);
//        FileReader fileReader = new FileReader(file);

        FileReader fileReader = new FileReader(filePath);
        int i;
        while ((i = fileReader.read()) != -1) {
            System.out.print((char)i);
        }
        fileReader.close();
    }

    /**
     * 使用Scanner
     * @throws IOException
     */
    public static void readFile4() throws IOException {
        File file = new File(filePath);
        Scanner sc = new Scanner(file);
        while (sc.hasNextLine()) {
            System.out.println(sc.nextLine());
        }
        sc.close();
    }

    /**
     * 读取整个文件
     * 使用Files.readAllBytes,Paths
     * @throws IOException
     */
    public static void readFile5() throws IOException {
        Path path = Paths.get(filePath);
        byte[] bytes = Files.readAllBytes(path);
        String data = new String(bytes);
        System.out.println(data);
    }

    /**
     * 读取整个文件
     * 使用Files.readAllLines,Paths,迭代器
     * @throws IOException
     */
    public static void readFile6() throws IOException {
        Path path = Paths.get(filePath);
        List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
        Iterator<String> itr = lines.iterator();
        while (itr.hasNext()) {
            System.out.println(itr.next());
        }
    }

    /**
     * 使用Files.lines
     * @throws IOException
     */
    public static void readFile6_1() throws IOException {
        Path path = Paths.get(filePath);
        Stream<String> lines = Files.lines(path);
        lines.forEach(e -> {
            System.out.println(e);
        });
    }

    /**
     * 使用Files.newBufferedReader
     * @throws IOException
     */
    public static void readFile7() throws IOException {
        Path path = Paths.get(filePath);
        BufferedReader bufferedReader= Files.newBufferedReader(path);
        String str;
        while ((str = bufferedReader.readLine())!=null){
            System.out.println(str);
        }
        bufferedReader.close();
    }

    public static void readFile7_1() throws IOException {
        Path path = Paths.get(filePath);
        BufferedReader bufferedReader= Files.newBufferedReader(path);
        String str;
        StringBuilder stringBuilder = new StringBuilder();
        while ((str = bufferedReader.readLine())!=null){
            stringBuilder.append(str + "\n");
        }
        System.out.println(stringBuilder.toString());
        bufferedReader.close();
    }
}

本文为joshua317原创文章,转载请注明:转载自joshua317博客 Java如何读取文件文本内容的几种方式汇总 - joshua317的博客

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值