From d60c3db582e164a6f6c7e95d73f4aaf92291f88d Mon Sep 17 00:00:00 2001 From: Robert Vokac Date: Mon, 24 Jul 2023 17:33:27 +0200 Subject: [PATCH] Added new Java class FileUtils --- power-io/src/main/java/module-info.java | 1 + .../powerframework/io/utils/FileUtils.java | 71 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 power-io/src/main/java/org/nanoboot/powerframework/io/utils/FileUtils.java diff --git a/power-io/src/main/java/module-info.java b/power-io/src/main/java/module-info.java index a65516b..b4e4457 100644 --- a/power-io/src/main/java/module-info.java +++ b/power-io/src/main/java/module-info.java @@ -33,4 +33,5 @@ module powerframework.io { requires java.logging; requires powerframework.utils; exports org.nanoboot.powerframework.io.bit.base64; + exports org.nanoboot.powerframework.io.utils; } 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 new file mode 100644 index 0000000..6d9a026 --- /dev/null +++ b/power-io/src/main/java/org/nanoboot/powerframework/io/utils/FileUtils.java @@ -0,0 +1,71 @@ + +/////////////////////////////////////////////////////////////////////////////////////////////// +// power-framework: Java library with many purposes of usage. +// Copyright (C) 2023-2023 the original author or authors. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; +// version 2.1 of the License only. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// 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; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +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(); +} + + +}