package ${basePackage}.frame.utils; 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.io.PrintWriter; import java.util.ArrayList; import java.util.List; public class FileUtil { public static List<String> readFileToLines(File filePath) { return readFileToLines(filePath.getAbsoluteFile()); } public static List<String> readFileToLines(String fileName) { List<String> returnString = new ArrayList<String>(); 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"); } public static byte[] readFileToByteArray(File file) throws IOException { InputStream in = openInputStream(file); Throwable var2 = null; byte[] var5; try { long fileLength = file.length(); var5 = fileLength > 0L ? toByteArray(in, fileLength) : toByteArray(in); } catch (Throwable var14) { var2 = var14; throw var14; } finally { if (in != null) { if (var2 != null) { try { in.close(); } catch (Throwable var13) { var2.addSuppressed(var13); } } else { in.close(); } } } return var5; } public static boolean writeStringToFile(String data, File file, boolean isNewLine, boolean isAppend) { PrintWriter printWriter = null; try { printWriter = openPrintWriter(file); if (isNewLine && isAppend) {// 换行追加 printWriter.println(); printWriter.append(data); } else if (!isNewLine && isAppend) {// 不换行追加 printWriter.append(data); } else {// 直接写入 printWriter.print(data); } return true; } catch (Exception e) { e.printStackTrace(); return false; } finally { if (printWriter != null) printWriter.close(); } } public static PrintWriter openPrintWriter(File file) { try { return new PrintWriter(new FileOutputStream(file)); } catch (FileNotFoundException e) { e.printStackTrace(); return null; } } public static FileInputStream openInputStream(File file) throws IOException { if (file.exists()) { if (file.isDirectory()) { throw new IOException("File '" + file + "' exists but is a directory"); } else if (!file.canRead()) { throw new IOException("File '" + file + "' cannot be read"); } else { return new FileInputStream(file); } } else { throw new FileNotFoundException("File '" + file + "' does not exist"); } } public static byte[] toByteArray(InputStream input, long size) throws IOException { if (size > 2147483647L) { throw new IllegalArgumentException("Size cannot be greater than Integer max value: " + size); } else { return toByteArray(input, (int) size); } } public static byte[] toByteArray(InputStream input, int size) throws IOException { if (size < 0) { throw new IllegalArgumentException("Size must be equal or greater than zero: " + size); } 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) { ; } if (offset != size) { throw new IOException("Unexpected read size. current: " + offset + ", expected: " + size); } else { return data; } } } public static byte[] toByteArray(InputStream input) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); Throwable var2 = null; byte[] var3; try { copy((InputStream) input, (OutputStream) output); var3 = output.toByteArray(); } catch (Throwable var12) { var2 = var12; throw var12; } finally { if (output != null) { if (var2 != null) { try { output.close(); } catch (Throwable var11) { var2.addSuppressed(var11); } } else { output.close(); } } } return var3; } public static int copy(InputStream input, OutputStream output) throws IOException { long count = copyLarge(input, output); 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); } public static long copy(InputStream input, OutputStream output, int bufferSize) throws IOException { return copyLarge(input, output, new byte[bufferSize]); } public static long copyLarge(InputStream input, OutputStream output, byte[] buffer) throws IOException { long count; int n; for (count = 0L; -1 != (n = input.read(buffer)); count += (long) n) { output.write(buffer, 0, n); } 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(); } } }