tags:

views:

120

answers:

2

In the last section of the code I print what the Reader gives me. But its just bogus, where did I go wrong?

public static void read_impl(File file, String targetFile) {
    // Create zipfile input stream
    FileInputStream stream = new FileInputStream(file);
    ZipInputStream zipFile = new ZipInputStream(new BufferedInputStream(stream));

    // Im looking for a specific file/entry
    while (!zipFile.getNextEntry().getName().equals(targetFile)) {
        zipFile.getNextEntry();
    }

    // Next step in api requires a reader
    // The target file is a UTF-16 encoded text file
    InputStreamReader reader = new InputStreamReader(zipFile, Charset.forName("UTF-16"));

    // I cant make sense of what this print
    char buf[] = new char[1];
    while (reader.read(buf, 0, 1) != -1) {
        System.out.print(buf);
    }
}
A: 

Your use of a char array is a bit pointless, though at first glance it should work. Try this instead:

int c;
while ((c = reader.read()) != -1) {
    System.out.print((char)c);
}

If that does not work either, then perhaps you got the wrong file, or the file does not contain what you think it does, or the console can't display the characters it contains.

Michael Borgwardt
It prints the exact same thing as my initial source. The console is the output window of the Netbeans IDE and afaik its capable of displaying those characters.
mizipzor
It's hard to diagnose without the most crucial information: what characters exactly the file contains and exactly what "bogus" is printed instead. The code you've shown should work.
Michael Borgwardt
+1  A: 

I'd guess that where you went wrong was believing that the file was UTF-16 encoded.

Can you show a few initial byte values if you don't decode them?

Stephen Denne
Sadly, you are correct. The customer claimed it was UTF-16. I was naive to believe it without checking it myself. It was, in fact, plain old UTF-8. At least it was an easy solution. :)
mizipzor