All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.net.wanmo.common.io.ReadLineUtil Maven / Gradle / Ivy

The newest version!
package cn.net.wanmo.common.io;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 按行读取
 */
public class ReadLineUtil {

    /**
     * 字符串按行读取
     *
     * @param str 字符串
     * @return 每行数据
     * @throws IOException 异常
     */
    public static List readStr(String str) throws IOException {
        List list = new ArrayList<>();

        BufferedReader bufferedReader = new BufferedReader(new StringReader(str));

        String line;
        while ((line = bufferedReader.readLine()) != null) {
            list.add(line);
        }

        return list;
    }

    /**
     * 文件按行读取
     *
     * @param file 文件
     * @return 每行数据
     * @throws IOException 异常
     */
    public static List readFile(File file) throws IOException {
        List list = new ArrayList<>();

        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

        String line;
        while ((line = bufferedReader.readLine()) != null) {
            list.add(line);
        }

        return list;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy