java读取txt文本工具类,按行读取txt返回List集合,读取所有返回String

一、按行读取txt并返回List集合

首先读取txt文本为文件字节流 FileInputStream ,然后转换为字符流,然后创建字符流缓冲区读取字符流,通过字符缓冲区 BufferdReader 的 readLine() 方法,按行读取。
关键代码:

	File file = new File("F:\\IDEA project\\test.txt");
    // 将字节流向字符流转换
    InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file), 
    	"utf-8");
    // 创建字符流缓冲区
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    String string = null;
    while ((string = bufferedReader.readLine()) != null) {
        System.out.println(string);
    }

txtUtil 工具类方法

    /**
     * 按行读取txt文件并返回一个List<String>集合
     * @param path
     * @return List<String>
     */
    public static List<String> getFileContent(String path) {
        List<String> strList = new ArrayList<>();
        File file = new File(path);
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        try {
            // 将字节流向字符流转换
            inputStreamReader = new InputStreamReader(new FileInputStream(file), "utf-8");
            // 创建字符流缓冲区
            bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            // 按行读取
            while ((line = bufferedReader.readLine()) != null) {
                strList.add(line);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return strList;
    }

二. 一次性读取txt文本所有内容,并返回String

可用两种方式实现,字节流读取或字符流读取。
使用字节流读取时,可以先定义一个字节数组 content[] ,然后使用 fileInputStream read(content[]) 方法,读取为字节并放入到定义的字节数组中,最终返回这样一个字节数组。
关键代码:

	byte[] fileContent = new byte[1024];
    FileInputStream fileInputStream = new FileInputStream(new File("FilePath"));
    fileInputStram.read(fileContent);

复制粘贴即可用的工具类方法:

	 /**
     * 字节流方式 读取所有txt文本内容
     * @param filePath
     * @return String
     */
    public static String getAllTxt(String filePath) {
        File file = new File(filePath);
        Long fileLengthLong = file.length();
        byte[] fileContent = new byte[fileLengthLong.intValue()];
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            fileInputStream.read(fileContent);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new String(fileContent);
    }

也可以使用字符流读取方式读取所有txt文本并返回一个String

    /**
     * 字符流读取方式 读取所有文本内容并返回String
     * @param filePath
     * @return String
     */
    public  String getAllTxtByChar(String filePath) {
        File file = new File(filePath);
        Long fileLength = file.length();
        char[] fileContent = new char[fileLength.intValue()];
        InputStreamReader inputStreamReader = null;
        try {
            inputStreamReader = new InputStreamReader(new FileInputStream(file));
            inputStreamReader.read(fileContent);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStreamReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new String(fileContent);
    }
  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值