[英]Reading Text File WIith Scanner (Java)
My Text file is called p1 and it contains:我的文本文件名为 p1,它包含:
p, -4, 5
q, 19, 8
r, 3, 0
x, 7.4, -1
y, -2.3, -16.5
z, 0, 1
My code is:我的代码是:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Driver {
public static void main(String[] args) {
String fileName = "p1.txt";
Scanner inputStream = null;
System.out.println("The file " + fileName + "\ncontains the following lines: \n");
try
{
inputStream = new Scanner(new File(fileName));
}
catch(FileNotFoundException e)
{
System.out.println("Error opening the file " + fileName);
System.exit(0);
}
while(inputStream.hasNextLine())
{
String line = inputStream.nextLine();
System.out.println(line);
}
inputStream.close();
}
}
For some reason my code can't read my text file and gives me the following output:出于某种原因,我的代码无法读取我的文本文件,并为我提供以下输出:
The file p1.txt
contains the following lines:
Error opening the file p1.txt
I'm not sure what I need to do.我不确定我需要做什么。
This is because your file "p1.txt"
does not exists.这是因为您的文件
"p1.txt"
不存在。 Check the location of your file.检查文件的位置。 It should be placed in same directory.
它应该放在同一目录中。
确保 txt 文件与项目在同一个文件夹中,如果没有给出它的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.