package com.coolerfall.easyutil;

import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class FileUtils {
	private static final String TAG = FileUtils.class.getSimpleName();
	
	/**
	 * Save content into the specified file.
	 * 
	 * @param filename absolute file path
	 * @param content  content
	 * @param mode     the file access mode, either "r", "rw", "rws" or "rwd".
	 */
	public static void write(String filename, String content, String mode) {
		try {
			File file = new File(filename);
			if (file.isDirectory()) {
				return;
			}
			
			File parentFile = file.getParentFile();
			parentFile.mkdirs();
			
			RandomAccessFile raf = new RandomAccessFile(filename, mode);
			byte[] buffer = content.getBytes("UTF-8");
			raf.write(buffer);
			raf.close();
		} catch (IOException e) {
			Log.e(TAG, "save file error: " + e.getMessage());
		}
	}

	/**
	 * Save content into the specified file.
	 *
	 * @param filename    absolute path
	 * @param content     the content to save
	 * @param shouldClear should clear the file first or not
	 */
	public static void write(String filename, String content, boolean shouldClear) {
		if (shouldClear) {
			delete(filename);
		}

		write(filename, content, "rw");
	}
	
	/**
	 * Save content into the specified file.
	 * 
	 * @param filename absolute path
	 * @param content  the content to save
	 */
	public static void write(String filename, String content) {
		write(filename, content, true);
	}

	/**
	 Save content into the specified file.
	 *
	 * @param file    file
	 * @param content the content to save
	 */
	public static void write(File file, String content) {
		write(file.getAbsolutePath(), content, true);
	}
	
	/**
	 * Read content from specified file.
	 * 
	 * @param  filename absolute file path
	 * @return content content
	 */
	public static String read(String filename) {
		try {
			RandomAccessFile raf = new RandomAccessFile(filename, "r");
			int fileLen = (int) raf.length();
			byte[] buffer = new byte[fileLen];
			
			int len = raf.read(buffer);
			raf.close();
			if (len != fileLen) {
				return null;
			}
			
			return new String(buffer);
		} catch (IOException e) {
			Log.e(TAG, "read file error: " + e.getMessage());
		}
		
		return null;
	}
	
	/**
	 * Delete file if existed.
	 * 
	 * @param filename absolute path
	 */
	public static void delete(String filename) {
		File file = new File(filename);
		if (!file.exists()) {
			return;
		}
		
		file.delete();
	}
}

@@ -427,24 +427,72 @@
 %22rwd%22.%0A%09 */%0A
+%09@SuppressWarnings(%22ResultOfMethodCallIgnored%22)%0A
 %09public stat
@@ -2397,24 +2397,72 @@
 e path%0A%09 */%0A
+%09@SuppressWarnings(%22ResultOfMethodCallIgnored%22)%0A
 %09public stat
@@ -2590,11 +2590,729 @@
 te();%0A%09%7D
+%0A%0A%09/**%0A%09 * Get the extension for the specified file.%0A%09 *%0A%09 * @param  filePath file path%0A%09 * @return          extension of file or empty if file has no extension%0A%09 */%0A%09public static String getExtention(String filePath) %7B%0A%09%09return getExtention(new File(filePath));%0A%09%7D%0A%0A%09/**%0A%09 * Get the extension for the specified file.%0A%09 *%0A%09 * @param  file file to get extension%0A%09 * @return      extension of file or empty if file has no extension%0A%09 */%0A%09public static String getExtention(File file) %7B%0A%09%09String ext = %22%22;%0A%0A%09%09if (!file.exists()) %7B%0A%09%09%09return ext;%0A%09%09%7D%0A%0A%09%09String filename = file.getName();%0A%09%09int index = filename.lastIndexOf(%22.%22);%0A%09%09if (index != -1 && index != 0) %7B%0A%09%09%09ext = filename.substring(index+1);%0A%09%09%7D%0A%0A%09%09return ext;%0A%09%7D
 %0A%7D%0A

update FileUtils