Java 如何通过跳过某些部分读取文件并输入数据信息二维数组
我正在尝试对文件进行流式处理,并将其内容输入到2D数组中。我有这个代码,它成功地读取了文件的所有内容。我想调整代码,使其跳过文件的第一行(#R1 R2 R3 R4 R5)和文件每行开始的数字。在某种程度上,我想删除arr[X][Y]中X=0和Y=0的所有元素。我希望在使用扫描仪读取文件时执行此操作,而不是装入一个新数组并遍历第一个数组并将所需数据存储在新数组中 这是流文件:Java 如何通过跳过某些部分读取文件并输入数据信息二维数组,java,arrays,Java,Arrays,我正在尝试对文件进行流式处理,并将其内容输入到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
# 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
这是所需的输出:
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
编辑:通过使用扫描仪读取循环前的第一行,删除不需要的行并重新调整循环的第二行,问题已经解决。代码示例:
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();
}
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException
{
Scanner sc=新的扫描仪(新的BufferedReader(新的文件阅读器(“reels_template.txt”));
最终int arrX=78;
最终到达时间=5;
字符串[][]arr=新字符串[arrX][arrY];
字符串[]行={};
line=sc.nextLine().trim().split(\\s+);
对于(int i=0;i来说,最简单的解决方案(尽管很明显)是不读取您不想读取的元素。您的代码将被修改如下:
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();
}
}
要在代码中使用此方法,请执行以下操作:
public static void main(String[] args){
ArrayList<String> allFileLines = readFileText("reels_template.txt");
for(String line : allFileLines)
System.out.println(line);
}
publicstaticvoidmain(字符串[]args){
ArrayList allFileLines=readFileText(“revels_template.txt”);
用于(字符串行:所有文件行)
系统输出打印项次(行);
}
看看主方法现在看起来有多优雅?这就是方法的使用方式。始终使它们具有更动态的性质,而不是硬编码的风格。我相信我刚才以类似的方式解决了第二部分。至于第一部分,我认为使用continue是一个更好的选择,因此我将相应地更新我的代码。非常感谢h提供帮助。您可能需要考虑一些边缘情况。例如,当您读取一个空文件时会发生什么?或者,当int arrX
大于您将要拥有的行数时会发生什么?(尝试将arrX设置为120,看看会发生什么,或者尝试解析一个空文件)更新:使用“继续”不会用任何值填充数组的第一行,而留下空值。我没有费心复制整个代码,因此这可能是由于我编写函数的方式造成的。@VaskoVasilev请查看更新后的答案。请参阅我发布的替代方法。
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();
}
}
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);
}