Java Regex行分割文件中累积的项目数组
这是我在文件中的项目列表Java Regex行分割文件中累积的项目数组,java,regex,Java,Regex,这是我在文件中的项目列表 Name1 2019 8293 Name1 2019 8293 Name1 2019 8293 Name1 2019 8293 我想通过这个解析并将数据添加到数据对象中。我想要的字段是字符串名称、整数年和整数人口 但是让我们先把它放到一个数组中。我累积字符串,然后像这样拆分它string[]array=output.split(regex)然后我想要的输出是数组的值[name120198293,…] 就代码而言,我有一个如何处理的计划,但是这个文件格式真的把我
Name1
2019 8293
Name1
2019 8293
Name1
2019 8293
Name1
2019 8293
我想通过这个解析并将数据添加到数据对象中。我想要的字段是字符串名称、整数年
和整数人口
但是让我们先把它放到一个数组中。我累积字符串,然后像这样拆分它string[]array=output.split(regex)代码>然后我想要的输出是数组的值[name120198293,…]
就代码而言,我有一个如何处理的计划,但是这个文件格式真的把我搞砸了。任何帮助都将不胜感激
注:
我尝试了“\r\n | \n |[]”
以下是代码:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String args[]) throws Exception {
List<String[]> result = new ArrayList<String[]>();
Reader dataReader = new FileReader("D:\\data.txt");
BufferedReader bufferReader = new BufferedReader(dataReader);
try {
String line;
while ((line = bufferReader.readLine()) != null) {
String[] yearAndPopulation = new String[2];
String name;
if (line.indexOf(' ') < 0) {
name = line;
//Read next line;
if((line = bufferReader.readLine()) != null) {
//replace all duplicated spaces by single space
line = line.trim().replaceAll(" +", " ");
yearAndPopulation = line.split(" ");
result.add(new String[] {name, yearAndPopulation[0], yearAndPopulation[1]});
}
}
}
printResult(result);
} finally {
dataReader.close();
bufferReader.close();
}
}
private static void printResult(List<String[]> result) {
for (String[] arr : result) {
System.out.println(String.format("Name:%s, Year:%s, Population:%s", arr[0], arr[1], arr[2]));
}
}
}
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.Reader;
导入java.util.ArrayList;
导入java.util.List;
公开课考试{
公共静态void main(字符串args[])引发异常{
列表结果=新建ArrayList();
Reader dataReader=新文件读取器(“D:\\data.txt”);
BufferedReader bufferReader=新的BufferedReader(dataReader);
试一试{
弦线;
而((line=bufferReader.readLine())!=null){
字符串[]yearAndPopulation=新字符串[2];
字符串名;
如果(第行索引(“”)小于0){
名称=行;
//读下一行;
if((line=bufferReader.readLine())!=null){
//用单个空格替换所有重复的空格
line=line.trim().replaceAll(“+”,”);
yearAndPopulation=行分割(“”);
add(新字符串[]{name,yearAndPopulation[0],yearAndPopulation[1]});
}
}
}
打印结果(result);
}最后{
dataReader.close();
bufferReader.close();
}
}
私有静态void打印结果(列表结果){
for(字符串[]arr:result){
System.out.println(String.format(“名称:%s,年份:%s,人口:%s”,arr[0],arr[1],arr[2]);
}
}
}
查看示例所需的输出,尤其是:
“然后,我希望数组的输出值是[name120198293,…]
”
您可以这样做:
String[] array = {}; // Array to hold the final readin in results
List<String> list = new ArrayList<>(); // List inteferface for processing file data.
try {
// Using for/each and java.nio.file to process file contents.
for (String line : Files.readAllLines(Paths.get("Data_File.txt"), StandardCharsets.UTF_8)) {
line = line.trim(); // Trim the line of leading or trailing whitespaces.
// Skip Blank Lines (if any) in file...
if (line.equals("")) {
continue;
}
// If a file line consists of two numerical values consisting
// of one or more digits separated by one or more whitespaces...
if (line.matches("\\d+\\s+\\d+")) {
// Split the line into Year and Population then
// add them to the list.
list.add(line.split("\\s+")[0]); // Year
list.add(line.split("\\s+")[1]); // Population
}
// Otherwise the line must be a Name.
else {
// Add Name to the list.
list.add(line); // Name
}
}
// Convert List to String[] Array
array = list.toArray(new String[0]);
}
catch (IOException ex) {
System.err.println(ex);
}
// Display the results read in from file now contained within the array[] Array.
String arrayString = Arrays.toString(array);
// Remove Square Brackets ([]) for display.
System.out.println(arrayString.substring(1, arrayString.length() - 1));
运行上述代码后的控制台输出:
分别阅读每组的第1行和第2行。然后在空格上拆分第二行。不需要正则表达式。为了一个简单的解决方案,我花了太多时间在这个问题上。非常感谢。我想我被一个事实吸引住了,那就是有人建议使用regex,而我忘记了从另一个角度来看待这个问题。
Name1
2019 9999
Name2
2019 8585
Name3
2019 8888
Name4
2018 7777
Name1, 2019, 9999, Name2, 2019, 8585, Name3, 2019, 8888, Name4, 2018, 7777