Java 试图从文件中读取数据(文件每两行包含换行符)
首先,这是数据文件的外观 5165168416516988798484984984984984Java 试图从文件中读取数据(文件每两行包含换行符),java,file-io,Java,File Io,首先,这是数据文件的外观 5165168416516988798484984984984984 98798798798 1556516846516518498 516884979789798798798491 我需要在换行之前读取两行(我可以很好地完成),然后继续逐行读取,直到文件结束。有一个偶数对的保证,所以不会有一个单独的线结束。我的问题是试图读取超过前两行的文件 code try { Scanner input = new Scanner (file); while (in
98798798798
1556516846516518498
516884979789798798798491 我需要在换行之前读取两行(我可以很好地完成),然后继续逐行读取,直到文件结束。有一个偶数对的保证,所以不会有一个单独的线结束。我的问题是试图读取超过前两行的文件
code
try {
Scanner input = new Scanner (file);
while (input.hasNextLine()) {
String number1 = input.nextLine();
String number2 = input.nextLine();
System.out.println("String: " + number1);
System.out.println("String: " + number2);
System.out.println(number1 + " " + number2);
number1= input.nextLine();
number2= input.nextLine();
}
}
这就是我到目前为止所做的。它只在完全没有换行符的情况下工作,尽管它仍然在末尾发出异常(“未找到行”)
一旦在文件中找到第一个中断,就会发生此异常。如何使整个文件以我希望的方式读取?(抓住这两行,对它们做点什么。抓住下两行,做点什么……一直到最后)看看修改过的代码,特别是带有注释的代码行
try {
Scanner input = new Scanner (file);
while (input.hasNextLine()) {
String number1 = input.nextLine();
String number2 = input.nextLine();
input.nextLine(); //this takes care of empty line
System.out.println("String: " + number1);
System.out.println("String: " + number2);
System.out.println(number1 + " " + number2);
}
}
这使用了另一种每行读取文本文件的方法:
try {
BufferedReader newBufferedReader = Files.newBufferedReader(Paths.get("PathToTextFile.txt"), Charset.defaultCharset());
while (newBufferedReader.ready()) {
System.out.println(newBufferedReader.readLine());
}
newBufferedReader.close();
} catch (IOException ex) {
Logger.getLogger(NewJFrame1.class.getName()).log(Level.SEVERE, null, ex);
}
嗯。让我们试试看
try {
Scanner input = new Scanner (file);
// input.hasNextLine() checks if there are more lines that can be read from
// the file. If there is a line, hasNextLine() will return true, and the code
// inside the while block will be executed.
// Then execution will come back here to perform the check again. This
// goes on until there are no more lines to consume.
while (input.hasNextLine()) {
// We are going to read two numbers from file.
// Ideally, we should check if there a line available before reading
// each of these lines. But since you confirm that the file will
// always have multiple number of triplets containing two lines
// with numbers, followed by a blank line, we are not going to
// call input.hasNextLine() here.
String number1 = input.nextLine();
String number2 = input.nextLine();
// Now, two numbers are read, read the next (empty) line to a variable
// but we will not use it anywhere.
String emptyLine = input.nextLine();
// ...or we could read it, but just discard it without assigning it to a variable
// input.nextLine();
// Print what we read to the output, like a boss.
System.out.println("String: " + number1);
System.out.println("String: " + number2);
System.out.println(number1 + " " + number2);
// This is not needed here (see below to understand why):
//number1= input.nextLine();
//number2= input.nextLine();
// after the following brace (}), execution will go back to the start
// of the while loop. and if there are more lines to be read, code
// inside while loop will be executed again.
}
}
希望这能把事情弄清楚
警告:要使此代码正常工作,输入文件中最后一个数字对后必须包含一个空行。否则,它将抛出一个
NoTouchElementException
我已经生成了完全工作的代码。当此代码读取超过最后一行时,将引发异常NoTouchElementException
但是,我们捕获此异常并让用户知道已到达文件末尾。或者,您也可以什么都不做。但是,这并不好
import java.io.File;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class Filez {
public static void main(String[] args) {
File file = null;
Scanner input = null;
try {
file = new File("C:/Temp/two.txt");
input = new Scanner(file);
String line = "";
int count = 0;
while (true) {
for(count = 1; count < 3; count++){
line = line + input.nextLine() + ", ";
}
System.out.println(line);
input.nextLine();
line = "";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(NoSuchElementException e){
System.out.println("\n\nReached end of file...\n");
}
}
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.NoSuchElementException;
导入java.util.Scanner;
公共类文件{
公共静态void main(字符串[]args){
File=null;
扫描仪输入=空;
试一试{
file=新文件(“C:/Temp/two.txt”);
输入=新扫描仪(文件);
字符串行=”;
整数计数=0;
while(true){
对于(计数=1;计数<3;计数++){
line=line+input.nextLine()+“,”;
}
系统输出打印项次(行);
input.nextLine();
第“”行;
}
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(无接触元素例外e){
System.out.println(“\n\n已获取文件结尾…\n”);
}
}
}
您可以忽略空行,不管它们是否存在
public static String readNonBlankLine(Scanner in) {
while(in.hasNextLine()) {
String s = in.nextLine();
if (!s.trim().isEmpty())
return s;
}
return null;
}
try {
Scanner input = new Scanner (file);
while (input.hasNextLine()) {
String number1 = readNonBlankLine(input);
String number2 = readNonBlankLine(input);
System.out.println("String: " + number1);
System.out.println("String: " + number2);
System.out.println(number1 + " " + number2);
}
}
我认为OP的问题是他/她不能了解
循环在Java中如何工作。我怀疑这在现阶段对他/她是否有帮助。请看我的答案。它有一个完整的工作程序。你只需要提供文件和文件路径就可以让我的代码正常工作。每一行都有一个换行符,这就是它成为一行的原因。你可以说每三行都是空白的。