SwingWorker with FileReader

前端 未结 1 475
再見小時候
再見小時候 2021-01-29 01:05

I have problem about applied SwingWorker with FileReader and my point is I need to implement FileReader with SwingWorker to make my UI Show the text from the file and this is my

相关标签:
1条回答
  • 2021-01-29 02:10

    Works just fine for me:

    public class Reader extends SwingWorker<List<String>, String> {
    
        protected List<String> doInBackground() throws Exception {
    
            ArrayList<String> lstText = new ArrayList<String>(25);
    
            BufferedReader in = null;
            try {
    
            FileReader read = new FileReader("Scanner.txt");
            in = new BufferedReader(read);
            String s = null;
    
            while ((s = in.readLine()) != null) {
    
                lstText.add(s);
                publish(s);
    
            }
    
            } finally {
    
    
                try {
    
                    in.close();
    
                } catch (Exception e) {
                }
            }
    
            return lstText;
    
        }
    
        @Override
        protected void process(List<String> chunks) {
    
            for (String text : chunks) {
    
                fldText.append(text + "\n");
    
            }
    
        }
    
        @Override
        protected void done() {
        }
    }
    
    0 讨论(0)
提交回复
热议问题