I would like to search a simple text file: for instance let's see the following is the contents of my text file...
Edward Password123
Cedrick pASSWORD321
How do I parse this so that I give my scanner object a username and it finds the respective password? Each username and password are on seperate lines all will be divided by a " " ( a space). How do I go about doing such a thing?
Here's what I have so far...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| package test;
import java.io.*; import java.util.*;
public class TestFileReader { public void readFile(String fileName) { try { File file = new File(fileName); Scanner scan = new Scanner(file); while (scan.hasNextLine()) { System.out.println(scan.nextLine()); } } catch (FileNotFoundException e) { System.out.println("File not found."); } }
public void writeToFile(String fileName, String Username, String Password) { try { FileWriter writer = new FileWriter(fileName, true); writer.write("\n" + Username + " " + Password); writer.flush(); } catch (IOException e) {} } public void getPassword(String fileName, String Username) { try { File file = new File(fileName); Scanner scan = new Scanner(file); } catch (FileNotFoundException e) { System.out.println("File not found."); } } } |