英文:
parse text file with array in java
问题
try (Scanner scan = new Scanner(new File("C:\\Test.txt"))) {				  
    String N=null;
         
    while( scan.hasNextLine() )
    {
        String[] ary = scan.nextLine().split("\\s+");           
        if( ary.length == 3)
        {
            if( ary[0].startsWith("A-"))
                N = ary[0]; 
            System.out.printf("A-%-5s %5s %5s%n", N, ary[1], ary[2]); 
        }
    }
}
文件示例:
START
ELEMENT
G1      G2      G3
A-0     FX      0
        FX      1 
A-20    FY      0
        FY      1  
谢谢。
英文:
I have to parse a text file with array of string the code bellow work fine I need a support how to get rid of the header of fields ( G1 & G2 & G3) while parsing the text file .
   try (Scanner scan = new Scanner(new File("C:\\Test.txt"))) {				  
			   
			     String N=null;
		         
		        while( scan.hasNextLine() )
		        {
		        	//scan.skip(Pattern.compile("G1  G2  G3 "));
		        	
		            String[] ary = scan.nextLine().split( "\\s+" );           
		            
		            if( ary.length == 3)
		            {
		                if( ary[0].startsWith( "A-" ) )
		                	
		                    N = ary[0]; 
             System.out.printf( "A-%-5s %5s %5s%n", N, ary[1], ary[2] ); 
2/ file sample is :
START
ELEMENT
G1      G2      G3
A-0     FX      0
        FX      1 
A-20    FY      0
        FY      1  
thanks
答案1
得分: 0
你的代码基本上是有效的,但我猜你在寻找更简单的方法。
非常有用的是 Stream 以及实用类 Files,它们操作的是 Path 类,这是 File 的一个更通用的概念。
Path path = Paths.get("C:\\Test.txt");
// A-* F* 0 的数据:
List<String[]> data = Files.lines(path) // lines(path, charset)
            .map(line -> line.split("\\s+"))
            .filter(arr -> arr.length == 3)
            .filter(arr -> arr[0].startsWith("A-"))
            .collect(Collectors.toList());
仅打印的话:
    Files.lines(path, Charset.forName("Windows-1252"))
            .map(line -> line.split("\\s+"))
            .filter(arr -> arr.length == 3)
            .filter(arr -> arr[0].startsWith("A-"))
            .forEach(arr -> System.out.printf("%-7s %5s %5s%n",
                    arr[0], arr[1], arr[2]));
如果问题是按照 G1 进行分组(空的第一列作为重复项),一个简单的方法是:
public static void main(String[] args) throws IOException {
    Path path = Paths.get("C:/Develop/Test.txt");
    List<String[]> data = new ArrayList<>();
    AtomicReference<String> g1 = new AtomicReference<>("");
    Files.lines(path) // UTF-8 文件
        .map(line -> line.split("\\s+"))
        .filter(arr -> arr.length == 3)
        .filter(arr -> arr[0].startsWith("A-")
            || (arr[0].isEmpty() && !data.isEmpty()))
        .forEach(arr -> {
            if (!arr[0].isEmpty()) {
                g1.set(arr[0]);
            }
            System.out.printf("%-7s %5s %5s%n", g1, arr[1], arr[2]);
            arr[0] = g1.get();
            data.add(arr);
        });
    for (String[] ary : data) {
        System.out.println(Arrays.toString(ary));
    }
}
我认为后者是你用 N 所打算的。要得到:
A-0     FX      0
A-0     FX      1 
A-20    FY      0
A-20    FY      1
英文:
Your code works principally, but I assume you are looking for something easier.
Really useful are Streams and the utility class Files, operating of the class Path, a more general concept of File.
Path path = Paths.get("C:\\Test.txt");
// Data of A-* F* 0:
List<String[]> data = Files.lines(path) // lines(path, charset)
            .map(line -> line.split("\\s+"))
            .filter(arr -> arr.length == 3)
            .filter(arr -> arr[0].startsWith("A-"))
            .collect(Collectors.toList());
For just printing:
    Files.lines(path, Charset.forName("Windows-1252"))
            .map(line -> line.split("\\s+"))
                    .filter(arr -> arr.length == 3)
                    .filter(arr -> arr[0].startsWith("A-"))
                    .forEach(arr -> System.out.printf("%-7s %5s %5s%n",
                            arr[0], arr[1], arr[2]));
If the problem is the grouping by G1 (empty first column as repetition), a no-brainer would be:
public static void main(String[] args) throws IOException {
    Path path = Paths.get("C:/Develop/Test.txt");
    List<String[]> data = new ArrayList<>();
    AtomicReference<String> g1 = new AtomicReference<>("");
    Files.lines(path) // UTF-8 file
            .map(line -> line.split("\\s+"))
                    .filter(arr -> arr.length == 3)
                    .filter(arr -> arr[0].startsWith("A-")
                            || (arr[0].isEmpty() && !data.isEmpty()))
                    .forEach(arr -> {
                        if (!arr[0].isEmpty()) {
                            g1.set(arr[0]);
                        }
                        System.out.printf("%-7s %5s %5s%n", g1, arr[1], arr[2]);
                        arr[0] = g1.get();
                        data.add(arr);
                    });
    for (String[] ary : data) {
        System.out.println(Arrays.toString(ary));
    }
}
I think the latter is what you intended with N.
To receive
A-0     FX      0
A-0     FX      1 
A-20    FY      0
A-20    FY      1  
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论