我的文本文件如下所示:
ABC = -1 Temp = 2 Try = 34 Message =“some text”SYS = 3
ABC = -1 Temp = 5 Try = 40 Message =“更多不同的文字”SYS = 6
并且模式继续,但只更改“”内的数值和文本。
注意:消息= 也可以包含多个引号。
我想将 ABC,Temp,Try和SYS 的值存储到 int变量 消息到字符串变量。
我目前正在使用:
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();
我想过使用扫描仪类并逐行阅读,但我对如何在不同变量中对行进行分类感到困惑?
答案 0 :(得分:4)
首先创建一个代表数据的类:
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;
}
}
然后制作一个方法,使用Regex capture groups将String
转换为此类:
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);
}
然后使用扫描仪读取文件:
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();
}
答案 1 :(得分:2)
您可以使用regex
进行此类解析,格式为:
"ABC=([+-]?\\d+) Temp=([+-]?\\d+) Try=([+-]?\\d+) Message=\"(.+)\" SYS=([+-]?\\d+)"
模式细分(Pattern Reference):
ABC=
- 文字字符串([+-]?\\d+)
- 捕获第1组中的正数或负数Temp=
- 文字字符串([+-]?\\d+)
- 捕获第2组中的正数或负数Try=
- 文字字符串([+-]?\\d+)
- 捕获第3组中的正数或负数Message=
- 文字字符串\"(.+)\"
在捕获组4 SYS=
- 文字字符串([+-]?\\d+)
- 捕获第5组中的正数或负数如果String
与模式匹配,您可以像这样提取您的值:
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);
}
}
}
结果:
-1 2 34 some text 3
-1 5 40 some more "and" different text 6
答案 2 :(得分:1)
如果您已使用indexOf
方法,则以下代码将起作用
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);
这将为您提供以下
abc : -1
temp : 2
try : 34
message : some text
sys : 3
仅当您获得的字符串数据具有此确切语法时才会起作用,即它包含ABC
,Temp
,Try
,Message
和{{1 }}。希望这会有所帮助。