工具类通过流的方式读取文件. @author Junzhou Liu @since 2020/10/17 19:31
| 15 | * @since 2020/10/17 19:31 |
| 16 | */ |
| 17 | @Slf4j |
| 18 | public class ReadFileUtils { |
| 19 | |
| 20 | /** |
| 21 | * 读取指定路径的文件。 |
| 22 | * |
| 23 | * @return fileContentStr |
| 24 | */ |
| 25 | public static String readFile(String filePath) { |
| 26 | String fileContentStr = null; |
| 27 | try { |
| 28 | InputStream inputStream = new FileInputStream(filePath); |
| 29 | int size = inputStream.available(); |
| 30 | byte[] buffer = new byte[size]; |
| 31 | inputStream.read(buffer); |
| 32 | inputStream.close(); |
| 33 | fileContentStr = new String(buffer, StandardCharsets.UTF_8); |
| 34 | } catch (FileNotFoundException e) { |
| 35 | log.debug("file not found", e); |
| 36 | } catch (IOException e) { |
| 37 | log.warn("", e); |
| 38 | } |
| 39 | return fileContentStr; |
| 40 | } |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * 从resource读取版本文件. |
| 45 | * |
| 46 | * @param fileName 文件名. |
| 47 | * @return fileContentStr |
| 48 | */ |
| 49 | public static String loadJsonFromAsset(String fileName) { |
| 50 | String fileContentStr = null; |
| 51 | try { |
| 52 | InputStream is = ReadFileUtils.class.getClassLoader().getResourceAsStream(fileName); |
| 53 | assert is != null; |
| 54 | int size = is.available(); |
| 55 | byte[] buffer = new byte[size]; |
| 56 | is.read(buffer); |
| 57 | is.close(); |
| 58 | fileContentStr = new String(buffer, StandardCharsets.UTF_8); |
| 59 | } catch (IOException e) { |
| 60 | log.warn("", e); |
| 61 | } |
| 62 | return fileContentStr; |
| 63 | } |
| 64 | |
| 65 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…