2016-06-24 1 views
0

텍스트 파일에있는 내용을 읽고 텍스트가 변경된 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 } 
 
}

+0

당신이 그것을 변경하거나하지 여부를 확인하기 위해 파일의 타임 스탬프를 확인할 수 있습니다. 언제 빈 문자열을 반환할까요? – Boola

+0

조건을 지정하면 루프가 가능한 한 빨리 실행됩니다. 좋지도 않아도됩니다. 대신이 논리를 별도의 스레드에 쓰고 모든 파일 내용 검사가 끝난 후 1 초 후에 스레드를 잠자 게 할 수 있습니다 ... 또한 내용을 검사하는 대신 파일 크기의 변형을 검사해볼 수 있습니다. –

+0

죄송합니다. redflar3에 대해 이해할 수 없습니다. Java를 처음 사용하기 때문에. 좀 더 자세히 설명해 주시겠습니까? 나는 흥미가있다. –

답변

0

당신이 그것을 변경하거나하지 여부를 확인하기 위해 파일의 타임 스탬프를 확인하실 수 있습니다 : 여기 내 코드입니다.

샘플 코드 : 또한

Long fileLastModified; 
    File file = new File("file"); 
    if (!file.exists()) { 
     syserr 
    } 
    if (file.lastModified() <= this.fileLastModified) { 
     return oldValue; 
    } 
    this.fileLastModified = file.lastModified(); 

redflar3에 의해 주석, 당신은 파일을 확인

0

안녕 당신은 타이머를 만들 수 있습니다 스레드를 사용한다 한 번 매 2 초는 그래서 조각은 여기

간다
new java.util.Timer().schedule( 
    new java.util.TimerTask() { 
     @Override 
     public void run() { 
      //Read your file 
      if(fileEmpty){ 
       //Update your textfiled 
       cancel(); //Stops this timer 
      } 
     } 
    }, 
    0,2000); 

일단 작업이 완료되면 purge();를 사용하십시오. 모든 취소 타이머를

희망을 지우려면이

관련 문제