자바 파일 읽 기 도구 클래스

3083 단어 도구 클래스
package com.lb.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 *        
 * @author admin
 *
 */
public class ReadUtil {

    /**
     *          ,         GBK      GBK      
     */
    private static final String FILE_CHARSET = "UTF-8";

    public static final List read(String filePath){
        List contents = new ArrayList<>();

        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), FILE_CHARSET))) {
            for (String line = br.readLine(); line != null; line = br.readLine()) {
                contents.add(line);
            }

            return contents;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static final String readAsString(String filePath){
        StringBuffer sbBuffer = new StringBuffer();

        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), FILE_CHARSET))) {
            for (String line = br.readLine(); line != null; line = br.readLine()) {
                sbBuffer.append(line + "\r
"
); } return sbBuffer.toString(); } catch (Exception e) { e.printStackTrace(); return null; } } }

좋은 웹페이지 즐겨찾기