Java 如何使用 FileReader 逐行读取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33892453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read line by line by using FileReader
提问by Ender phan
Thank you for your attention.
感谢您的关注。
I have created a program which I'm using a Login form and Register form.
Once users register their email and their password will be saved it into submit.txt
. Then they will turn back to login form and enter their email and password which are saved into submit.txt
.
我创建了一个程序,我正在使用登录表单和注册表单。一旦用户注册了他们的电子邮件,他们的密码就会被保存到submit.txt
. 然后他们将返回登录表单并输入保存在submit.txt
.
In my code, I'm using write file for Register form, and Read file for Login Form. But, it doesn't work. I know my problem in the code used for Read File. May you help me to realize that?.
在我的代码中,我对注册表单使用写入文件,对登录表单使用读取文件。但是,它不起作用。我知道我在用于读取文件的代码中的问题。你能帮我意识到这一点吗?
Thank you too much for your help.
非常感谢您的帮助。
if (checkPassword(usern, hash)) {
System.out.println("Logged in!");
ChooseWindow ptb = new ChooseWindow();
ptb.setVisible(true);
LoginWindow.this.dispose();
} else {
System.out.println("Wrong password");
}
public boolean checkPassword(String username, String pass) {
try {
FileReader inFile = new FileReader("/users/Ender/Desktop/GetUser/submit.txt");
BufferedReader inStream = new BufferedReader(inFile);
String inString;
while ((inString = inStream.readLine()) != null) {}
inStream.close();
}catch (IOException e) {
e.printStackTrace();
}
return false;
}
采纳答案by Ahmad Alkhatib
here is my code to read from a file :
这是我从文件中读取的代码:
String line;
try {
BufferedReader bufferreader = new BufferedReader(new FileReader("C:\Users\ahmad\Desktop\aaa.TXT"));
while ((line = bufferreader.readLine()) != null) {
/**
Your implementation
**/
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
回答by Ramesh-X
You can read a file using following code..
您可以使用以下代码读取文件..
try {
BufferedReader bufferreader = new BufferedReader(new FileReader("./users/Ender/Desktop/GetUser/submit.txt"));
String line;
while ((line = bufferreader.readLine()) != null) {
// line variable contains the readed line
// You can append it to another String to get the whole text or anything you like
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
If you want to write file use following code..
如果你想写文件使用下面的代码..
BufferedWriter writer = new BufferedWriter(new FileWriter("./users/Ender/Desktop/GetUser/submit.txt"));
writer.write(your_text);
writer.close();
If you want to append text use following code to create the instance of BufferedWriter
如果要附加文本,请使用以下代码创建 BufferedWriter
BufferedWriter writer = new BufferedWriter(new FileWriter("/users/Ender/Desktop/GetUser/submit.txt", true));
回答by Aracurunir
Assuming we have a file with username
and hash
stored as follows:
假设我们有一个文件username
并hash
存储如下:
Hello World
Test Test
DumbUser 12345
User sjklfashm-0998()(&
Hello World
测试测试
DumbUser 12345
用户 sjklashm-0998() (&
and we want to use the first word in every line as username
and the second as password
/hash
. Then the idea is:
我们想在每一行中使用第一个单词 as username
,第二个单词作为password
/ hash
。那么思路是:
- Read a line
- Split the line into parts at " "
- Compare the first part to
username
and the second topass
- If there is a match return true, else start over
- 读一行
- 在“ ”处将线分成几部分
- 比较第一部分
username
和第二部分pass
- 如果有匹配返回true,否则重新开始
Which results in this code:
这导致此代码:
public boolean checkPassword(String username, String pass) {
// if there is no username or no password you can not log in
if(username == null || pass == null){ // diff
return false; // diff
} // diff
try {
FileReader inFile = new FileReader(PASSWORD_FILE);
BufferedReader inStream = new BufferedReader(inFile);
String inString;
while ((inString = inStream.readLine()) != null) {
// we split every line into two parts separated by a space " "
String usernameHash[] = inString.split(" "); // diff
// if there are more or less than two parts this line is corrupted and useless
if(usernameHash.length == 2 // diff
&& username.equals(usernameHash[0]) // diff
&& pass.equals(usernameHash[1])){ // diff
// if username matches the first part and hash the second everything is goo
return true; // diff
} // diff
}
inStream.close();
}catch (IOException e) {
e.printStackTrace();
}
return false;
}
I marked the parts where my code differs from yours with // diff
我标记了我的代码与您的代码不同的部分 // diff