FileWriterUtil.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package com.fdkankan.fusion.common.util;
  2. import java.io.*;
  3. import java.math.BigDecimal;
  4. import java.nio.file.Paths;
  5. import java.nio.file.StandardCopyOption;
  6. import java.util.List;
  7. import java.util.Objects;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10. import static cn.hutool.core.util.ClassUtil.getClassLoader;
  11. public class FileWriterUtil {
  12. public static void writerJson(String tagPath,String fileName,String msg){
  13. try {
  14. File file = new File(tagPath);
  15. if(!file.exists()){
  16. file.mkdirs();
  17. }
  18. FileWriter fw = new FileWriter(tagPath +File.separator +fileName);
  19. fw.write(msg);
  20. fw.flush();
  21. fw.close();
  22. }catch (Exception e){
  23. e.printStackTrace();
  24. }
  25. }
  26. public static String readFile(String file) throws Exception {
  27. BufferedReader reader = new BufferedReader(new FileReader(file));
  28. String line = null;
  29. StringBuilder stringBuilder = new StringBuilder();
  30. String ls = System.getProperty("line.separator");
  31. try {
  32. while((line = reader.readLine()) != null) {
  33. stringBuilder.append(line);
  34. stringBuilder.append(ls);
  35. }
  36. return stringBuilder.toString();
  37. } finally {
  38. reader.close();
  39. }
  40. }
  41. public static void streamToFile(String fileName,String newFilePath){
  42. InputStream stream = getClassLoader().getResourceAsStream(fileName);
  43. try {
  44. assert stream != null;
  45. java.nio.file.Files.copy(
  46. stream,
  47. Paths.get(newFilePath),
  48. new StandardCopyOption[]{StandardCopyOption.REPLACE_EXISTING});
  49. }catch (Exception e){
  50. e.printStackTrace();
  51. }
  52. }
  53. public static String setFileSize(Long length) {
  54. BigDecimal decimal = new BigDecimal(length);
  55. BigDecimal size = new BigDecimal(1024);
  56. if(length >= 1024 * 1024 ){
  57. return decimal.divide(size.multiply(size)).setScale(2,BigDecimal.ROUND_HALF_UP).toString() + "MB";
  58. }
  59. return decimal.divide(size).setScale(2,BigDecimal.ROUND_HALF_UP).toString() +"KB";
  60. }
  61. public static void getCanRunList(List<File> fileList,File objPathFile) {
  62. File[] files = objPathFile.listFiles();
  63. for (File file1 : files) {
  64. if(file1.isFile()){
  65. if(file1.getName().endsWith(".obj") || file1.getName().endsWith(".las") || file1.getName().endsWith(".ply")
  66. || file1.getName().endsWith(".osgb") || file1.getName().endsWith(".b3dm") || file1.getName().endsWith(".laz")){
  67. fileList.add(file1);
  68. }
  69. }else {
  70. getCanRunList(fileList,file1);
  71. }
  72. }
  73. }
  74. public static String checkB3dmTileset(File objPathFile) {
  75. File[] files = objPathFile.listFiles();
  76. if(files == null || files.length<=0){
  77. return null;
  78. }
  79. for (File file : files) {
  80. if(file.isFile()){
  81. if(file.getName().endsWith(".json")){
  82. return file.getPath();
  83. }
  84. }
  85. }
  86. return null;
  87. }
  88. public static boolean isChinese(String str) {
  89. String regEx = "[\\u4e00-\\u9fa5]+";
  90. Pattern p = Pattern.compile(regEx);
  91. Matcher m = p.matcher(str);
  92. return m.find();
  93. }
  94. }