Java打印问题

时间:2011-06-14 11:15:52

标签: java

我有一个文本文件,如下所示,

(DA1A11,Inflation accelerates, Thursday rate hike seen      )
(DA1A12, In India's grain bowl, farms face threat from MNREGS      )
(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      )

我必须将数据作为char从“(”到“)”打印为char,然后等待5秒再次开始打印,我该怎么做。

3 个答案:

答案 0 :(得分:1)

我不知道你想要什么。但我会尝试。

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");    

答案 1 :(得分:1)

如果你不需要5秒钟,你可以使用Thread.sleep(),否则你可能需要一个Scheduler,比如Java Quartz,我没有使用它。

答案 2 :(得分:0)

    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
        }
    }