### 擖弌椡

[栠傞] [慜偵] [師偵]

#### 仭 擖弌椡

僨乕僞偺 擖弌椡 偵偼懡悢偺僋儔僗偑梡堄偝傟偰偄傑偡丅僋儔僗偺奒憌峔憿傪壓婰偵帵偟傑偡丅InputStream宯僋儔僗偼庡偵僶僀僫儕僨乕僞傪埖偆嵺偵丄Reader宯僋儔僗偼庡偵僥僉僗僩僨乕僞傪埖偆嵺偵梡偄傜傟傑偡丅

    仩 Object
    劌仩 InputStream乮拪徾僋儔僗丗庡偵僶僀僫儕梡乯
    劆劌仩 AudioInputStream
    劆劌仩 ByteArrayInputStream
    劆劌仩 FileInputStream乮僶僀僫儕僼傽僀儖撉傒崬傒乯
    劆劌仩 FilterInputStream
    劆劆劌仩 BufferedInputStream乮旕悇彠乯
    劆劆劌仩 CheckedInputStream
    劆劆劌仩 CipherInputStream
    劆劆劌仩 DataInputStream
    劆劆劌仩 DigestInputStream
    劆劆劌仩 InflaterInputStream
    劆劆劆劌仩 GZIPInputStream
    劆劆劆劋仩 ZipInputStream
    劆劆劆丂劋仩 JarInputStream
    劆劆劌仩 LineNumberInputStream
    劆劆劌仩 ProgressMonitorInputStream
    劆劆劋仩 PushbackInputStream
    劆劌仩 ObjectInputStream
    劆劌仩 PipedInputStream
    劆劌仩 SequenceInputStream
    劆劋仩 StringBufferInputStream
    劋仩 Reader乮拪徾僋儔僗丗庡偵僥僉僗僩梡乯
    丂劌仩 BufferedReader乮1峴偢偮撉傒崬傓乯
    丂劆劋仩 LineNumberReader
    丂劌仩 CharArrayReader
    丂劌仩 FilterReader
    丂劆劋仩 PushbackReader
    丂劌仩 InputStreamReader乮暥帤僐乕僪巜掕偑壜擻乯
    丂劆劋仩 FileReader乮僥僉僗僩僼傽僀儖撉傒崬傒乯
    丂劌仩 PipedReader
    丂劋仩 StringReader

#### 仭 僶僀僫儕僼傽僀儖傪撉傒崬傓乮FileInputStream乯

FileInputStream 偼巜掕偟偨僼傽僀儖傪僶僀僫儕僼傽僀儖偲偟偰撉傒崬傒傑偡丅

仒FileInputStreamTest.java

    import java.io.*;

    class FileInputStreamTest {
        public static void main(String[] args) {
            try {
                FileInputStream in = new FileInputStream("file.txt");
                int ch;
                while ((ch = in.read()) != -1) {
                    System.out.print(Integer.toHexString(ch) + " ");
                }
                in.close();
            } catch (IOException e) {
                System.out.println(e);
            }
        }
    }

僔僼僩JIS偺乽偁偄偆乿偲偄偆撪梕偺僼傽僀儖傪撉傒崬傫偩応崌丄寢壥偼師偺傛偆偵側傝傑偡丅82a0 偼乽偁乿丄82a2 偼乽偄乿丄82a4 偼乽偆乿偺僔僼僩JIS暥帤僐乕僪丄d 偼暅婣僐乕僪乮CR乯丄a 偼夵峴乮LF乯僐乕僪偱偡丅娍帤僐乕僪偑偦偺傑傑丄1僶僀僩偢偮撉傒崬傑傟偰偄傑偡丅

    82 a0 82 a2 82 a4 d a

#### 仭 僥僉僗僩僼傽僀儖傪撉傒崬傓乮InputStreamReader乯

InputStreamReader 偼巜掕偟偨僗僩儕乕儉傪丄巜掕偟偨暥帤僐乕僪偱峔惉偝傟傞僥僉僗僩僼傽僀儖偲偟偰撉傒崬傒傑偡丅

仒InputStreamReaderTest.java

    import java.io.*;

    class InputStreamReaderTest {
        public static void main(String[] args) {
            try {
                FileInputStream is = new FileInputStream("file.txt");
                InputStreamReader in = new InputStreamReader(is, "SJIS");
                int ch;
                while ((ch = in.read()) != -1) {
                    System.out.print(Integer.toHexString(ch) + " ");
                }
                in.close();
            } catch (IOException e) {
                System.out.println(e);
            }
        }
    }

