package heufybot.utils;

import heufybot.core.Logger;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileUtils 
{
	public static String readFile(String filePath) 
	{
		try 
		{
			FileInputStream fstream = new FileInputStream(filePath);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String result = "";
			String read = "";
			while ((read = br.readLine()) != null) 
			{
				result += read += "\n";
			}
			in.close();
			return result;
		} 
		catch (FileNotFoundException e) 
		{
			Logger.error("FileUtils", "The file " + filePath + " does not exist.");
			return null;
		}
		catch (IOException e) 
		{
			Logger.error("FileUtils", "The file " + filePath + " could not be read.");
			return null;
		}
	}
	
	public static boolean writeFile(String filePath, String text) 
	{
		File file = new File(filePath);
		if (file.exists()) 
		{
			file.delete();
		}
		FileWriter writer;
		try 
		{
			writer = new FileWriter(filePath, false);
			BufferedWriter bw = new BufferedWriter(writer);
			bw.write(text);
			bw.flush();
			bw.close();
			return true;
		} 
		catch (IOException e) 
		{
			Logger.error("FileUtils", "The file " + filePath + " could not be written.");
			return false;
		}
	}
	
	public static boolean writeFileAppend(String filePath, String text)
	{
		FileWriter writer;
		try 
		{
			writer = new FileWriter(filePath, true);
			BufferedWriter bw = new BufferedWriter(writer);
			bw.write(text);
			bw.flush();
			bw.close();
			return true;
		} 
		catch (IOException e) 
		{
			Logger.error("FileUtils", "The file " + filePath + " could not be written.");
			return false;
		}
	}
	
	public static boolean touchFile(String filePath)
	{
		File file = new File(filePath);
		try 
		{
			return file.createNewFile();
		}
		catch (IOException e)
		{
			Logger.error("FileUtils", "The file " + filePath + " could not be written.");
			return false;
		}
	}
	
	public static void touchDir(String filePath)
	{
		File file = new File(filePath);
		if(!file.exists())
		{
			file.mkdir();
		}
	}
	
	public static boolean deleteFile(String filePath)
	{
		File file = new File(filePath);
		return file.delete();
	}
	
	public static boolean fileExists(String filePath)
	{
		File file = new File(filePath);
		return file.exists();
	}
}

@@ -252,22 +252,28 @@
 .io.File
-Writer
+OutputStream
 ;%0Aimport
@@ -327,16 +327,84 @@
 mReader;
+%0Aimport java.io.OutputStreamWriter;%0Aimport java.nio.charset.Charset;
 %0A%0Apublic
@@ -1254,80 +1254,165 @@
 %7D%0A%09%09
-FileWriter writer;%0A%09%09try %0A%09%09%7B%0A%09%09%09writer = new FileWriter(filePath, false
+try %0A%09%09%7B%0A%09%09%09FileOutputStream fos = new FileOutputStream(filePath, false);%0A%09%09%09OutputStreamWriter writer = new OutputStreamWriter(fos, Charset.forName(%22UTF-8%22)
 );%0A%09
@@ -1745,79 +1745,164 @@
 %7B%0A%09%09
-FileWriter writer;%0A%09%09try %0A%09%09%7B%0A%09%09%09writer = new FileWriter(filePath, true
+try %0A%09%09%7B%0A%09%09%09FileOutputStream fos = new FileOutputStream(filePath, true);%0A%09%09%09OutputStreamWriter writer = new OutputStreamWriter(fos, Charset.forName(%22UTF-8%22)
 );%0A%09

Use UTF-8 encoding when logging