java-如何通过跳过某些部分来读取文件并输入数据信息二维数组

我正在尝试流式传输文件并将其内容输入2D数组.我有这段代码可以成功读取文件的所有内容.我想以这样的方式调整代码,使其跳过文件的第一行(#R1 R2 R3 R4 R5)以及文件每一行上的数字.在某种程度上,我想删除arr [X] [Y]中所有X = 0和Y = 0的元素.我想在使用扫描仪读取文件的同时执行此操作,而不是创建一个新数组并遍历第一个数组并将所需数据存储在新数组中.

 

这是流文件:

 

#   R1  R2  R3  R4  R5
1   J   S2  Q   S2  J
2   J   S2  Q   S2  J
3   J   S2  Q   S2  J
4   J   S2  Q   S2  J
5   J   Q   S5  Q   J
6   S3  Q   S5  Q   S3
7   S3  Q   S5  Q   S3
8   S3  Q   S5  Q   S3
9   S3  S1  S5  S1  S3
10  A   S1  S5  S1  A
11  A   S1  K   S1  A
12  A   S1  K   S1  A
13  A   S1  K   S1  A
14  A   S1  K   S1  A
15  S2  A   K   A   S2
16  S2  A   S3  A   S2
17  S2  A   S3  A   S2
18  S2  A   S3  A   S2
19  S2  A   S3  A   S2
20  Q   S5  S3  S5  Q
21  Q   S5  J   S5  Q
22  Q   S5  J   S5  Q
23  Q   S5  J   S5  Q
24  Q   S5  J   S5  Q
25  S4  K   S2  K   S4
26  S4  K   S2  K   S4
27  S4  K   S2  K   S4
28  S4  K   S2  K   S4
29  S4  K   S2  K   S4
30  K   S4  S2  S4  K
31  K   S4  A   S4  K
32  K   S4  A   S4  K
33  K   S4  A   S4  K
34  K   S4  A   S4  K
35  S1  S4  A   S4  S1
36  S1  A   S1  A   S1
37  S1  A   S1  A   S1
38  S1  A   S1  A   S1
39  S1  A   S1  A   S1
40  Q   S3  S1  S3  Q
41  Q   S3  Q   S3  Q
42  Q   S3  Q   S3  Q
43  Q   S3  Q   S3  Q
44  Q   J   Q   J   Q
45  S2  J   S4  J   S2
46  S2  J   S4  J   S2
47  S2  J   S4  J   S2
48  S2  S2  S4  S2  S2
49  S2  S2  A   S2  S2
50  J   S2  A   S2  J
51  J   S2  A   S2  J
52  J   Q   A   Q   J
53  J   Q   A   Q   J
54  J   Q   J   Q   J
55  S5  Q   J   Q   S5
56  S5  J   J   J   S5
57  S5  J   J   J   S5
58  S5  J   S1  J   S5
59  S5  J   S1  J   S5
60  A   S3  S1  S3  A
61  A   S3  S1  S3  A
62  A   S3  Q   S3  A
63  A   S3  Q   S3  A
64  A   K   Q   K   A
65  S1  K   Q   K   S1
66  S1  K   S4  K   S1
67  S1  K   S4  K   S1
68  S1  Q   S4  Q   S1
69  S1  Q   S4  Q   S1
70  K   Q   K   Q   K
71  K   Q   K   Q   K
72  K   J   K   J   K
73  K   J   K   J   K
74  K   J   K   J   K
75  S3  J   J   J   S3
76  S3  NA  J   NA  S3
77  S3  NA  J   NA  S3
78  S3  NA  J   NA  S3
   public static void main(String[] args) throws FileNotFoundException 
{
   Scanner sc = new Scanner(new BufferedReader(new FileReader("reels_template.txt")));

   int arrX = 78;
   int arrY = 6;
   String [][] arr = new String[arrX][arrY];
   String[] line = {};



     for (int i=0; i<arrX; i++) {
         if (sc.hasNextLine()) {
         line = sc.nextLine().trim().split("\\s+");
         }
        for (int j=0; j<arrY; j++) {        
            arr[i][j] = line[j] + " ";                             
        }
     }


   for (int i = 0; i < arrX; i++) {
       for ( int j = 0; j < arrY; j++) {
           System.out.print(arr[i][j]);
       }
       System.out.println();
   }

}

这是上面代码的输出:

 

 # R1 R2 R3 R4 R5 
 1 J S2 Q S2 J 
 2 J S2 Q S2 J 
 3 J S2 Q S2 J 
 4 J S2 Q S2 J 
 5 J Q S5 Q J 
 6 S3 Q S5 Q S3 
 7 S3 Q S5 Q S3 
 8 S3 Q S5 Q S3

这是所需的输出:

 

 J S2 Q S2 J 
 J S2 Q S2 J 
 J S2 Q S2 J 
 J S2 Q S2 J 
 J Q S5 Q J 
 S3 Q S5 Q S3 
 S3 Q S5 Q S3 
 S3 Q S5 Q S3

编辑:通过使用扫描仪读取第一行,然后循环删除不需要的行并重新调整第二个for循环,解决了问题.代码示例:

 

 public static void main(String[] args) throws FileNotFoundException 
{
   Scanner sc = new Scanner(new BufferedReader(new FileReader("reels_template.txt")));

   final int arrX = 78;
   final int arrY = 5;
   String[][] arr = new String[arrX][arrY];
   String[] line = {};


     line = sc.nextLine().trim().split("\\s+");
     for (int i=0; i<arrX; i++) {
         if (sc.hasNextLine()) {
         line = sc.nextLine().trim().split("\\s+");
         }
        for (int j=0; j<=arrY; j++) {  
            if (j!=0) {
            arr[i][j-1] = line[j] + " "; 
           }
        }
     }


   for (int i = 0; i < arrX; i++) {
       for ( int j = 0; j < arrY; j++) {
           System.out.print(arr[i][j]);
       }
       System.out.println();
   }

}

最佳答案

最简单的解决方案(即使很明显)将是根本不阅读您不想阅读的元素.您的代码将被这样修改:

 

 

public static void main(String[] args) throws FileNotFoundException {
   Scanner sc = new Scanner(new BufferedReader(new FileReader("reels_template.txt")));

   int arrX = 77;
   int arrY = 4;
   String [][] arr = new String[arrX][arrY];
   String[] line = {};

   sc.nextLine();  // remove the first line from the scanner

   for (int i=0; i<arrX; i++) {
     if (sc.hasNextLine())
       line = sc.nextLine().trim().split("\\s+");

       for (int j=0; j<arrY; j++) {        
         arr[i][j] = line[j+1] + " "; //always skips first element of 'line'                           
       }
    }


    for (int i = 0; i < arrX; i++) {
      for ( int j = 0; j < arrY; j++) 
        System.out.print(arr[i][j]);

      System.out.println();
    }

}

但是,这段代码仅适用于具有特定数量的“行”和每行中具有特定数量的元素的文件.如何使其更具“动态性”?

以下方法会将文件的每一行作为字符串添加到ArrayList< String>中.然后,它将删除第一行,然后删除每行前面的第一个数字.

 

public ArrayList<String> readFileText (String filename){
    //ArrayList to hold all the lines
    ArrayList<String> lines = null;

    //Get lines of text (Strings) as a stream
    try (Stream<String> stream = Files.lines(Paths.get(filename))){
        // convert stream to a List-type object
        lines = (ArrayList<String>)stream.collect(Collectors.toList());
    }
    catch (IOException ioe){
        System.out.println("\nCould not read lines of text from the file.\n" +
                "Reason: parameter \"" + ioe.getMessage() + "\"");
    }
    catch (SecurityException se){
        System.out.println("Could not read the file provided." + 
          "Please check if you have permission to access it.");
    }

    lines.remove(0);  //Remove the first line of the file. Line at index 0.

    for(String line : lines)
        //Fine the first occurrence of white-space in each line,
        //and use that substring, to remove the number of the line.
        line = line.substring(line.indexOf(" "));

    return lines;
}

要在您的代码中使用此方法:

 

public static void main(String[] args){
    ArrayList<String> allFileLines = readFileText("reels_template.txt");

    for(String line : allFileLines)
        System.out.println(line);
}

看看主要方法现在看起来有多优雅?这就是方法的使用方式.始终要使它们具有更动感的特性,而不是硬编码的样式.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值