FileWriterUtil.java 3.2 KB

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