Java InputReader. Detect if file being read is binary?

Posted by Trizicus on Stack Overflow See other posts from Stack Overflow or by Trizicus
Published on 2009-12-20T04:49:54Z Indexed on 2010/03/18 3:31 UTC
Read the original article Hit count: 326

I had posted a question in regards to this code. I found that JTextArea does not support the binary type data that is loaded.

So my new question is how can I go about detecting the 'bad' file and canceling the file I/O and telling the user that they need to select a new file?

class Open extends SwingWorker<Void, String>
{
    File file;
    JTextArea jta;

    Open(File file, JTextArea jta)
    {
        this.file = file;
        this.jta = jta;
    }

    @Override
    protected Void doInBackground() throws Exception
    {
        BufferedReader br = null;

        try
        {
            br = new BufferedReader(new FileReader(file));

            String line = br.readLine();

            while(line != null)
            {
                publish(line);
                line = br.readLine();
            }
        }
        finally
        {
            try
            {
                br.close();
            } catch (IOException e) { }
        }
        return null;
    }

    @Override
    protected void process(List<String> chunks)
    {
        for(String s : chunks)
            jta.append(s + "\n");
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about swingworker