サンプル集  >  Java  >  ファイル読み込みと文字コードA
ファイル読み込みと文字コードA
2009/03/05

No.132 の文字コードを、引数で指定するようにしました。 また、変換結果をファイルに出力する機能も追加しました。

◆環境
OS Windows XP Professional Version 2002 Service Pack 2
J2SE SDK 1.6.0_01

fileByteRead2.java
 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: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileOutputStream;

/**
 * ファイルをバイト単位に読み込む
 * @author ymlib.com
 */

public class fileByteRead2
{
    public static void main( String args[] )
        throws FileNotFoundException
             , IOException
    {
        File fin = new File( args[0] );
        System.out.println( "file size(byte): " + fin.length() );

        FileInputStream is = new FileInputStream( fin );

        byte[] buf = new byte[( int )fin.length()];
        int ret = is.read( buf );

        System.out.println( "◆変換前" );
        dsp( buf );

        System.out.println(
            "-----------------------------------------------" );
        System.out.println( "◆変換後" );
        buf = new String( buf, args[1] ).getBytes( args[2] );
        dsp( buf );

        is.close();

        File fout = new File( args[0] + "." + args[2] );
        FileOutputStream io = new FileOutputStream( fout );
        io.write( buf );
        io.close();
    }

    private static void dsp( byte[] buf )
    {
        for ( int i = 0; i < buf.length; i++ )
        {
            if ( i % 16 == 15 )
            {
                System.out.println(
                    Integer.toHexString( buf[i] & 0xff ) );
            }
            else
            {
                System.out.print(
                    Integer.toHexString( buf[i] & 0xff ) + " " );
            }
        }
    }
}

SJISで、数字、英字、半角カナなどが混在したデータを作成します。

SJIS.dat
1: 
12345ABCDEfghijアイウエオかきくけこサイスセソ東西南北

SJIS から Cp930 に変換してみます。 Cp930 は「UDC 4370 文字を含む日本語カタカナ漢字、5026 のスーパーセット」、「x-IBM930」です。

◆実行結果
>java -cp . fileByteRead2 SJIS.dat SJIS Cp930
file size(byte): 48
◆変換前
31 32 33 34 35 41 42 43 44 45 66 67 68 69 6a b1
b2 b3 b4 b5 82 a9 82 ab 82 ad 82 af 82 b1 83 54
83 43 83 58 83 5a 83 5c 93 8c 90 bc 93 ec 96 6b
-----------------------------------------------
◆変換後
f1 f2 f3 f4 f5 c1 c2 c3 c4 c5 67 68 69 71 72 81
82 83 84 85 e 44 86 44 87 44 88 44 89 44 8a 43
8c 43 82 43 8e 43 8f 43 90 45 57 45 58 45 59 45
5a f

出力された「SJIS.dat.Cp930」を SJIS に戻してみます。

>java -cp . fileByteRead2 SJIS.dat.Cp930 Cp930 SJIS
file size(byte): 50
◆変換前
f1 f2 f3 f4 f5 c1 c2 c3 c4 c5 67 68 69 71 72 81
82 83 84 85 e 44 86 44 87 44 88 44 89 44 8a 43
8c 43 82 43 8e 43 8f 43 90 45 57 45 58 45 59 45
5a f -----------------------------------------------
◆変換後
31 32 33 34 35 41 42 43 44 45 66 67 68 69 6a b1
b2 b3 b4 b5 82 a9 82 ab 82 ad 82 af 82 b1 83 54
83 43 83 58 83 5a 83 5c 93 8c 90 bc 93 ec 96 6b

元通りになりました。

▲ PageTop  ■ Home


Copyright (C) 2012 ymlib.com