答案 0 :(得分:3)
int lineCounter = 0;
int variableCounter = 7;
BufferedReader br = new BufferedReader(new FileReader(new File("your-file-here.txt")));
String line = "";
List<String> lineSavers = new ArrayList<String>();
while ((line = br.readLine()) != null) {
lineSavers.add(line);
++lineCounter;
if (lineCounter == variableCounter) {
// do something with the lineSaver
lineSavers.clear();
linteCounter = 0;
// start the cycle again.
}
}
我在这里没有考虑过很多东西:
答案 1 :(得分:1)
您可以使用commons-io一次读取文件,然后遍历列表。一些示例代码:
public static void main(String[] args) {
// Read the content of the file in one go
InputStream input = null;
// This is where the file content is stored
List<String> lines = null;
try {
input = new FileInputStream("input.txt");
lines = IOUtils.readLines(new BufferedInputStream(input));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// Always (try to) close the file
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (lines != null) {
String[] params = null; // per 7 lines
for (int i = 0; i < lines.size(); i++) {
int fromZeroToSixCounter = i % 7;
if ( fromZeroToSixCounter == 0 ) {
if (params != null) {
// the first time (i == 0) params is null and this will not execute
doSomethingWithParams(params);
}
// new batch of 7 lines
params = new String[7];
}
params[fromZeroToSixCounter] = lines.get(i);
}
if (params != null) {
// the last batch
doSomethingWithParams(params);
}
}
}
public static void doSomethingWithParams(String[] params) {
System.out.println("New batch");
for (int i=0; i < params.length; i++) {
System.out.println(params[i]);
}
}
答案 2 :(得分:0)
基本结构将是这样的:
while (true) {
String v1 = reader.readLine();
if (v1 == null)
break;
String v2 = reader.readLine();
...
String v7 = reader.readLine();
/* Do what ever you want with v1 through v7. */
...
}
/* End of loop. */