铁马冰河2000

导航

统计

文件读取工具类-FileReadUtil

==============================================文件读取工具类

复制代码
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FileReadUtil {
    
    private static final Logger logger = LoggerFactory.getLogger(FileReadUtil.class);
    
    /**
     * 获取输入流
     * @param relativePath 文件相对当前应用程序的类加载器的路径
     * @return
     */
    public static InputStream getResourceStream(String relativePath) {
        return Thread.currentThread().getContextClassLoader().getResourceAsStream(relativePath);
    }
    
    /**
     * 读取Text文件操作
     * @param filePath
     * @return
     */
    public static byte[] readToByte(String filePath) {
        FileInputStream in = null;
        byte[] data = null;
        try {
            in =new FileInputStream(new File(filePath));
            data = new byte[in.available()];
            in.read(data);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }
    
    /**
     * 读取Text文件操作
     * @param filePath
     * @return
     */
    public static List<String> readToList(String filePath) {
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        List<String> lines = new ArrayList<>();
        try {
            fileReader = new FileReader(filePath);
            bufferedReader = new BufferedReader(fileReader);
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                lines.add(line);
            }
        } catch (Exception e) {
            logger.info("读取文件失败, 原因:{}, filePath={}", e.getMessage(), filePath);
            e.printStackTrace();
        } finally {
            if(fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return lines;
    }
    
    /**
     * 读取Text文件操作
     * @param filePath
     * @return
     */
    public static String readToText(String filePath) {
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        String lines = "";
        try {
            fileReader = new FileReader(filePath);
            bufferedReader = new BufferedReader(fileReader);
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                lines += line + "\n";
            }
        } catch (Exception e) {
            logger.info("读取文件失败, 原因:{}, filePath={}", e.getMessage(), filePath);
            e.printStackTrace();
        } finally {
            if(fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return lines;
    }
}
复制代码

 

==============================================文件读取工具测试类

复制代码
    /**
     * 文件读取
     */
    @Test
    public void test_fileRead() {
        String filePath = "E:/home/commUtils/temp/Person03.txt";
//        String readToText = FileReadUtil.readToText(filePath);
//        System.out.println(readToText);
        List<String> readToList = FileReadUtil.readToList(filePath);
        for(String line : readToList) {
            System.out.println(line);
        }
//        byte[] readToByte = FileReadUtil.readToByte(filePath);
//        System.out.println(new String(readToByte));
    }
复制代码

 

posted on   铁马冰河2000  阅读(147)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示