Added new Java class FileUtils 2
This commit is contained in:
parent
d60c3db582
commit
3321ba3981
@ -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<String> 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<String> readLinesFromFile(File file) {
|
||||
return readLinesFromFile(file, false);
|
||||
}
|
||||
|
||||
public static List<String> 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user