文件读取工具类-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)); }