简体   繁体   中英

Reading character files between specific word in java

I have a java file, FileJava.java like this:

public class FileJava {
public static void main(String args[]) {
    for (int i = 0; i < 5; i++) {

    }
}

}

Then, i read above code line by line using this code:

import java.util.List;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;

public class FileReplace {
 List<String> lines = new ArrayList<String>();
 String line = null;


public void  doIt() {
    try {
        File f1 = new File("FileJava.java");

        FileReader fr = new FileReader(f1);
        BufferedReader br = new BufferedReader(fr);

        while ((line = br.readLine()) != null) {

            if (line.contains("for"))
                {
                    lines.add("long A=0;");

                    if(line.contains("(") && line.contains(")")){
                        String get = line;
                        String[] split = get.split(";");

                        String s1 = split[0];
                        String s2 = split[1];
                        String s3 = split[2];


                    }
            }
            lines.add(line);
        }
        fr.close();
        br.close();

        FileWriter fw = new FileWriter(f1);
        BufferedWriter out = new BufferedWriter(fw);
        for(String s : lines)
             out.write(s);
        out.flush();
        out.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

public static void main(String args[]) {
    FileReplace fr = new FileReplace();
    fr.doIt();

}

}

The question is, how to read character between '(' and ')' inside (for) in the FileJava.java, the character i mean "int i = 0; i < 5; i++" that will be stored in a variable, i have split based on ";", but when i print, the value :

s1 = for (int i = 0
s2 = i < 5
s3 = i++) {

While i expect:

s1 = int i = 0
s2 = i < 5
s3 = i++

Thanks

To answer your question how to restrict the splitting to the parenthesized section:

String[] split =
   get.substring( get.indexOf('(')+1, get.indexOf(')').split("\\s*;\\s*");

Edit to address another prob.

Printing of the file will all happen in one line, because BufferedReader.readLine strips the line ends (LF, CRLF) from the line it returns. Thus, add a line break when writing:

for(String s : lines){
    out.write(s);
    out.newLine();
}
int index1 = line.indexOf("(");
int index2 = line.indexOf(")");
line = line.subString(index1 + 1, index2);

If you want to read the contents of a java file, you would be much better off using an AST (Abstract Syntax Tree) parser which will read in the contents of the file and then callback when it encounters certain expressions. In your case, you can listen just for the 'for' loop.

Its because you are splitting on ';' for the input

for (int i = 0; i < 5; i++) {

which will return all the characters between ';'

You can write a new method, called getBracketContent for example, that will be something like

String getBracketContent(String str)
{
    int startIdx = str.indexOf('(')
    int endIdx = str.indexOf(')')

    String content = str.subString(startIdx + 1, endIdx);
}

then your code would be

if(line.contains("(") && line.contains(")")){
                    String get = getBracketContent(line);
                    String[] split = get.split(";");

Ideally I would use regular expressions to parse the information you need, but that is probably something you may want to look into later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM