Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

java.nio, FileChannel Reading - Differences in Windows and Linux

807607Oct 23 2006 — edited Oct 23 2006
My sample application (posted below) is having a problem. When run in Windows (under 1.5.0_06), the file I read in comes in just fine utilizing the code below. When I run the same code in Linux (also under 1.5.0_06), it is not being read in properly. The problem is that the characters I am trying to read are not being properly resolved in Linux, but for some reason they are in Windows.

Any help with the problem would be greatly appreciated.
The character causing the problem is the latin small letter a with diaeresis (HTML: ä, ASCII: &#0228, Unicode: ä, ALT+ASCII key combo: ALT+0228). If I could attach the file, I would. However, if you want it, just post here and I'll e-mail it to you.

Here's the code:
public static void main(String[] args) {
    // pick a file
    JFileChooser fileDlg = new JFileChooser(System.getProperty("user.home"));
    fileDlg.showDialog(null, "Read");
    File file = fileDlg.getSelectedFile();

    try {
      // open the fiel & the channel
      FileChannel channel = new FileInputStream(file).getChannel();

      byte[] array = new byte[8000];
      ByteBuffer buffer = ByteBuffer.wrap(array);

      // read into the buffer
      while ((channel.read(buffer)) != -1) {
        ByteArrayInputStream istream = new ByteArrayInputStream(array);
        BufferedInputStream bistream = new BufferedInputStream(istream);
        InputStreamReader breader = new InputStreamReader(bistream, "UTF8");

        // print out what's in the buffer
        char[] mybuff = new char[8000];
        while (breader.read(mybuff) != -1)
          System.out.println(String.valueOf(mybuff));
      }
    }
    catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }

    System.exit(0);
  }
Thanks,
Joe
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 20 2006
Added on Oct 23 2006
5 comments
883 views