- public static final Logger logger = LoggerFactory.getLogger(FileUtil.class);
-
- private static final int BUFFER_SIZE = 2 * 1024 * 1024;
-
- private static final int MAX_PACKAGE_SIZE = 50 * 1024 * 1024;
- private static Gson gson = new Gson();
-
- private FileUtil() {
- //Empty constructor
- }
-
- /**
- * create dir.
- *
- * @param dir
- * dir to create
- * @return boolean
- */
- public static boolean createDirectory(String dir) {
- File folder = new File(dir);
- if (!folder.exists() && !folder.mkdirs()) {
- return false;
- } else {
- return true;
- }
- }
-
- /**
- * delete file.
- *
- * @param file
- * the file to delete
- * @return boolean
- */
- public static boolean deleteFile(File file) {
- String hintInfo = file.isDirectory() ? "dir " : "file ";
- boolean isFileDeleted = file.delete();
- boolean isFileExist = file.exists();
- if (!isFileExist) {
- if (isFileDeleted) {
- logger.info("delete {} {}" ,hintInfo, file.getAbsolutePath());
- } else {
- isFileDeleted = true;
- logger.info("file not exist. no need delete {} {}" ,hintInfo , file.getAbsolutePath());
- }
- } else {
- logger.info("fail to delete {} {} " , hintInfo , file.getAbsolutePath());
- }
- return isFileDeleted;
- }
-
- /**
- * unzip zip file.
- *
- * @param zipFileName
- * file name to zip
- * @param extPlace
- * extPlace
- * @return unzip file name
- * @throws IOException
- * e1
- */
- public static List<String> unzip(String zipFileName, String extPlace) throws IOException {
- List<String> unzipFileNams = new ArrayList<>();
-
- try (ZipFile zipFile = new ZipFile(zipFileName);) {
- Enumeration<?> fileEn = zipFile.entries();
- byte[] buffer = new byte[BUFFER_SIZE];
-
- while (fileEn.hasMoreElements()) {
- ZipEntry entry = (ZipEntry) fileEn.nextElement();
- if (entry.isDirectory()) {
- continue;
- }
-
- File file = new File(extPlace, entry.getName());
- if (!file.getParentFile().exists()) {
- createDirectory(file.getParentFile().getAbsolutePath());
- }
-
- try (InputStream input = zipFile.getInputStream(entry);
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));) {
- int length = 0;
- while ((length = input.read(buffer)) != -1) {
- bos.write(buffer, 0, length);
- }
- unzipFileNams.add(file.getAbsolutePath());
- }
- }
- }
- return unzipFileNams;
- }
-
- public static boolean checkFileExists(String filePath) {
- File file = new File(filePath);
- return file.exists();
- }
-
- public static boolean deleteFile(String filePath) {
- File file = new File(filePath);
- return deleteFile(file);
- }
-
- public static boolean writeJsonDatatoFile(String fileAbsPath, Object obj) {
- logger.info("Write JsonData to file : {} " , fileAbsPath);
-
- boolean bResult = false;
- if (checkFileExists(fileAbsPath)) {
- deleteFile(fileAbsPath);
- }
-
- try(FileWriter writer = new FileWriter(new File(fileAbsPath))) {
- gson.toJson(obj, writer);
- bResult = true;
- } catch (Exception e) { //NOSONAR
- logger.info("Exception: writeJsonDatatoFile-->" + fileAbsPath, e);
- }
- return bResult;
- }
-
- public static <T> Object readJsonDatafFromFile(String fileAbsPath, Class<T> clazz) {
- if (!checkFileExists(fileAbsPath)) {
- logger.info("read JsonData from file , file not found : {}" ,fileAbsPath);
- return null;
- }
-
- logger.info("read JsonData from file : {}" , fileAbsPath);
-
- T obj = null;
- /*
+ public static final Logger logger = LoggerFactory.getLogger(FileUtil.class);
+
+ private static final int BUFFER_SIZE = 2 * 1024 * 1024;
+
+ private static final int MAX_PACKAGE_SIZE = 50 * 1024 * 1024;
+ private static Gson gson = new Gson();
+
+ private FileUtil() {
+ //Empty constructor
+ }
+
+ /**
+ * create dir.
+ *
+ * @param dir
+ * dir to create
+ * @return boolean
+ */
+ public static boolean createDirectory(String dir) {
+ File folder = new File(dir);
+ if (!folder.exists() && !folder.mkdirs()) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+ /**
+ * delete file.
+ *
+ * @param file
+ * the file to delete
+ * @return boolean
+ */
+ public static boolean deleteFile(File file) {
+ String hintInfo = file.isDirectory() ? "dir " : "file ";
+ String fileAbsPath = "";
+ boolean isFileDeleted=false;
+ try {
+ if (file.exists()){
+ Files.delete(Paths.get(file.getPath()));
+ fileAbsPath = file.getAbsolutePath();
+ logger.info("delete {} {}" ,hintInfo, fileAbsPath);
+ }
+ else{
+ logger.info("file not exist. no need delete {} {}" ,hintInfo , fileAbsPath);
+ }
+ isFileDeleted=true;
+
+ } catch (IOException e) {
+ logger.error("fail to delete {} {} ", hintInfo, fileAbsPath, e);
+ }
+ return isFileDeleted;
+ }
+
+ /**
+ * unzip zip file.
+ *
+ * @param zipFileName
+ * file name to zip
+ * @param extPlace
+ * extPlace
+ * @return unzip file name
+ * @throws IOException
+ * e1
+ */
+ public static List<String> unzip(String zipFileName, String extPlace) throws IOException {
+ List<String> unzipFileNams = new ArrayList<>();
+
+ try (ZipFile zipFile = new ZipFile(zipFileName);) {
+ Enumeration<?> fileEn = zipFile.entries();
+ byte[] buffer = new byte[BUFFER_SIZE];
+
+ while (fileEn.hasMoreElements()) {
+ ZipEntry entry = (ZipEntry) fileEn.nextElement();
+ if (entry.isDirectory()) {
+ continue;
+ }
+
+ File file = new File(extPlace, entry.getName());
+ if (!file.getParentFile().exists()) {
+ createDirectory(file.getParentFile().getAbsolutePath());
+ }
+
+ try (InputStream input = zipFile.getInputStream(entry);
+ BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));) {
+ int length = 0;
+ while ((length = input.read(buffer)) != -1) {
+ bos.write(buffer, 0, length);
+ }
+ unzipFileNams.add(file.getAbsolutePath());
+ }
+ }
+ }
+ return unzipFileNams;
+ }
+
+ public static boolean checkFileExists(String filePath) {
+ File file = new File(filePath);
+ return file.exists();
+ }
+
+ public static boolean deleteFile(String filePath) {
+ File file = new File(filePath);
+ return deleteFile(file);
+ }
+
+ public static boolean writeJsonDatatoFile(String fileAbsPath, Object obj) {
+ logger.info("Write JsonData to file : {} " , fileAbsPath);
+
+ boolean bResult = false;
+ if (checkFileExists(fileAbsPath)) {
+ deleteFile(fileAbsPath);
+ }
+
+ try(FileWriter writer = new FileWriter(new File(fileAbsPath))) {
+ gson.toJson(obj, writer);
+ bResult = true;
+ } catch (Exception e) { //NOSONAR
+ logger.info("Exception: writeJsonDatatoFile-->" + fileAbsPath, e);
+ }
+ return bResult;
+ }
+
+ public static <T> Object readJsonDatafFromFile(String fileAbsPath, Class<T> clazz) {
+ if (!checkFileExists(fileAbsPath)) {
+ logger.info("read JsonData from file , file not found : {}" ,fileAbsPath);
+ return null;
+ }
+
+ logger.info("read JsonData from file : {}" , fileAbsPath);
+
+ T obj = null;
+ /*