如何使用FileReader逐行读取
感谢您的关注。
我创建了一个程序,我正在使用登录表单和注册表单。 一旦用户注册他们的电子邮件,他们的密码将被保存到submit.txt
。 然后他们将返回登录表单并输入保存在submit.txt
的电子邮件和密码。
在我的代码中,我使用Register文件的写文件和Login Form的Read文件。 但是,它不起作用。 我在用于读取文件的代码中知道我的问题。 你可以帮我实现吗?
非常感谢你的帮助。
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; }
这是我从文件中读取的代码:
String line; try { BufferedReader bufferreader = new BufferedReader(new FileReader("C:\\Users\\ahmad\\Desktop\\aaa.TXT")); while ((line = bufferreader.readLine()) != null) { /** Your implementation **/ line = bufferreader.readLine(); } } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); }
假设我们有一个username
和hash
存储的文件如下:
你好,世界
测试一下
DumbUser 12345
用户sjklfashm-0998() ( &
我们希望将每行中的第一个单词用作用username
,将第二个单词用作password
/ hash
。 那个想法是:
- 读一行
- 将线分成“”部分
- 比较第一部分和
username
,第二部分pass
- 如果匹配返回true,则重新开始
这导致以下代码:
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; }
我用// diff
标记了我的代码与你的代码不同的部分
您可以使用以下代码读取文件。
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(); }
如果你想编写文件使用以下代码..
BufferedWriter writer = new BufferedWriter(new FileWriter("./users/Ender/Desktop/GetUser/submit.txt")); writer.write(your_text); writer.close();
如果要附加文本,请使用以下代码创建BufferedWriter
实例
BufferedWriter writer = new BufferedWriter(new FileWriter("/users/Ender/Desktop/GetUser/submit.txt", true));