Java printing problem
I was having a text file as given below,
(DA1A11,Inflation accelerates, Thursday rate hike seen )
(DA1A12, In India's grain bowl, farms face threat from M开发者_StackOverflowNREGS )
(DA1A13, World off course on climate; renewables vital )
(DA1A14, Sri Lanka, India resume ferry service after 30 years )
(DA1A15, Hackers break into U.S. Senate computers )
(DA1A16, India seeks OECD help to bring back illicit funds - fin min )
(DA1A17, CBI looks into inflated oilfields costs - report )
(DA1A18, Maruti loses millions on continued strike, fears of spillover mount )
(DA1A19, Australia to warm up for India clash against New Zealand )
i have to print data's as char by char from "(" to ")" then wait for 5 sec and start printing again, how can i do this.
I don't exactly know what you want. But I will try.
String txt = ...; // Your long text
System.out.println(txt.replaceAll("\\((.+?)\\)", "$1");
try { Thread.sleep(5000L); } catch (Exception e) {} // Sleep 5 seconds
System.out.println(txt.replaceAll("\\((.+?)\\)", "$1");
If you don't strictly need 5 seconds, you can use Thread.sleep(), otherwise you may need a Scheduler, like Java Quartz, I didn't used it though.
BufferedReader reader = null;
try {
while(true){ // break condition here
reader = new BufferedReader ( new FileReader( "/myFilePath/file.txt" ) );
String line;
while( (line = reader.readLine()) != null ) {
char[] chars = line.toCharArray(); // adding .replaceAll("\\(|\\)", "") for no ()
for( char c : chars ) {
System.out.println(c);
}
Thread.currentThread().sleep( 5000 );
}
}
} catch (IOException e) {
// handle
} catch( InterruptedException e ) {
// handle
} finally {
try {
reader.close();
} catch (IOException e) {
// handle
}
}
精彩评论