텍스트 파일에있는 내용을 읽고 텍스트가 변경된 textField2 날씨에 다음과 같은 코드가 있습니다. 변경 사항이있는 경우 계속 진행해야합니다. 그렇지 않은 경우 파일이 비어있을 때까지 다시 확인해야합니다. 누구든지 도와 줄 수 있습니까?텍스트 파일의 텍스트가 비어있는 것으로 계속 확인하려면 어떻게합니까? Java
package application;
import javafx.event.ActionEvent;
import javafx.scene.control.TextField;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Parent;
import javafx.fxml.FXMLLoader;
import javafx.fxml.FXML;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Main extends Application {
\t @Override
\t public void start(Stage primaryStage) {
\t \t try {
\t \t \t // BorderPane root = new BorderPane();
\t \t \t Parent root = FXMLLoader.load(getClass().
\t \t \t \t \t getResource("Root.fxml"));
\t \t \t
\t \t \t Scene scene = new Scene(root,550,400);
\t \t \t scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
\t \t \t primaryStage.setScene(scene);
\t \t \t primaryStage.show();
\t \t } catch(Exception e) {
\t \t \t e.printStackTrace();
\t \t }
\t }
\t
\t public static void main(String[] args) {
\t \t launch(args);
\t }
\t
\t @FXML
\t private TextField textField;
\t
\t @FXML
\t private TextField textField2;
\t
\t @FXML
\t protected void onClick(ActionEvent event) throws IOException {
BufferedWriter writer = null;
try {
//create a temporary file
// This will output the full path where the file will be written to...
writer = new BufferedWriter(new FileWriter("C:/Users/Custom/Documents/Sample.txt"));
writer.write(textField.getText());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// Close the writer regardless of what happens...
writer.close();
} catch (Exception e) {
}
while (readFile("C:/Users/Custom/Documents/Sample.txt") != ""){
\t textField2.setText("Waiting...");
}
textField2.setText("Done!");
\t }
}
\t String readFile(String fileName) throws IOException {
\t BufferedReader br = new BufferedReader(new FileReader(fileName));
\t try {
\t StringBuilder sb = new StringBuilder();
\t String line = br.readLine();
\t while (line != null) {
\t sb.append(line);
\t sb.append("\n");
\t line = br.readLine();
\t }
\t return sb.toString();
\t } finally {
\t br.close();
\t }
\t }
}
당신이 그것을 변경하거나하지 여부를 확인하기 위해 파일의 타임 스탬프를 확인할 수 있습니다. 언제 빈 문자열을 반환할까요? – Boola
조건을 지정하면 루프가 가능한 한 빨리 실행됩니다. 좋지도 않아도됩니다. 대신이 논리를 별도의 스레드에 쓰고 모든 파일 내용 검사가 끝난 후 1 초 후에 스레드를 잠자 게 할 수 있습니다 ... 또한 내용을 검사하는 대신 파일 크기의 변형을 검사해볼 수 있습니다. –
죄송합니다. redflar3에 대해 이해할 수 없습니다. Java를 처음 사용하기 때문에. 좀 더 자세히 설명해 주시겠습니까? 나는 흥미가있다. –