package sample;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class Hello {

	public static void main(String[] args) {

		
		String str03 = "0123456789abcdef";
		// int i = 0;
		try {
			// String byte[];
			byte[] bytes = new byte[1024];
			bytes = str03.getBytes("UTF-8"); // String.getBytes();orString.getBytes(encoding);

			// ǂݍރt@C̖O
			String inputFileName = "input.dat";

			// ރt@C̖O
			String outputFileName = "output.dat";

			// t@CIuWFNg̐
			File inputFile = new File(inputFileName);
			File outputFile = new File(outputFileName);
			// ̓Xg[̐

			FileInputStream fis = new FileInputStream(inputFile);
			BufferedInputStream bis = new BufferedInputStream(fis);

			// o̓Xg[̐
			FileOutputStream fos = new FileOutputStream(outputFile);
			BufferedOutputStream bos = new BufferedOutputStream(fos);

			// t@Cւ̓ǂݏ
			int len = 0;
			while ((len = bis.read(bytes)) != -1) {
				bos.write(bytes, 0, len);
			}

			// n
			bos.flush();
			bos.close();
			bis.close();

		} catch (UnsupportedEncodingException e) {
			// TODO&#33258;&#21205;&#29983;&#25104;&#12373;&#12428;&#12383;catch&#12502;&#12525;&#12483;&#12463;
			e.printStackTrace();
		}
	
		
		
		
		
		
		
		if(false){
		
		String str02 = "0123456789abcdef";
		int i = 0;
		try {
			// String byte[];
			byte[] bytes = str02.getBytes("UTF-8"); // String.getBytes();orString.getBytes(encoding);

			for (int k = 0; k < 16; k++) {

				switch (bytes[i]) {
				case 48: // 0
					bytes[i] = 0x00;
					break;
				case 49: // 1
					bytes[i] = 0x01;
					break;
				case 50: // 2
					bytes[i] = 0x02;
					break;
				case 51: // 3
					bytes[i] = 0x03;
					break;
				case 52: // 4
					bytes[i] = 0x04;
					break;
				case 53: // 5
					bytes[i] = 0x05;
					break;
				case 54: // 6
					bytes[i] = 0x06;
					break;
				case 55: // 7
					bytes[i] = 0x07;
					break;
				case 56: // 8
					bytes[i] = 0x08;
					break;
				case 57: // 9
					bytes[i] = 0x09;
					break;
				case 97: // a
					bytes[i] = 0x0a;
					break;
				case 98: // b
					bytes[i] = 0x0b;
					break;
				case 99: // c
					bytes[i] = 0x0c;
					break;
				case 100: // d
					bytes[i] = 0x0d;
					break;
				case 101: // e
					bytes[i] = 0x0e;
					break;
				case 102: // f
					bytes[i] = 0x0f;
					break;
				}
				i++;
			}

			// String xx = new String(bytes, "UTF-8");
			// System.out.println(xx);
			System.out.println(bytes);
		} catch (UnsupportedEncodingException e) {
			// TODO&#33258;&#21205;&#29983;&#25104;&#12373;&#12428;&#12383;catch&#12502;&#12525;&#12483;&#12463;
			e.printStackTrace();
		}

		// writer
		try {
			File file = new File("c:\\tmp\\test.txt");

			if (checkBeforeWritefile(file)) {
				FileWriter filewriter = new FileWriter(file);

				filewriter.write("ɂ\r\n");
				filewriter.write("Cł\r\n");
				filewriter.write(str02);

				filewriter.close();
			} else {
				System.out.println("t@Cɏ߂܂");
			}
		} catch (IOException e) {
			System.out.println(e);
		}
		}
		if (false) {
			try {
				File csv = new File("sample.csv");
				BufferedReader br = new BufferedReader(new FileReader(csv));

				String line = "";
				while ((line = br.readLine()) != null) {
					// StringTokenizer st = new StringTokenizer(line, ",");
					// byte[] st = "".getBytes(Charset.forName("UTF-8"));

					// while(st.hasMoreTokens()){
					// System.out.print(st.nextToken() + "\t");
					// }
					System.out.println();
				}
				br.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		

		if (false) {
			// TODO ꂽ\bhEX^u
			String str = new String();
			for (int j = 0; j < 10; j++) {
				// System.out.println("Hello, java\n");
				str += "قق\n";
			}
			System.out.println(str);
		}

	private static boolean checkBeforeWritefile(File file) {
		if (file.exists()) {
			if (file.isFile() && file.canWrite()) {
				return true;
			}
		}

		return false;
	}

}
