5 Replies Latest reply: Oct 30, 2006 12:23 PM by 798701 RSS

    help,DataInputStream and Unicode encoding problem

    807607
      Hello,everybody
      I am writing a small software for fun,but an problem about Unicode encoding stopped me. I tried to parse a file including integers,floats and Unicode characters(not UTF-8 but some other encoding type). I looked for the JDK documentation and I found that the class DataInputStream( implementing the interface DataInput) fitted my requirement best, then I tried but the Unicode characters are not read correctly( messy codes,only '????????').
      would you please help me? thanks a lot :-)
        • 1. Re: help,DataInputStream and Unicode encoding problem
          807607
          Pls post the code part, where you feel the problem lies.
          • 2. Re: help,DataInputStream and Unicode encoding problem
            807607
            the class DataInputStream has the methods useful to me, but find there is no method to set the encoding format ,both in DataInputStream and argument types used in its constructor:
            *********************************
            FileInputStream fis=new FileInputStream(fileName);
            DataInputStream     dis=new DataInputStream(fis);
            String line =dis.readLine();               System.out.println(line);
            // only "????????" output as result :-(
            **************************************
            I wonder how to set the encoding type,or another class.
            if I do it this way,it works,but there is no methods such as "readFloat","readInt",etc, so it's not what I want :
            *****************************
            FileInputStream fis=new FileInputStream(fileName);
            InputStreamReader read=new InputStreamReader(fis,"GB2312");
            BufferedReader reader=new BufferedReader(read);
            DataInputStream     dis=new DataInputStream(fis);
            String line = reader.readLine();
            System.out.println(line);
            *******************************

            thank you for your repley!
            • 3. Re: help,DataInputStream and Unicode encoding problem
              807607
              well, I don't get it, if you want use getFloat or getInt or some method like this, what does this get to do with mess code?
              Why you just use two objects DataOutputStream and BufferredReader,
              get them do what they can!!!!
              • 4. Re: help,DataInputStream and Unicode encoding problem
                798701
                Using DataInputStream you would read bytes from the file and convert them to characters yourself with the String(byte[] data, String encoding)-method. For example:
                byte[] data = new byte[100]
                datainput.readFully(data); // read 100 bytes from the file
                String output = new String(data, "GB2312");
                • 5. Re: help,DataInputStream and Unicode encoding problem
                  798701
                  Why you just use two objects DataOutputStream and
                  BufferredReader,
                  get them do what they can!!!!
                  If you actually try that out, you will find that you have serious problems with those objects buffering the stream data.