逐行读取+替换基于上一行/下一行

时间:2013-12-22 00:17:14

标签: java regex line-by-line

请帮助;) 我正在逐行阅读这个文件:

Select
    *
From
    TABLE1
Where
    A = A;

我要检查: 如果“From”返回“From Table:”,并且前一行是“From”,则将{name of table}移动到与“From Table:”+ {table}相同的行。

我有这段代码:

// reading file line by line in Java using BufferedReader
FileInputStream fis = null;
BufferedReader reader = null;

try {
    fis = new FileInputStream("text.txt");
    reader = new BufferedReader(new InputStreamReader(fis));

    System.out
            .println("Reading File line by line using BufferedReader");

    String line = reader.readLine();
    while (line != null) {
        // System.out.println(line);
        // line = reader.readLine();

        String newtext = line
                // .replace("Select","Select Fields: ")
                .replace("From", "From Table: ")
                .replace("Where", "Where is condition: ")
                .replace("\t", "") // tabs
                .replace("\n", ""); // empty rows
        System.out.println(newtext);
        line = reader.readLine();
    }

} catch (FileNotFoundException ex) {
    Logger.getLogger(BufferedReaderExample.class.getName()).log(
            Level.SEVERE, null, ex);
} catch (IOException ex) {
    Logger.getLogger(BufferedReaderExample.class.getName()).log(
            Level.SEVERE, null, ex);

} finally {
    try {
        reader.close();
        fis.close();
    } catch (IOException ex) {
        Logger.getLogger(BufferedReaderExample.class.getName()).log(
                Level.SEVERE, null, ex);
    }
}

0 个答案:

没有答案