bob dylan
bob dylan

Reputation: 667

BufferedReader is not showing normal characters from file

I am trying to read the contents of a file, such a .txt, .doc, etc, but for some reason it displays only the special symbols/characters if its not a .txt file.

For example, if I have a text file and print out the contents, which has "hello world", it will print out "hello world".

However, if its a Word doc, which has "hello world", then it would display something like "ê@˜u©Ë#Έ˜sñî‚Y‡G,óãe©Ré_?ê7¥Å"

I want it to only read the "normal characters" of any kind of word doc or text file. Here is what I have:

private void openFile(){

    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();//chooser is a JFileChooser
        try {//read the files contents and display it as the current pages text

            BufferedReader reader = new BufferedReader(new FileReader(file));
            String textInFile = "", line;

            while ((line = reader.readLine()) != null) {
                textInFile += line;
            }
            editor.setText(textInFile);//display the text to a JTextPane named editor
        } catch (Exception e1) {
        }
    }
}

Why did I get down voted?

Upvotes: 0

Views: 133

Answers (1)

Simon DeWolf
Simon DeWolf

Reputation: 388

That is because doc files are not encoded as text, which is what BufferedReader handles. You'll need to use a library, such as Apache POI, to decode that file type. https://poi.apache.org/document/docoverview.html

Upvotes: 2

Related Questions