package com.chess.genesis;

import android.net.Uri;
import android.os.Environment;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.MappedByteBuffer;

final class FileUtils
{
	private FileUtils()
	{
	}

	private static FileInputStream tryOpenFileStream(final String path) throws FileNotFoundException
	{
		FileInputStream file = null;
		boolean error = false;
		String p = new String(path);

		while (p.length() > 0) {
			try {
				file = new FileInputStream(p);
			} catch (FileNotFoundException e) {
				error = true;

				final int i = p.indexOf('/', 1);
				if (i == -1)
					p = new String();
				else
					p = p.substring(i);
			}
			if (!error)
				return file;
			error = false;
		}
		throw new FileNotFoundException();
	}

	public static String readFile(final String path) throws FileNotFoundException, IOException
	{
		final FileInputStream stream = tryOpenFileStream(path);

	try {
		final FileChannel fc = stream.getChannel();
		final MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

		/* Instead of using default, pass in a decoder. */
		return Charset.defaultCharset().decode(bb).toString();
	} finally {
		stream.close();
	}
	}

	public static Uri writeFile(final String filename, final String data) throws FileNotFoundException, IOException
	{
		final String state = Environment.getExternalStorageState();

		if (!Environment.MEDIA_MOUNTED.equals(state))
			throw new FileNotFoundException();

		final File dir = Environment.getExternalStorageDirectory();
		final File file = new File(dir, filename);
		final FileOutputStream buffer = new FileOutputStream(file);

		buffer.write(data.getBytes());
		buffer.close();

		return Uri.fromFile(file);
	}
}

@@ -76,16 +76,48 @@
 onment;%0A
+import java.lang.StringBuilder;%0A
 import j
@@ -364,16 +364,42 @@
 eBuffer;
+%0Aimport java.util.Scanner;
 %0A%0Afinal 
@@ -1126,17 +1126,16 @@
 (path);%0A
-%0A
 %09try %7B%0A%09
@@ -1145,24 +1145,52 @@
 nal 
-FileChannel fc =
+StringBuilder data = new StringBuilder((int)
  str
@@ -1205,16 +1205,24 @@
 hannel()
+.size())
 ;%0A%09%09fina
@@ -1227,181 +1227,129 @@
 nal 
-MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.siz
+Scanner scanner = new Scanner(stream);%0A%0A%09%09while (scanner.hasNextLin
 e())
-;%0A
 %0A%09%09
-/* Instead of using default, pass in a decoder. */%0A%09%09return Charset.defaultCharset().decode(bb)
+%09data.append(scanner.nextLine());%0A%09%09return data
 .toS
@@ -1370,16 +1370,34 @@
 nally %7B%0A
+%09%09// close stream%0A
 %09%09stream

change FileUtils.readFile to use the Scanner class