Преглед на файлове

新增文件读取工具类

tianyunperfect преди 4 години
родител
ревизия
ad0ce6e0b8
променени са 2 файла, в които са добавени 44 реда и са изтрити 0 реда
  1. 6 0
      springboot-common/pom.xml
  2. 38 0
      springboot-common/src/main/java/com/alvin/common/util/FileUtil.java

+ 6 - 0
springboot-common/pom.xml

@@ -31,6 +31,12 @@
             <version>3.8</version>
         </dependency>
 
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>2.4</version>
+        </dependency>
+
         <!--httpClient-->
         <dependency>
             <groupId>org.apache.httpcomponents</groupId>

+ 38 - 0
springboot-common/src/main/java/com/alvin/common/util/FileUtil.java

@@ -0,0 +1,38 @@
+package com.alvin.common.util;
+
+import java.io.*;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * 常用文件工具类
+ * 1、读取所有行请使用common-io工具类
+ * 2、读取单行数据可以使用
+ *
+ * @author tianyunperfect
+ * @date 2021/01/15
+ */
+public class FileUtil {
+    /**
+     * 获取到 BF,一定要手动关闭
+     *
+     * @param filePath 文件路径
+     * @param encode   编码
+     * @return {@link BufferedReader}* @throws FileNotFoundException 文件未发现异常
+     */
+    public static BufferedReader getBufferedReader(String filePath, String encode) throws FileNotFoundException, UnsupportedEncodingException {
+        return new BufferedReader(
+                new InputStreamReader(
+                        new FileInputStream(filePath), encode));
+    }
+
+    /**
+     * 获取到 BF,一定要手动关闭,默认UTF-8
+     *
+     * @param filePath 文件路径
+     * @return {@link BufferedReader}* @throws FileNotFoundException 文件未发现异常
+     * @throws UnsupportedEncodingException 不支持的编码异常
+     */
+    public static BufferedReader getBufferedReader(String filePath) throws FileNotFoundException, UnsupportedEncodingException {
+        return getBufferedReader(filePath, StandardCharsets.UTF_8.name());
+    }
+}