Monday, October 12, 2020

Java Basic Util - FileUtil

 package basic.util;


import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.util.Properties;


import basic.util.exception.BaseException;

import basic.util.exception.BusinessException;


public class FileUtil {


public static void savePropertiesToFile(Properties prop, String path) {

ensureFolderExist(path);

try (OutputStream output = new FileOutputStream(path)) {

prop.store(output, null);

} catch (IOException exception) {

BaseException.throwException(BaseException.NotLoaded, exception, "Setting File", path);

}

}


public static void appendPropertiesToFile(Properties prop, String path) {

Properties propTosave = loadFileToProperties(path);

for (String key : prop.stringPropertyNames()) {

propTosave.setProperty(key, prop.getProperty(key));

}


savePropertiesToFile(propTosave, path);

}


public static Properties loadFileToProperties(String path) {

Properties prop = new Properties();

try (InputStream input = new FileInputStream(path)) {

prop.load(input);

} catch (IOException exception) {

BaseException.throwException(BusinessException.NotLoaded, exception, "Setting File", path);

}

return prop;

}


public static boolean isFileExist(String path) {

File file = new File(path);

return file.exists();

}


public static String readFile(String path) {

ensureFileExist(path);

StringBuilder builder = new StringBuilder();

try (BufferedReader br = new BufferedReader(new FileReader(path))) {

String line = br.readLine();

while (line != null) {

builder.append(line);

builder.append(System.lineSeparator());

line = br.readLine();

}

} catch (FileNotFoundException exception) {

BaseException.throwException(BaseException.NotFound, exception, "Setting File", path);

} catch (IOException exception) {

BaseException.throwException(BaseException.NotLoaded, exception, "Setting File", path);

}


return builder.toString();


}


public static void saveFile(String content, String path) {

ensureFolderExist(path);

try (OutputStreamWriter pw = new OutputStreamWriter(new FileOutputStream(path))) {

pw.write(content);

} catch (FileNotFoundException exception) {

BaseException.throwException(BaseException.NotFound, "Setting File", path);

} catch (IOException exception) {

BaseException.throwException(BaseException.Failed, exception, "Save file", "Setting File", path);

}

}

public static void appendFile(String content, String path) {

ensureFileExist(path);

try (OutputStreamWriter pw = new OutputStreamWriter(new FileOutputStream(path, true))) {

pw.write(content);

} catch (FileNotFoundException exception) {

BaseException.throwException(BaseException.NotFound, exception, "Setting File", path);

} catch (IOException exception) {

BaseException.throwException(BaseException.Failed, exception,"Save file", "Setting File", path);

}

}

public static String readJsonFile(String path) {

String content = readFile(path);

        return "[" + content + "]";

}

public static void saveJsonFile(String content, String path) {

if (StringUtil.isAnyEmpty(content)) return;

saveFile(removeBracket(content), path);

}

public static void appendJsonFile(String content, String path) {

if (StringUtil.isAnyEmpty(content)) return;

File file = new File(path);

if (file.length() > 0) appendFile("," + removeBracket(content), path);

else appendFile(removeBracket(content), path);

}


private static String removeBracket(String value) {

String result = value;

if (result.substring(0, 1).equals("["))

result=result.substring(1);

if (result.substring(result.length() - 1, result.length()).equals("]"))

result=result.substring(0, result.length() - 1);

return result;

}

public static void ensureFolderExist(String path) {

File file = new File(path);

if (!file.getParentFile().exists()) {

file.getParentFile().mkdirs();

}

}


public static void ensureFileExist(String path) {

File file = new File(path);

if (!file.getParentFile().exists()) {

file.getParentFile().mkdirs();

}


if (!file.exists()) {

try {

file.createNewFile();

} catch (IOException exception) {

BaseException.throwException(exception);

}

}

}


public static File getFile(String path) {

ensureFolderExist(path);


File file = new File(path);

if (!file.exists()) {

try {

file.createNewFile();

} catch (IOException exception) {

BaseException.throwException(BaseException.Failed, "Create", "File", path);

}

}

return file;

}

public static void main(String[] args) {

ensureFileExist("C:/abcd/aa.txt");

}

}


No comments:

Post a Comment