我已经制作了一个程序来检查用户名和&密码是否正确,但我想打开一个具有已定义路径的文件,如果它们是正确的。我怎么做?这就是我尝试过的:
import java.util.Scanner;
import java.io.*;
public class Login {
public static void main(String[] args) {
int pass;
String un;
String file_name = "C:/Users/Username/Desktop";
System.out.println("Welcome, please enter the username and password");
Scanner scan = new Scanner(System.in);
System.out.println("Username?:");
un = scan.nextLine();
System.out.println("Password?:");
pass = scan.nextInt();
if (un.equals("Admin") && pass == 123) {
System.out.println("That's correct");
Readfile file = new Readfile(file_name);
String[] aryLines = file.OpenFile();
} else {
System.out.println("You ain't the Admin");
}
}
}
登录检查工作正常,但我无法打开该文件。它除其他外说Readfile cannot be resolved to a type
答案 0 :(得分:0)
这是因为ReadFile没有解析为因为没有这样的类而输入。
您可以使用BufferedReader和FileReader来读取文本文件。如下:
BufferedReader br = null;
try {
String line;
br = new BufferedReader(new FileReader("C:\\testing.txt"));
while ((line= br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
答案 1 :(得分:-1)
String file_name = "C:/Users/Username/Desktop";
不是文件名,而是文件夹名称。你需要添加类似的东西:
String file_name = "C:/Users/Username/Desktop/filename.txt";
此外,在您这样做之后,您应该检查您的代码,从互联网上的示例中受益。我没有尝试,但看起来会有一些错误;最后一个是我对你的建议。