aspose file tools*
The moose likes Beginning Java and the fly likes Pause / Sleep in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Pause / Sleep in Java" Watch "Pause / Sleep in Java" New topic
Author

Pause / Sleep in Java

Kamal Ahmed
Ranch Hand

Joined: Feb 15, 2005
Posts: 91
Hi,

In the following Code: I am trying to configure a sleep or wait , Can anyone give me some HINT on how to do this ?

Thanks,

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ReadFile {

/**
* Fetch the entire contents of a text file, and return it in a String.
* This style of implementation does not throw Exceptions to the caller.
*
* @param aFile is a file which already exists and can be read.
*/
static public String getContents(File aFile) {
//...checks on aFile are elided
StringBuffer contents = new StringBuffer();

//declared here only to make visible to finally clause
BufferedReader input = null;
try {
//use buffering
//this implementation reads one line at a time
input = new BufferedReader( new FileReader(aFile) );
String line = null; //not declared within while loop
while (( line = input.readLine()) != null){
contents.append(line);

//Trying to implement a configurable wait here

contents.append(System.getProperty("line.separator"));
}
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex){
ex.printStackTrace();
}
finally {
try {
if (input!= null) {
//flush and close both "input" and its underlying FileReader
input.close();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
return contents.toString();
}

public static void main ( String[] aArguments ) throws IOException {
File testFile = new File("checkpoint_20000_.txt");
getContents(testFile);

System.out.println( "File contents: " + getContents(testFile) );
}
}
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Just call the static method Thread.sleep(long millis). This method throws an InterruptedException, so you'll want to enclose it in a try/catch block.

See API...
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html
[ February 18, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
Don't get me started about those stupid light bulbs.
 
subject: Pause / Sleep in Java