文件读取工具类-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)); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)