From 3321ba3981b2005f474983ec43ecbc216fdf0b0e Mon Sep 17 00:00:00 2001 From: Robert Vokac Date: Mon, 24 Jul 2023 17:43:24 +0200 Subject: [PATCH] Added new Java class FileUtils 2 --- .../powerframework/io/utils/FileUtils.java | 67 ++++++++++--------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/power-io/src/main/java/org/nanoboot/powerframework/io/utils/FileUtils.java b/power-io/src/main/java/org/nanoboot/powerframework/io/utils/FileUtils.java index 6d9a026..a813a4d 100644 --- a/power-io/src/main/java/org/nanoboot/powerframework/io/utils/FileUtils.java +++ b/power-io/src/main/java/org/nanoboot/powerframework/io/utils/FileUtils.java @@ -1,4 +1,3 @@ - /////////////////////////////////////////////////////////////////////////////////////////////// // power-framework: Java library with many purposes of usage. // Copyright (C) 2023-2023 the original author or authors. @@ -17,7 +16,6 @@ // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA /////////////////////////////////////////////////////////////////////////////////////////////// - package org.nanoboot.powerframework.io.utils; import java.io.File; @@ -35,37 +33,44 @@ import java.util.stream.Collectors; * @author robertvokac */ public class FileUtils { + private FileUtils() { //Not meant to be instantiated. } - public static String readTextFromFile(File file) { - if (!file.exists()) { - return ""; - } - try { - return new String(Files.readAllBytes(Paths.get(file.getAbsolutePath()))); - } catch (IOException ex) { - throw new RuntimeException("Reading file failed: " + file.getName(), ex); - } -} -public static List readLinesFromFile(File file) { - String content = readTextFromFile(file); - String[] lines = content.split("\\r?\\n|\\r"); - return Arrays.stream(lines).filter(l -> !l.trim().isEmpty()).collect( - Collectors.toList()); -} -public static void writeTextToFile(String text, File file) { - FileWriter fileWriter; - try { - fileWriter = new FileWriter(file); - } catch (IOException ex) { - ex.printStackTrace(); - throw new RuntimeException("Writing to file failed: " + file.getName(), ex); - } - PrintWriter printWriter = new PrintWriter(fileWriter); - printWriter.print(text); - printWriter.close(); -} - + public static String readTextFromFile(File file) { + if (!file.exists()) { + return ""; + } + try { + return new String(Files.readAllBytes(Paths.get(file.getAbsolutePath()))); + } catch (IOException ex) { + throw new RuntimeException("Reading file failed: " + file.getName(), ex); + } + } + + public static List readLinesFromFile(File file) { + return readLinesFromFile(file, false); + } + + public static List readLinesFromFile(File file, boolean skipEmptyLines) { + String content = readTextFromFile(file); + String[] lines = content.split("\\r?\\n|\\r"); + return Arrays.stream(lines).filter(l -> skipEmptyLines ? !l.trim().isEmpty() : true).collect( + Collectors.toList()); + } + + public static void writeTextToFile(String text, File file) { + FileWriter fileWriter; + try { + fileWriter = new FileWriter(file); + } catch (IOException ex) { + ex.printStackTrace(); + throw new RuntimeException("Writing to file failed: " + file.getName(), ex); + } + PrintWriter printWriter = new PrintWriter(fileWriter); + printWriter.print(text); + printWriter.close(); + } + }