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…
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."); }
}
}