Get data from notepad and display it.
Hi Smith,
Here is a very simple solution for you.
Â
import java.io.*;
class text_readerÂ
{
 public static void main(String args[])
 {
Â
   try{
Â
    // open the text file
    // you can specify a full path if the fileÂ
    // is not located on the same folder
     FileInputStream fstream = new FileInputStream("textfile.txt");
     // inialize input stream
     DataInputStream dstream = new DataInputStream(fstream);
     BufferedReader reader = new BufferedReader(new InputStreamReader(dstream));
     String strLine;
     String fields [];
     fields = new String[6];
     // fields
     fields[0] = "Employee Code";
     fields[1] = "Employee name";
     fields[2] = "Age";
     fields[3] = "Position";
     fields[4] = "Rate per hour";
     fields[5] = "Country";
     System.out.println("Enter employee code:");
     InputStreamReader input = new InputStreamReader(System.in);
     BufferedReader inputreader = new BufferedReader(input);
     String sEmpSearchCode = inputreader.readLine();
     boolean bFound = false;Â
     //read File Line By Line
     while ((strLine = reader.readLine()) != null)
     {
    Â
       // tokenize the string, match 1st element with our search string
       String[] tokens = strLine.split("\,");
       String sEmpCode = tokens[0];
       if(sEmpCode.compareTo(sEmpSearchCode) == 0)
       {
         // we found it
         // print it
         bFound = true;
         for (int index=0; index<tokens.length; index++)
         System.out.println(fields[index] + " = " + tokens[index]);
         break;
       }
      Â
     }
      //close the input stream
      dstream.close();
   Â
    if(!bFound)
      System.out.println( sEmpSearchCode + " not found");
    }
    catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }
  }
}
Â
Create a textfile named "textfile.txt" on the same folder with your java program. The format is:
Â
110011,John Shy.,20,Secretary,5.00$,USA
110100,Jon Doe,22,VP,100.00$,USA
Â
The program reads it line by line and split the data using comma.Â
Â
I hope it helps.