Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. package utils;
  2.  
  3. import interfaces.OnResult;
  4.  
  5. import java.io.*;
  6. import java.net.URISyntaxException;
  7. import java.nio.file.Files;
  8. import java.nio.file.Paths;
  9. import java.nio.file.StandardOpenOption;
  10. import java.util.ArrayList;
  11.  
  12. public class FileUtils {
  13. private static int index = 0;
  14. private static int count = 0;
  15.  
  16. public static void split(String folder, String dataFile, int quantity) {
  17. read(folder + dataFile, data -> {
  18. String newName = dataFile;
  19. if (newName.contains(".")) {
  20. newName = newName.split("\\.")[0];
  21. }
  22. appendFile(folder + newName + "-" + index + ".dat", data + "\n");
  23. if (count % 2 == 1) {
  24. if (++index == quantity) index = 0;
  25. }
  26. count++;
  27. });
  28. }
  29.  
  30. public static long countLines(String path) {
  31. long count = 0;
  32. File fileDir = new File(path);
  33. try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileDir), "UTF8"))) {
  34. while (br.readLine() != null) {
  35. count++;
  36. }
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. }
  40. return count;
  41. }
  42.  
  43. public static void listFiles(String path, OnResult onResult) {
  44. File folder = new File(path);
  45. File[] listOfFiles = folder.listFiles();
  46. if (listOfFiles != null) {
  47. for (File file : listOfFiles) {
  48. if (file.isFile()) {
  49. onResult.onSuccess(file.getName());
  50. }
  51. }
  52. } else {
  53. Logs.infoLn("List files null");
  54. }
  55. }
  56.  
  57. public static void append(String path, Object data) {
  58. try {
  59. File f = new File(path);
  60. if (!f.exists()) {
  61. f.createNewFile();
  62. }
  63. Files.write(Paths.get(path), data.toString().getBytes(), StandardOpenOption.APPEND);
  64. } catch (IOException e) {
  65. Logs.infoLn(e.getMessage());
  66. }
  67. }
  68.  
  69. public static File getFileResource(String path) {
  70. try {
  71. return new File(ClassLoader.getSystemResource(path).toURI());
  72. } catch (URISyntaxException e) {
  73. e.printStackTrace();
  74. return null;
  75. }
  76. }
  77.  
  78. public static void write(String path, String message) {
  79. try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "UTF8"))) {
  80. bw.write(message);
  81. bw.close();
  82. } catch (IOException e) {
  83. e.printStackTrace();
  84. }
  85. }
  86.  
  87. public static void readResource(String path, OnResult onResult) {
  88. InputStream fileInputStream = ClassLoader.getSystemResourceAsStream(path);
  89. try (BufferedReader br = new BufferedReader(new InputStreamReader(fileInputStream, "UTF8"))) {
  90. String line;
  91. while ((line = br.readLine()) != null) onResult.onSuccess(line);
  92. br.close();
  93. fileInputStream.close();
  94. } catch (IOException e) {
  95. e.printStackTrace();
  96. }
  97. }
  98.  
  99. public static void read(String path, OnResult dataTask) {
  100. File fileDir = new File(path);
  101. try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileDir), "UTF8"))) {
  102. String line;
  103. while ((line = br.readLine()) != null) {
  104. dataTask.onSuccess(line);
  105. }
  106. } catch (IOException e) {
  107. e.printStackTrace();
  108. }
  109. }
  110.  
  111. public static ArrayList<String> readToList(String path) {
  112. ArrayList<String> list = new ArrayList<>();
  113. read(path, list::add);
  114. return list;
  115. }
  116.  
  117. public static ArrayList<String> readResourceToList(String path) {
  118. ArrayList<String> list = new ArrayList<>();
  119. readResource(path, list::add);
  120. return list;
  121. }
  122.  
  123. public static void appendFile(String path, String data) {
  124. BufferedWriter bw = null;
  125. FileWriter fw = null;
  126. try {
  127. File file = new File(path);
  128. if (!file.exists()) {
  129. file.createNewFile();
  130. }
  131. fw = new FileWriter(file.getAbsoluteFile(), true);
  132. bw = new BufferedWriter(fw);
  133. bw.write(data);
  134. } catch (IOException e) {
  135. e.printStackTrace();
  136. } finally {
  137. try {
  138. if (bw != null) bw.close();
  139. if (fw != null) fw.close();
  140. } catch (IOException ex) {
  141. ex.printStackTrace();
  142. }
  143. }
  144. }
  145.  
  146. public static boolean isExist(String path) {
  147. return new File(path).exists();
  148. }
  149.  
  150. public static boolean isDir(String path) {
  151. return new File(path).isDirectory();
  152. }
  153.  
  154. public static boolean delete(String path) {
  155. File file = new File(path);
  156. return file.delete();
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement