I want to be able to read in a text file and then print it out. That in itself is fairly straight forward. But I also need to be able to eliminate a line of text if it starts with a certain character.

Example text file to read in:

Hello,
How
#are
you
doing
today?

I would like the output file to look like this (eliminate any lines starting with #):

Hello,
How
you
doing
today?

I thought the code below would do the job, but I get the error 'java.lang.StringIndexOutOfBoundsException: String index out of range: 0' and I'm not really sure why.

Code:
public StringBuffer readTextFile(String filename) {

        File file = new File(filename);
        StringBuffer contents = new StringBuffer();
        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            // repeat until all lines are read
            while ((text = reader.readLine()) != null) {

                if(text.charAt(0) == '#'){
                    // Do nothing (don't append to contents since line starts with #)
                }
                else {
                    contents.append(text).append(System.getProperty(
                        "line.separator"));
                }

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return contents;
    }