从 java 中的文件中读取 N 行?
Reading N lines from a file in java?
我不太确定如何解释我的问题,但我会尽力而为。例如,我有一个包含 100 个数字的文件,是否可以从这个 100 个数字的文件中读取第 25-50 行。
要从头开始读取 N 个数量,我会做这样的事情;
ArrayList<Double> array = new ArrayList<Double>();
Scanner input = new Scanner(new File("numbers.txt"));
int counter = 0;
while(input.hasNextLine() && counter < 10)
{
array.add(Double.parseDouble(input.nextLine()));
counter++;
}
但我不太确定如何从给定的行开始阅读,例如第 25-50 行或 25-75 行或 75-100 行等
非常感谢任何帮助,如果我的问题不清楚,请告诉我。
编辑:
文件中的一些数据:
- 1.45347,1.1545,1.2405
- 1.467,1.4554,1.2233
- 1.4728,1.3299,1.1532
- 1.131,1.5139,1.0044
- 1.4614,1.7373,1.6235
- 1.654,1.5544,1.61147
假设每行有多个号码(号码数量未知):
int start = 25;
int n finish = 50;
String delimit = ",";
List<Double> array = new ArrayList<Double>(finish - start);
Scanner input = new Scanner(new File("numbers.txt"));
int counter = 1;
while(input.hasNextLine() && counter <= finish)
{
String line = input.nextLine();
String[] splits = line.split(delimit);
for (int i=0; i<splits.length; i++){
if (counter >= start && counter <=finish){
array.add(Double.parseDouble(splits[i]));
}
counter++;
}
}
使用 Java 8 你有一个简单的解决方案。请注意,以下代码不会进行任何类型的边界检查(这留作练习):
private static final Pattern COMMA = Pattern.compile(",");
public static List<Double> readNumbers(final String file,
final int startLine, final int endLine)
throws IOException
{
final long skip = (long) (startLine - 1);
final long limit = (long) (endLine - startLine);
final Path path = Paths.get(file);
try (
final Stream<String> stream = Files.lines(path, StandardCharsets.UTF_8);
) {
return stream.skip(skip).limit(limit)
.flatMap(COMMA::splitAsStream)
.map(Double::valueOf)
.collect(Collectors.toList());
}
}
看来问题也不清楚;上面的代码读取给定行范围内的所有双打。如果您想要读取从给定开始 "index" 到给定结束 "index" 的所有双打,您在上面的代码中所要做的就是将 .skip().limit()
的位置更改为之后.map()
.
byte[] inputBytes = "line 1\nline 2\nline 3\ntok 1 tok 2".getBytes();
Reader r = new InputStreamReader(new ByteArrayInputStream(inputBytes));
BufferedReader br = new BufferedReader(r);
Scanner s = new Scanner(br);
System.out.println("First line: " + br.readLine());
System.out.println("Second line: " + br.readLine());
System.out.println("Third line: " + br.readLine());
System.out.println("Remaining tokens:");
while (s.hasNext())
System.out.println(s.next());
并像 Astra 建议的那样添加一个 while 循环
我不太确定如何解释我的问题,但我会尽力而为。例如,我有一个包含 100 个数字的文件,是否可以从这个 100 个数字的文件中读取第 25-50 行。
要从头开始读取 N 个数量,我会做这样的事情;
ArrayList<Double> array = new ArrayList<Double>();
Scanner input = new Scanner(new File("numbers.txt"));
int counter = 0;
while(input.hasNextLine() && counter < 10)
{
array.add(Double.parseDouble(input.nextLine()));
counter++;
}
但我不太确定如何从给定的行开始阅读,例如第 25-50 行或 25-75 行或 75-100 行等
非常感谢任何帮助,如果我的问题不清楚,请告诉我。
编辑:
文件中的一些数据:
- 1.45347,1.1545,1.2405
- 1.467,1.4554,1.2233
- 1.4728,1.3299,1.1532
- 1.131,1.5139,1.0044
- 1.4614,1.7373,1.6235
- 1.654,1.5544,1.61147
假设每行有多个号码(号码数量未知):
int start = 25;
int n finish = 50;
String delimit = ",";
List<Double> array = new ArrayList<Double>(finish - start);
Scanner input = new Scanner(new File("numbers.txt"));
int counter = 1;
while(input.hasNextLine() && counter <= finish)
{
String line = input.nextLine();
String[] splits = line.split(delimit);
for (int i=0; i<splits.length; i++){
if (counter >= start && counter <=finish){
array.add(Double.parseDouble(splits[i]));
}
counter++;
}
}
使用 Java 8 你有一个简单的解决方案。请注意,以下代码不会进行任何类型的边界检查(这留作练习):
private static final Pattern COMMA = Pattern.compile(",");
public static List<Double> readNumbers(final String file,
final int startLine, final int endLine)
throws IOException
{
final long skip = (long) (startLine - 1);
final long limit = (long) (endLine - startLine);
final Path path = Paths.get(file);
try (
final Stream<String> stream = Files.lines(path, StandardCharsets.UTF_8);
) {
return stream.skip(skip).limit(limit)
.flatMap(COMMA::splitAsStream)
.map(Double::valueOf)
.collect(Collectors.toList());
}
}
看来问题也不清楚;上面的代码读取给定行范围内的所有双打。如果您想要读取从给定开始 "index" 到给定结束 "index" 的所有双打,您在上面的代码中所要做的就是将 .skip().limit()
的位置更改为之后.map()
.
byte[] inputBytes = "line 1\nline 2\nline 3\ntok 1 tok 2".getBytes();
Reader r = new InputStreamReader(new ByteArrayInputStream(inputBytes));
BufferedReader br = new BufferedReader(r);
Scanner s = new Scanner(br);
System.out.println("First line: " + br.readLine());
System.out.println("Second line: " + br.readLine());
System.out.println("Third line: " + br.readLine());
System.out.println("Remaining tokens:");
while (s.hasNext())
System.out.println(s.next());
并像 Astra 建议的那样添加一个 while 循环