@Override public StringBuilder doInBackground() throws IOException, InterruptedException { int lineNumber = 0; try (Scanner in = new Scanner(new FileInputStream(file))) { while (in.hasNextLine()) { String line = in.nextLine(); lineNumber++; text.append(line); text.append("\n"); ProgressData data = new ProgressData(); data.number = lineNumber; data.line = line; publish(data); Thread.sleep(1); // to test cancellation; no need to do this // in your programs } } return text; }