Image

Imageasmor wrote in Imagejava_dev

Hey everyone! Just joined, and I have a bit of a problem.

I'm trying to read from a text file, and I'm using a String Buffer class to store the contents. Now, according to the documentation, StringBuffer.appent(int i) should append the string representation of an integer, but for me it appears to be just appending the integer.

Here's the relevant code snippet:

FileReader in = new FileReader(file);
int c;
StringBuffer incoming = new StringBuffer();
while ((c = in.read()) != -1) {
   incoming.append(c);
}
in.close();
JOptionPane.showMessageDialog(null, incoming.toString());


Now, it's definitely reading the file. I don't have the whole ASCII chart memorized, but for example there are sequential numbers where there should be "ab", and repeating numbers where there should "SS."

I have a feeling it has something to do with the encoding, but I'll be blunt and admit that I need to have this done by tomorrow night, and I'd never even tried looking up how to handle files before today. Trying to figure out encoding stuff, if that is the problem, seems an utterly daunting task.

Any help would greatly be appreciated!

Just in case it's helpful, here's the whole method:

    public int openFile() throws IOException {
        int choice = chooser.showOpenDialog(null);
        if (choice == OPEN) {
            File file=chooser.getSelectedFile();
            if (file.exists()) {
                FileReader in = new FileReader(file);
                int c;
                StringBuffer incoming = new StringBuffer();
                while ((c = in.read()) != -1) {
                    incoming.append(c);
                }
                in.close();
                JOptionPane.showMessageDialog(null, incoming.toString());
            } else {
                choice=2;
            }
        }
        return choice;
    }