From da5c4cd996773cedb7b6ac33e2c56761767bedbb Mon Sep 17 00:00:00 2001 From: wangbing Date: Thu, 19 Mar 2020 16:26:51 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81FileUtil?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: fd7dfe188a5165d44612f1bfa8073418be81679f --- .../SpringBoot/java/frame/utils/FileUtil.java | 115 +++++++++++++++++- 1 file changed, 110 insertions(+), 5 deletions(-) diff --git a/src/main/resources/modules/SpringBoot/java/frame/utils/FileUtil.java b/src/main/resources/modules/SpringBoot/java/frame/utils/FileUtil.java index e5031b93..a9b7dc19 100644 --- a/src/main/resources/modules/SpringBoot/java/frame/utils/FileUtil.java +++ b/src/main/resources/modules/SpringBoot/java/frame/utils/FileUtil.java @@ -1,9 +1,48 @@ package ${basePackage}.frame.utils; -import java.io.*; +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; public class FileUtil { + public static List readFile2Lines(String fileName) { + List returnString = new ArrayList(); + File file = new File(fileName); + BufferedReader reader = null; + try { + reader = new BufferedReader(new FileReader(file)); + String tempString = null; + int line = 1; + // 一次读入一行,直到读入null为文件结束 + while ((tempString = reader.readLine()) != null) { + // 显示行号 + returnString.add(tempString); + line++; + } + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e1) { + } + } + } + return returnString; + } + public static String readFileToString(File file) throws IOException { byte[] bytes = readFileToByteArray(file); return new String(bytes, "UTF-8"); @@ -61,20 +100,20 @@ public class FileUtil { } public static byte[] toByteArray(InputStream input, int size) throws IOException { - if(size < 0) { + if (size < 0) { throw new IllegalArgumentException("Size must be equal or greater than zero: " + size); - } else if(size == 0) { + } else if (size == 0) { return new byte[0]; } else { byte[] data = new byte[size]; int offset; int read; - for(offset = 0; offset < size && (read = input.read(data, offset, size - offset)) != -1; offset += read) { + for (offset = 0; offset < size && (read = input.read(data, offset, size - offset)) != -1; offset += read) { ; } - if(offset != size) { + if (offset != size) { throw new IOException("Unexpected read size. current: " + offset + ", expected: " + size); } else { return data; @@ -116,6 +155,18 @@ public class FileUtil { return count > 2147483647L ? -1 : (int) count; } + public static boolean copy(File in, File out) throws Exception { + try { + FileInputStream input = new FileInputStream(in); + FileOutputStream output = new FileOutputStream(out); + copy(input, output); + input.close(); + output.close(); + return true; + } catch (IOException ie) { + return false; + } + } public static long copyLarge(InputStream input, OutputStream output) throws IOException { return copy(input, output, 4096); @@ -134,4 +185,58 @@ public class FileUtil { return count; } + + public static boolean clearDirectory(File dir) { + if ((dir == null) || !dir.isDirectory()) { + throw new IllegalArgumentException("Argument " + dir + + " is not a directory. "); + } + + File[] entries = dir.listFiles(); + int sz = entries.length; + + for (int i = 0; i < sz; i++) { + if (entries[i].isDirectory()) { + if (!clearDirectory(entries[i])) { + return false; + } + } else { + if (!entries[i].delete()) { + return false; + } + } + } + + if (!dir.delete()) { + return false; + } + return true; + } + + /** + * 将DOS/Windows格式的路径转换为UNIX/Linux格式的路径。 + * 其实就是将路径中的"\"全部换为"/",因为在某些情况下我们转换为这种方式比较方便, + * 某中程度上说"/"比"\"更适合作为路径分隔符,而且DOS/Windows也将它当作路径分隔符。 + */ + public static String toUNIXpath(String filePath) { + return filePath.replace('\\', '/'); + } + + public static void inputStream2File(InputStream is, File target) { + try { + if (!target.exists() && !target.createNewFile()) { + throw new RuntimeException(target.getName() + "not exists"); + } + OutputStream outStream = new FileOutputStream(target); + byte[] buffer = new byte[8 * 1024]; + int bytesRead; + while ((bytesRead = is.read(buffer)) != -1) { + outStream.write(buffer, 0, bytesRead); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } }