1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.io.*;

public class Chars {
    public static void main(String[] args) {
        try {
            String s="こんにちは\\";
            for(byte b:sjis2eucjp(s.getBytes("SJIS"))) System.out.printf("%x ",b);
            
            file(new File("d:\\sjis.txt"),new File("d:\\eucjp.txt"));
        } catch (Exception e) {e.printStackTrace();}
    }
    
    //Shift_JISのバイト列をEUC-JPのバイト列に変換して返す関数
    static public byte[] sjis2eucjp(byte[] sjis) throws Exception{
        return (new String(sjis,"SJIS")).getBytes("EUC-JP");
    }
    
    //Shift_JISで書かれたファイルをEUC-JPに変換してファイル出力
    static public void file(File sjis,File eucjp) throws Exception{
        //効率を上げたい場合はBufferedReader等でラップする
        Reader in=new InputStreamReader(new FileInputStream(sjis),"SJIS");
        Writer out=new OutputStreamWriter(new FileOutputStream(eucjp),"EUC-JP");
        
        int c;
        while((c=in.read())!=-1) out.write(c);
        
        out.close();
        in.close();
    }
}