gpt4 book ai didi

java - 无法从文本文件读取数据

转载 作者:太空宇宙 更新时间:2023-11-04 12:42:24 24 4
gpt4 key购买 nike

我有一个名为 pwd.txt 的文本文件,位于 projectfolder->src->pwd.txt我按如下方式访问该文件:

File f1 = new File("src\\pwd.txt");
if(f1.exists()==false){
System.out.println("file not");
}
FileReader fr = new FileReader(f1);
while ((ch = fr.read()) != -1) {
pass = pass + (char) ch;
}
LoginForm.pwd = pass;
fr.close();

好吧,首先让我告诉您,在 IDE (Eclipse) 中运行时我可以访问此文件,但是当我创建 jar 文件时,我无法访问该文件pwd.txt。当我看到该 jar 文件的内容时,我可以看到文件 pwd.txt

最佳答案

尝试以下代码,希望对您有所帮助。

BufferedReader br = null;

//first open the file
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream("src\\pwd.txt")));
} catch (FileNotFoundException e) {
System.out.println("File not found!");
return;
}

try {
String line = br.readLine();
while (line != null) {
System.out.println(line);
line = br.readLine();
}
br.close();
} catch (IOException e) {
System.out.println("ERROR:"+e);
}

关于java - 无法从文本文件读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36701299/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com