FileWriterUtil.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package com.fdkankan.fusion.common.util;
  2. import java.io.*;
  3. import java.nio.file.Paths;
  4. import java.nio.file.StandardCopyOption;
  5. import static cn.hutool.core.util.ClassUtil.getClassLoader;
  6. public class FileWriterUtil {
  7. public static void writerJson(String tagPath,String fileName,String msg){
  8. try {
  9. File file = new File(tagPath);
  10. if(!file.exists()){
  11. file.mkdirs();
  12. }
  13. FileWriter fw = new FileWriter(tagPath +File.separator +fileName);
  14. fw.write(msg);
  15. fw.flush();
  16. fw.close();
  17. }catch (Exception e){
  18. e.printStackTrace();
  19. }
  20. }
  21. public static String readFile(String file) throws Exception {
  22. BufferedReader reader = new BufferedReader(new FileReader(file));
  23. String line = null;
  24. StringBuilder stringBuilder = new StringBuilder();
  25. String ls = System.getProperty("line.separator");
  26. try {
  27. while((line = reader.readLine()) != null) {
  28. stringBuilder.append(line);
  29. stringBuilder.append(ls);
  30. }
  31. return stringBuilder.toString();
  32. } finally {
  33. reader.close();
  34. }
  35. }
  36. public static void streamToFile(String fileName,String newFilePath){
  37. InputStream stream = getClassLoader().getResourceAsStream(fileName);
  38. try {
  39. assert stream != null;
  40. java.nio.file.Files.copy(
  41. stream,
  42. Paths.get(newFilePath),
  43. new StandardCopyOption[]{StandardCopyOption.REPLACE_EXISTING});
  44. }catch (Exception e){
  45. e.printStackTrace();
  46. }
  47. }
  48. public static File getObjLasPlyFile(File objPathFile) {
  49. if(objPathFile.isDirectory()){
  50. File[] file = objPathFile.listFiles();
  51. if(file == null || file.length <=0){
  52. return null;
  53. }
  54. for (File file1 : file) {
  55. if(file1 == null){
  56. continue;
  57. }
  58. if(file1.isDirectory()){
  59. return getObjLasPlyFile(file1);
  60. }
  61. if(file1.getName().contains("obj") || file1.getName().contains("ply") || file1.getName().contains("las")){
  62. return file1;
  63. }
  64. }
  65. }
  66. return null;
  67. }
  68. }