DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
DreamFile.java
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 Dreamfire Studio
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24package com.dreamfirestudios.dreamcore.DreamFile;
25
26import java.io.*;
27import java.nio.file.Files;
28import java.nio.file.StandardCopyOption;
29import java.util.ArrayList;
30import java.util.List;
31
32public class DreamFile {
33
39 public static void DeleteFile(File sourceFile) {
40 if (!sourceFile.exists()) return;
41 sourceFile.delete();
42 }
43
52 public static void CreateFile(String directory, String sourceFile) throws IOException {
53 DreamDir.createDirectory(new File(directory));
54 var newFile = new File(directory + "/" + sourceFile);
55 newFile.createNewFile();
56 }
57
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());
68 }
69
77 public static void WriteFile(File file, List<String> lines) throws IOException {
78 Files.write(file.toPath(), lines);
79 }
80
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());
93 }
94 }
95 }
96
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);
107 }
108
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);
119 }
120
127 public static long GetFileSize(File file) {
128 if (!file.exists()) return 0;
129 return file.length();
130 }
131
138 public static long GetLastModified(File file) {
139 if (!file.exists()) return 0;
140 return file.lastModified();
141 }
142
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)) {
154 return file;
155 }
156 }
157 return null;
158 }
159
166 public static List<File> ListFiles(File directory) {
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);
171 }
172 return files;
173 }
174
182 public static String ReadFileAsString(File file) throws IOException {
183 if (!file.exists()) throw new FileNotFoundException("File not found: " + file.getPath());
184 return new String(Files.readAllBytes(file.toPath()));
185 }
186
194 public static void WriteFileFromString(File file, String content) throws IOException {
195 Files.write(file.toPath(), content.getBytes());
196 }
197}
static void createDirectory(File directory)
Creates the specified directory if it does not exist.
Definition DreamDir.java:65
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).