package jp.kshoji; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class TextToKeyCode { private static final String keyCodeShiftTable = "~!@#$%^&*()_+{}|:\"<>?"; private static final String keyCodeTable = "`1234567890-=[]\\;',./"; private static final int KEY_SHIFT = 2; private static String readFileToString(File file) throws IOException { byte[] byteArray = new byte[(int) file.length()]; FileInputStream fis = new FileInputStream(file); fis.read(byteArray); fis.close(); return new String(byteArray); } public static void main(String[] args) throws IOException { String toKeyCode = readFileToString(new File("totas.txt")); int indexOfCharAt; ByteArrayOutputStream output = new ByteArrayOutputStream(); for (int i = 0; i < toKeyCode.length(); i++) { byte byte1; byte byte2 = 0; char charAt = toKeyCode.charAt(i); if (charAt >= '0' && charAt <= '9') { byte1 = (byte) charAt; } else if (charAt >= 'a' && charAt <= 'z') { byte1 = (byte) charAt; } else if (charAt >= 'A' && charAt <= 'Z') { byte1 = (byte) (charAt - 'A' + 'a'); byte2 = KEY_SHIFT; } else if ((indexOfCharAt = keyCodeShiftTable.indexOf(charAt)) != -1) { byte1 = (byte) keyCodeTable.charAt(indexOfCharAt); byte2 = KEY_SHIFT; } else if ((indexOfCharAt = keyCodeTable.indexOf(charAt)) != -1) { byte1 = (byte) charAt; } else if (charAt == '\n' || charAt == ' ' || charAt == '\t') { byte1 = (byte) charAt; } else { continue; } output.write(new byte[]{byte1, byte2}); } FileOutputStream fos = new FileOutputStream(new File("tas.txt")); fos.write(output.toByteArray()); fos.flush(); fos.close(); } }