This repository has been archived by the owner on Jan 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e1d4bbc
commit 9d59ca5
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/main/java/org/auioc/mods/ahutils/utils/java/FileUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.auioc.mods.ahutils.utils.java; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import org.auioc.mods.ahutils.utils.LogUtil; | ||
|
||
public interface FileUtils { | ||
|
||
static Path getOrCreateDirectory(String directoryName) throws Exception { | ||
Path path = Paths.get(directoryName); | ||
if (!path.toFile().exists()) { | ||
try { | ||
Files.createDirectory(path); | ||
LogUtil.warn(LogUtil.getMarker("FileUtils"), "Folder " + path.toAbsolutePath() + " does not exist, created automatically."); | ||
} catch (final IOException e) { | ||
LogUtil.error(LogUtil.getMarker("FileUtils"), "Could not create directory!", e); | ||
throw new Exception("Could not create directory " + path); | ||
} | ||
} | ||
return path; | ||
} | ||
|
||
static File writeText(String directoryName, String fileName, StringBuffer buffer) throws Exception { | ||
Path path = getOrCreateDirectory(directoryName); | ||
File file = new File(path.toUri().getPath() + "/" + fileName); | ||
if (file.exists()) { | ||
LogUtil.warn(LogUtil.getMarker("FileUtils"), "File " + file + " already exists, overwrite."); | ||
} | ||
try { | ||
final BufferedWriter writer = new BufferedWriter(new FileWriter(file, false)); | ||
writer.write(buffer.toString()); | ||
writer.close(); | ||
} catch (final Exception e) { | ||
LogUtil.error(LogUtil.getMarker("FileUtils"), "Cannot write data to file!", e); | ||
throw new Exception("Cannot write data to file " + file); | ||
} | ||
return file; | ||
} | ||
|
||
} |