您需要打开文件并使用 readLine() 遍历每一行,直到到达文件末尾。
- 我假设您在遍历文件时会保持一致性。如果您想存储信息并在以后使用它,我会考虑使用某种类型的数据结构。
当您遍历它时,您可以使用简单的正则表达式检查该行以检查它是否是标签名称。如果没有,请根据 ' '(空格字符)拆分行,它将以数组的形式返回给您。然后根据一致的尺寸检查尺寸。
基本伪代码:
int consistentSize = 5; // assume you have a size in mind
while ( (line = readLine()) != EOF)
{
// check for if label, if it's a simple name, you won't really need a regex
if (line == label)
{
// not sure if you want to do any consistency checking in here
} else {
String[] currLine = line.split(' ');
bool consist = true;
// now loop through currLine and do a check if each character is a number
for (int i = 0; i < currLine.size(); i++)
{
// can't remember java function for this (isNum() I think)
if (!currLine[i].isNum) { consist = false; break; }
}
// if got past this, the row has all numbers, therefore it is ok
// could easily add another array to keep track of rows that didn't have valid numbers and suhc
if (currLine.size() < consistentSize) System.out.println("row "+j + " is inconsistent");
}
}
如果您不知道每行的预期大小,您还可以添加另一个循环,并放入一些逻辑以找到最常见的大小,然后找出不匹配的内容。我不确定您的一致性检查需要有多复杂。