乽偁偄偆乿偲偄偆撪梕偺僼傽僀儖傪撉傒崬傫偩応崌丄寢壥偼師偺傛偆偵側傝傑偡丅

    3042 3044 3046 d a

3042 偼乽偁乿丄3044 偼乽偄乿丄3046 偼乽偆乿偺 Unicode昞尰偱偡丅InputStreamReader 傪梡偄傞偙偲偱僔僼僩JIS 偺僼傽僀儖傪 Java 偺昗弨暥帤僐乕僪偱偁傞 Unicode 偵曄姺偟側偑傜撉傒崬傓偙偲偑偱偒傑偟偨丅

暥帤僐乕僪 "SJIS" 傪徣棯偟偨応崌偼丄僾儔僢僩僼僅乕儉偵埶懚偟偨僨僼僅儖僩乮徣棯帪偺乯暥帤僐乕僪偱撉傒弌偝傟傑偡丅擔杮岅 Windows 偺応崌偺僨僼僅儖僩暥帤僐乕僪偼 "MS932" 偵側傝傑偡丅SJIS 偲 MS932 偼傎偲傫偳摨偠傕偺偱偡偑丄乽乣乿傗乽乤乿側偳堦晹偺暥帤偱摦嶌偑堎側傝傑偡丅

暥帤僐乕僪 | 愢柧
--- | ---
ISO-8859-1 | ASCII丅
ISO-2022-JP | JIS僐乕僪丅
Shift_JIS | JDK1.1 傑偱偼 SJIS 偲摨媊丅JDK1.2 偐傜偼 MS932 偲摨媊丅
SJIS | 僔僼僩JIS丅
MS932 | Microsoft 932丅僔僼僩JIS偲傎傏摨條偩偑庒姳堎側傞丅
CP932 | Code Page 932丅MS932 偲傎傏摨條偩偑庒姳堎側傞丅
EUC_JP | EUC丅

巊梡壜擻側暥帤僐乕僪偵娭偟偰偼壓婰偺儁乕僕傪嶲徠偟偰偔偩偝偄丅

- http://java.sun.com/j2se/1.4/ja/docs/ja/guide/intl/encoding.doc.html

#### 仭 僥僉僗僩僼傽僀儖傪撉傒崬傓乮FileReader乯

FileReader 偼 InputStreamReader 偺柺搢偝傪夝徚偡傞偨傔偵梡堄偝傟偨僒僽僋儔僗偱偡丅InputStreamReader 傛傝娙扨偵埖偊傑偡偑丄暥帤僐乕僪偵偼忢偵徣棯帪暥帤僐乕僪偑梡偄傜傟傑偡丅

仒FileReaderTest.java

    import java.io.*;

    class FileReaderTest {
        public static void main(String[] args) {
            try {
                FileReader in = new FileReader("file.txt");
                int ch;
                while ((ch = in.read()) != -1) {
                    System.out.print(Integer.toHexString(ch) + " ");
                }
                in.close();
            } catch (IOException e) {
                System.out.println(e);
            }
        }
    }

    FileReader in = new FileReader("file.txt");

偼丄壓婰偺僐乕僪偲傎傏摨偠堄枴傪帩偪傑偡丅

    FileInputStream is = new FileInputStream("file.txt");
    InputStreamReader in = new InputStreamReader(is);

#### 仭 僼傽僀儖傪1峴偢偮傛傒偙傓乮BufferedReader乯

僼傽僀儖傪 1峴偢偮撉傒崬傓偵偼 BufferedReader 傪梡偄傑偡丅

仒BufferedReaderTest.java

    import java.io.*;

    class BufferedReaderTest {
        public static void main(String[] args) {
            try {
                FileReader in = new FileReader("file.txt");
                BufferedReader br = new BufferedReader(in);
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
                br.close();
                in.close();
            } catch (IOException e) {
                System.out.println(e);
            }
        }
    }

http://www.tohoho-web.com/java/file.htm
弶斉丗2004擭6寧26擔丄嵟廔峏怴丗2004擭6寧26擔
[栠傞] [慜偵] [師偵]