How to wait on file creation in Java
Here are some steps that we can remember while move in to coding part
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
/**
*
* @author Anand
*/
public class FileOperation {
public void ProcessFile() {
File statText = new File("statsTest1.txt");// Check for this file
while (!statText.exists()) {
try {
Thread.sleep(10);//wait for 10 ns
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("Finally got the =>"+statText.getName());
}
public static void main(String[] args) {
FileOperation write = new FileOperation();
write.ProcessFile();
}
}
For any query Ask Here . happy Coding
Anand
- Iterate the (!file.exist()) blog till this block becomes as a false . so for that we can use Thread class and do Thread.sleep(10). that will wait for 10ns and once time has been expire again it start same loop . means every 10 ns it will check on directory whether file is exist or not . if not then iterate.
- once it got the file on directory then while loop iteration will stop and terminate the execution of while block
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
/**
*
* @author Anand
*/
public class FileOperation {
public void ProcessFile() {
File statText = new File("statsTest1.txt");// Check for this file
while (!statText.exists()) {
try {
Thread.sleep(10);//wait for 10 ns
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("Finally got the =>"+statText.getName());
}
public static void main(String[] args) {
FileOperation write = new FileOperation();
write.ProcessFile();
}
}
For any query Ask Here . happy Coding
Anand
Comments
Post a Comment