简体   繁体   中英

How can I parse the content of file to variables?

I have text file which looks like this :

ABC=-1 Temp=2 Try=34 Message="some text" SYS=3
ABC=-1 Temp=5 Try=40 Message="some more and different text" SYS=6

and the pattern continues but only the numeric values and text inside the " " is changed.

NOTE : the Message= could have multiple quotes as well.

I want to store the value of ABC,Temp,Try and SYS to int variables And Message to a String variable .

I am currently using:

Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
  String line = scanner.nextLine();
  int count = line.indexOf("ABC=");
  if (count >= 0) {
    int clear = line.charAt(count + 3);
  }
}
scanner.close();

I thought of using the Scanner class and read line by line, but I am confused about how can I classify the line in different variables?

First make a class that represents the data:

public static class MyData { // please pick a better name
    final int abc;
    final int temp;
    final int tryNumber; // try is a keyword
    final String message;
    final int sys;

    public MyData(int abc, int temp, int tryNumber, String message, int sys) {
      this.abc = abc;
      this.temp = temp;
      this.tryNumber = tryNumber;
      this.message = message;
      this.sys = sys;
    }
}

Then make a method that transforms a String into this class using Regex capture groups :

private static Pattern p =
  Pattern.compile("ABC=([^ ]+) Temp=([^ ]+) Try=([^ ]+) Message=\"(.+)\" SYS=([^ ]+)");

private static MyData makeData(String input) {
  int abc = 0, temp = 0, tryNumber = 0, sys = 0;
  String message = "";

  Matcher m = p.matcher(input);
  if (!(m.find()) return null;
  abc = Integer.parseInt(m.group(1));
  temp = Integer.parseInt(m.group(2));
  tryNumber = Integer.parseInt(m.group(3));
  message = m.group(4);
  sys = Integer.parseInt(m.group(5));

  return new MyData(abc, temp, tryNumber, message, sys);
}

Then read the file using a scanner:

public static void main (String... args) throws Exception {
    File file = new File("/path/to/your/file.txt");
    List<MyData> dataList = new ArrayList<>();

    Scanner scanner = new Scanner(file);
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        MyData data = makeData(line);
        if(data != null) dataList.add(data);
    }
    scanner.close();
}

Here's a completely working demo on ideone

You can use regex for this kind of parsing with a pattern of:

"ABC=([+-]?\\d+) Temp=([+-]?\\d+) Try=([+-]?\\d+) Message=\"(.+)\" SYS=([+-]?\\d+)"

Pattern Breakdown ( Pattern Reference ):

  • ABC= - literal string
  • ([+-]?\\\\d+) - captures a positive or negative number in capture group 1
  • Temp= - literal string
  • ([+-]?\\\\d+) - captures a positive or negative number in capture group 2
  • Try= - literal string
  • ([+-]?\\\\d+) - captures a positive or negative number in capture group 3
  • Message= - literal string
  • \\"(.+)\\" captures a string in between double quotes in capture group 4
  • SYS= - literal string
  • ([+-]?\\\\d+) - captures a positive or negative number in capture group 5

If the String matches the pattern you can extract your values like this:

public static void main(String[] args) throws Exception {
    List<String> data = new ArrayList() {{
        add("ABC=-1 Temp=2 Try=34 Message=\"some text\" SYS=3");
        add("ABC=-1 Temp=5 Try=40 Message=\"some more \"and\" different text\" SYS=6");
    }};
    String pattern = "ABC=([+-]?\\d+) Temp=([+-]?\\d+) Try=([+-]?\\d+) Message=\"(.+)\" SYS=([+-]?\\d+)";

    int abc = 0;
    int temp = 0;
    int tryNum = 0;
    String message = "";
    int sys = 0;
    for (String d : data) {
        Matcher matcher = Pattern.compile(pattern).matcher(d);
        if (matcher.matches()) {
            abc = Integer.parseInt(matcher.group(1));
            temp = Integer.parseInt(matcher.group(2));
            tryNum = Integer.parseInt(matcher.group(3));
            message = matcher.group(4);
            sys = Integer.parseInt(matcher.group(5));

            System.out.printf("%d %d %d %s %d%n", abc, temp, tryNum, message, sys);
        }
    }
}

Results:

-1 2 34 some text 3
-1 5 40 some more "and" different text 6

If you are already using the indexOf approach, the following code will work

    String a = "ABC=-1 Temp=2 Try=34 Message=\"some text\" SYS=3";
    int abc_index = a.indexOf("ABC");
    int temp_index = a.indexOf("Temp");
    int try_index = a.indexOf("Try");
    int message_index = a.indexOf("Message");
    int sys_index = a.indexOf("SYS");
    int length = a.length();

    int abc = Integer.parseInt(a.substring(abc_index + 4, temp_index - 1));
    int temp = Integer.parseInt(a.substring(temp_index + 5, try_index - 1));
    int try_ = Integer.parseInt(a.substring(try_index + 4, message_index - 1));
    String message = a.substring(message_index + 9, sys_index - 2);
    int sys = Integer.parseInt(a.substring(sys_index + 4, length));

    System.out.println("abc : " + abc);
    System.out.println("temp : " + temp);
    System.out.println("try : " + try_);
    System.out.println("message : " + message);
    System.out.println("sys : " + sys);

This will give you the following

abc : -1
temp : 2
try : 34
message : some text
sys : 3

This will work only if the string data you get has this exact syntax, ie, it contains ABC , Temp , Try , Message , and SYS . Hope this helps.

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