用Java解析带有数组的文本文件。

huangapple go评论61阅读模式
英文:

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(&quot;C:\\Test.txt&quot;);

// Data of A-* F* 0:
List&lt;String[]&gt; data = Files.lines(path) // lines(path, charset)
            .map(line -&gt; line.split(&quot;\\s+&quot;))
            .filter(arr -&gt; arr.length == 3)
            .filter(arr -&gt; arr[0].startsWith(&quot;A-&quot;))
            .collect(Collectors.toList());

For just printing:

    Files.lines(path, Charset.forName(&quot;Windows-1252&quot;))
            .map(line -&gt; line.split(&quot;\\s+&quot;))
                    .filter(arr -&gt; arr.length == 3)
                    .filter(arr -&gt; arr[0].startsWith(&quot;A-&quot;))
                    .forEach(arr -&gt; System.out.printf(&quot;%-7s %5s %5s%n&quot;,
                            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(&quot;C:/Develop/Test.txt&quot;);
    List&lt;String[]&gt; data = new ArrayList&lt;&gt;();
    AtomicReference&lt;String&gt; g1 = new AtomicReference&lt;&gt;(&quot;&quot;);
    Files.lines(path) // UTF-8 file
            .map(line -&gt; line.split(&quot;\\s+&quot;))
                    .filter(arr -&gt; arr.length == 3)
                    .filter(arr -&gt; arr[0].startsWith(&quot;A-&quot;)
                            || (arr[0].isEmpty() &amp;&amp; !data.isEmpty()))
                    .forEach(arr -&gt; {
                        if (!arr[0].isEmpty()) {
                            g1.set(arr[0]);
                        }
                        System.out.printf(&quot;%-7s %5s %5s%n&quot;, 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  

huangapple
  • 本文由 发表于 2020年7月27日 00:54:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63103099.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定