import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import java.io.InputStreamReader;

public class XXExample
{
	private static final Logger LOGGER = LogManager.getLogger(XXExample.class);
	public static String FILE_PATH =("E:\\SkyDrive\\cheer\\abc\\hallo.txt");
	public static String NEW_FILE_PATH =("E:\\SkyDrive\\cheer\\BCD\\BOX.txt");
	public static void main(String[] args) 
	{
		v1Read();
		//v2Read();
		//v1Write();
		//v2Write();
		//v3Read();
		//v4Read();
	}

	public static void v1Read()
	{
		InputStream in = null;
		try
		{
			in = new FileInputStream(FILE_PATH);
			int result = 0;
			StringBuffer sb = new StringBuffer();
			while ((result = in.read()) !=-1)
			{
				sb.append((char)result);
			}
			LOGGER.info("读取到{}", sb.toString());
		}
		catch(FileNotFoundException e)
		{
			LOGGER.catching(e);
		}
		catch(IOException e)
		{
			LOGGER.catching(e);
		}
		finally
		{
			try
			{
				if(in != null)
				{
					in.close();
				}
			}
			catch(IOException e)
			{
				LOGGER.catching(e);
			}
		}
	}

	public static void v2Read()
	{
		InputStream in = null;
		try
		{
			in = new FileInputStream(FILE_PATH);
			int len = 0;
			// 每次成功读取的元素个数
			byte[] buf = new byte[5];
			StringBuffer sb = new StringBuffer();
			while((len = in.read(buf)) != -1)
			{
				sb.append(new String(buf, 0, len));
			}
			LOGGER.info("读写到{}", sb.toString());
		}
		catch(FileNotFoundException e)
		{
			LOGGER.catching(e);
		}
		catch(IOException e)
		{
			LOGGER.catching(e);
		}
		finally
		{
			try
			{
				if(in != null)
				{
					in.close();
				}
			}
			catch(IOException e)
			{
				LOGGER.catching(e);
			}
		}
	}

	public static void v1Write()
	{
		OutputStream out = null;
		try
		{
			out = new FileOutputStream(NEW_FILE_PATH);
			out.write('a');
			LOGGER.info("写入成功");
		}
		catch(FileNotFoundException e)
		{
			LOGGER.catching(e);
		}
		catch(IOException e)
		{
			LOGGER.catching(e);
		}
		finally
		{
			try
			{
				if(out != null)
				{
					out.close();
				}
			}
			catch(IOException e)
			{
				LOGGER.catching(e);
			}
		}
	}

	public static void v2Write()
	{
		OutputStream out = null;
		try
		{
			out = new FileOutputStream(NEW_FILE_PATH);
			int len = 0;
			byte[] bytes = {50, 36, 45, 68};
			out.write(bytes);
			LOGGER.info("写入成功");

		}
		catch(FileNotFoundException e)
		{
			LOGGER.catching(e);
		}
		catch(IOException e)
		{
			LOGGER.catching(e);
		}
		finally
		{
			try
			{
				if(out != null)
				{
					out.close();
				}
			}
			catch(IOException e)
			{
				LOGGER.catching(e);
			}
		}
	}


	public static void v3Read()
	{
		InputStream in = null;
		Reader reader = null;
		try
		{
			in = new FileInputStream(FILE_PATH);
			reader = new InputStreamReader(in, "gb2312");
			//reader = new FileReader(FILE_PATH);

			int result = 0;
			while((result = reader.read()) != -1)
			{
				LOGGER.info("读取到:{}", (char)result);
			}	
		}
		catch(FileNotFoundException e)
		{
			LOGGER.catching(e);
		}
		catch(IOException e)
		{
			LOGGER.catching(e);
		}
		finally
		{
			try
			{
				if(null != reader)
				{
					reader.close();
				}
			}
			catch(IOException e)
			{
				LOGGER.catching(e);
			}
		}

	}

	// 每次读一个字符数组
	public static void v4Read()
	{
		Reader reader = null;
		try
		{
			//in = new FileInputStream(FILE_PATH);
			//reader = new InputStreamReader(in, "gb2312");
			reader = new FileReader(FILE_PATH);

			int len = 0;
			char[] buf = new char[10];
			StringBuffer sb = new StringBuffer();
			
			while((len = reader.read(buf)) != -1)
			{
				sb.append(new String(buf, 0, len));
			}
			LOGGER.info("读取到:{}", sb.toString());
		}
		catch(FileNotFoundException e)
		{
			LOGGER.catching(e);
		}
		catch(IOException e)
		{
			LOGGER.catching(e);
		}
		finally
		{
			try
			{
				if(null != reader)
				{
					reader.close();
				}
			}
			catch(IOException e)
			{
				LOGGER.catching(e);
			}
		}

	}
}
