Added new Java class FileUtils 2

This commit is contained in:
Robert Vokac 2023-07-24 17:43:24 +02:00
parent d60c3db582
commit 3321ba3981
No known key found for this signature in database
GPG Key ID: 693D30BEE3329055

View File

@ -1,4 +1,3 @@
/////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////
// power-framework: Java library with many purposes of usage. // power-framework: Java library with many purposes of usage.
// Copyright (C) 2023-2023 the original author or authors. // 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 // License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
/////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////
package org.nanoboot.powerframework.io.utils; package org.nanoboot.powerframework.io.utils;
import java.io.File; import java.io.File;
@ -35,9 +33,11 @@ import java.util.stream.Collectors;
* @author robertvokac * @author robertvokac
*/ */
public class FileUtils { public class FileUtils {
private FileUtils() { private FileUtils() {
//Not meant to be instantiated. //Not meant to be instantiated.
} }
public static String readTextFromFile(File file) { public static String readTextFromFile(File file) {
if (!file.exists()) { if (!file.exists()) {
return ""; return "";
@ -47,14 +47,20 @@ public class FileUtils {
} catch (IOException ex) { } catch (IOException ex) {
throw new RuntimeException("Reading file failed: " + file.getName(), ex); throw new RuntimeException("Reading file failed: " + file.getName(), ex);
} }
} }
public static List<String> readLinesFromFile(File file) {
public static List<String> readLinesFromFile(File file) {
return readLinesFromFile(file, false);
}
public static List<String> readLinesFromFile(File file, boolean skipEmptyLines) {
String content = readTextFromFile(file); String content = readTextFromFile(file);
String[] lines = content.split("\\r?\\n|\\r"); String[] lines = content.split("\\r?\\n|\\r");
return Arrays.stream(lines).filter(l -> !l.trim().isEmpty()).collect( return Arrays.stream(lines).filter(l -> skipEmptyLines ? !l.trim().isEmpty() : true).collect(
Collectors.toList()); Collectors.toList());
} }
public static void writeTextToFile(String text, File file) {
public static void writeTextToFile(String text, File file) {
FileWriter fileWriter; FileWriter fileWriter;
try { try {
fileWriter = new FileWriter(file); fileWriter = new FileWriter(file);
@ -65,7 +71,6 @@ public static void writeTextToFile(String text, File file) {
PrintWriter printWriter = new PrintWriter(fileWriter); PrintWriter printWriter = new PrintWriter(fileWriter);
printWriter.print(text); printWriter.print(text);
printWriter.close(); printWriter.close();
} }
} }