JavaFX ReadFile计数器+另一个计数器任务?

时间:2015-01-01 15:35:44

标签: java multithreading javafx task counter

我有一个UI whit 2标签。

第一个标签:所有行 第二个标签:行以xxx结尾

我的程序读取文件并在任务中读取所有行:

public class ReadFileTask extends Task<Void> {

@Override
protected Void call() throws Exception {

    File file = new File("comments.txt"); //TODO

    long lines = Files.lines(file.toPath()).count();
    long line = 0;
    BufferedReader reader = new BufferedReader(new FileReader(file));
    while ((reader.readLine()) != null) {
        line++;
        updateMessage("" + line);

    }
    return null;
}}

现在我有了第一个标签。

对于第二张标签,我试试这个:

public class ReadFileTask extends Task<Void> {
        static int xxxLines = 0;
@Override
protected Void call() throws Exception {

    File file = new File("comments.txt"); //TODO

    long lines = Files.lines(file.toPath()).count();
    long line = 0;
    BufferedReader reader = new BufferedReader(new FileReader(file));
    while ((reader.readLine()) != null) {
        line++;
        updateMessage("" + line);

                if (reader.readLine().endsWith("xxx")) {  
          xxxLines++;


                }          
    }
    return null;
}}

按钮是:

    public void Datei(ActionEvent event) throws IOException, InterruptedException {
    ReadFileTask task = new ReadFileTask();
    daten.textProperty().bind(task.messageProperty());
    new Thread(task).start();
    Platform.runLater(()->xxx.setText("" + ReadFileTask.xxxLines));

}

1 个答案:

答案 0 :(得分:0)

使用Platform.runLater()作为第二个标签。但要注意:不要使用这些更新来泛滥GUI Thread:

Platform.runLater(()->xxxLabel.setText("" + xxxLines));