Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package utils;
- import interfaces.OnResult;
- import java.io.*;
- import java.net.URISyntaxException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.nio.file.StandardOpenOption;
- import java.util.ArrayList;
- public class FileUtils {
- private static int index = 0;
- private static int count = 0;
- public static void split(String folder, String dataFile, int quantity) {
- read(folder + dataFile, data -> {
- String newName = dataFile;
- if (newName.contains(".")) {
- newName = newName.split("\\.")[0];
- }
- appendFile(folder + newName + "-" + index + ".dat", data + "\n");
- if (count % 2 == 1) {
- if (++index == quantity) index = 0;
- }
- count++;
- });
- }
- public static long countLines(String path) {
- long count = 0;
- File fileDir = new File(path);
- try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileDir), "UTF8"))) {
- while (br.readLine() != null) {
- count++;
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return count;
- }
- public static void listFiles(String path, OnResult onResult) {
- File folder = new File(path);
- File[] listOfFiles = folder.listFiles();
- if (listOfFiles != null) {
- for (File file : listOfFiles) {
- if (file.isFile()) {
- onResult.onSuccess(file.getName());
- }
- }
- } else {
- Logs.infoLn("List files null");
- }
- }
- public static void append(String path, Object data) {
- try {
- File f = new File(path);
- if (!f.exists()) {
- f.createNewFile();
- }
- Files.write(Paths.get(path), data.toString().getBytes(), StandardOpenOption.APPEND);
- } catch (IOException e) {
- Logs.infoLn(e.getMessage());
- }
- }
- public static File getFileResource(String path) {
- try {
- return new File(ClassLoader.getSystemResource(path).toURI());
- } catch (URISyntaxException e) {
- e.printStackTrace();
- return null;
- }
- }
- public static void write(String path, String message) {
- try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "UTF8"))) {
- bw.write(message);
- bw.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void readResource(String path, OnResult onResult) {
- InputStream fileInputStream = ClassLoader.getSystemResourceAsStream(path);
- try (BufferedReader br = new BufferedReader(new InputStreamReader(fileInputStream, "UTF8"))) {
- String line;
- while ((line = br.readLine()) != null) onResult.onSuccess(line);
- br.close();
- fileInputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void read(String path, OnResult dataTask) {
- File fileDir = new File(path);
- try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileDir), "UTF8"))) {
- String line;
- while ((line = br.readLine()) != null) {
- dataTask.onSuccess(line);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static ArrayList<String> readToList(String path) {
- ArrayList<String> list = new ArrayList<>();
- read(path, list::add);
- return list;
- }
- public static ArrayList<String> readResourceToList(String path) {
- ArrayList<String> list = new ArrayList<>();
- readResource(path, list::add);
- return list;
- }
- public static void appendFile(String path, String data) {
- BufferedWriter bw = null;
- FileWriter fw = null;
- try {
- File file = new File(path);
- if (!file.exists()) {
- file.createNewFile();
- }
- fw = new FileWriter(file.getAbsoluteFile(), true);
- bw = new BufferedWriter(fw);
- bw.write(data);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (bw != null) bw.close();
- if (fw != null) fw.close();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
- public static boolean isExist(String path) {
- return new File(path).exists();
- }
- public static boolean isDir(String path) {
- return new File(path).isDirectory();
- }
- public static boolean delete(String path) {
- File file = new File(path);
- return file.delete();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement