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:
ä, 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