24package com.dreamfirestudios.dreamcore.DreamFile;
27import java.nio.file.Files;
28import java.nio.file.StandardCopyOption;
29import java.util.ArrayList;
40 if (!sourceFile.exists())
return;
52 public static void CreateFile(String directory, String sourceFile)
throws IOException {
54 var newFile =
new File(directory +
"/" + sourceFile);
55 newFile.createNewFile();
65 public static List<String>
ReadFile(File file)
throws IOException {
66 if (!file.exists())
throw new FileNotFoundException(
"File not found: " + file.getPath());
67 return Files.readAllLines(file.toPath());
77 public static void WriteFile(File file, List<String> lines)
throws IOException {
78 Files.write(file.toPath(), lines);
88 public static void AppendToFile(File file, List<String> lines)
throws IOException {
89 if (!file.exists()) file.createNewFile();
90 try (FileWriter writer =
new FileWriter(file,
true)) {
91 for (String line : lines) {
92 writer.write(line + System.lineSeparator());
104 public static void CopyFile(File sourceFile, File destinationFile)
throws IOException {
105 if (!sourceFile.exists())
throw new FileNotFoundException(
"Source file not found: " + sourceFile.getPath());
106 Files.copy(sourceFile.toPath(), destinationFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
116 public static void MoveFile(File sourceFile, File destinationFile)
throws IOException {
117 if (!sourceFile.exists())
throw new FileNotFoundException(
"Source file not found: " + sourceFile.getPath());
118 Files.move(sourceFile.toPath(), destinationFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
128 if (!file.exists())
return 0;
129 return file.length();
139 if (!file.exists())
return 0;
140 return file.lastModified();
150 public static File
SearchFile(File directory, String fileName) {
151 if (!directory.exists() || !directory.isDirectory())
return null;
152 for (File file : directory.listFiles()) {
153 if (file.getName().equalsIgnoreCase(fileName)) {
167 List<File> files =
new ArrayList<>();
168 if (!directory.exists() || !directory.isDirectory())
return files;
169 for (File file : directory.listFiles()) {
170 if (file.isFile()) files.add(file);
183 if (!file.exists())
throw new FileNotFoundException(
"File not found: " + file.getPath());
184 return new String(Files.readAllBytes(file.toPath()));
195 Files.write(file.toPath(), content.getBytes());
static void createDirectory(File directory)
Creates the specified directory if it does not exist.
static void CopyFile(File sourceFile, File destinationFile)
Copies a file to a new location.
static void MoveFile(File sourceFile, File destinationFile)
Moves a file to a new location.
static void WriteFile(File file, List< String > lines)
Writes the specified lines to a file.
static List< String > ReadFile(File file)
Reads all lines from a file and returns them as a List of strings.
static String ReadFileAsString(File file)
Reads a file as a single string.
static File SearchFile(File directory, String fileName)
Searches for a file by name within a specified directory (non-recursive).
static void WriteFileFromString(File file, String content)
Writes a single string to a file.
static void CreateFile(String directory, String sourceFile)
Creates a new file at the specified directory.
static void DeleteFile(File sourceFile)
Deletes the specified file if it exists.
static long GetFileSize(File file)
Returns the size of a file in bytes.
static void AppendToFile(File file, List< String > lines)
Appends the specified lines to a file.
static long GetLastModified(File file)
Retrieves the last modified timestamp of a file.
static List< File > ListFiles(File directory)
Lists all files in a directory (non-recursive).