0

I have a text file as below.

Name        :Kiran   
Id      :1000  
Address     :Karol bagh Delhi  
Contact No. :9876612338  
Band        :L1  
Salary      :20000  
Project     :USM  
Type        :Permanent

Name        :Tarun  
Id      :1001  
Address     :Tarol begh Kolktta  
Contact No. :9876692338  
Band        :L2  
Salary      :30000  
Project     :GSM  
Type        :Permanent

Name        :Pavan  
Id      :1002  
Address     :Cottonpet Bangalore  
Contact No. :9889612338       
Band        :L3  
Salary      :40000  
Project     :PKM   
Type        :Contract

I want to read Id & salary for every employee's record?How can I do this using file I/O or any other way?

4
  • What do you have so far? Nov 27, 2013 at 11:42
  • 2
    1. What have you tried? 2. Google it. 3. Load the data into memory i.e. a 2D array or (even better) map it to an object. 4. "get" what you want from populated objects. 5. Google it. 6. Google it.
    – Ben Dale
    Nov 27, 2013 at 11:43
  • Is this javaee related? Nov 27, 2013 at 11:43
  • I have tried file I/O in java,which gives all the data.But I want only salary & Id for each employee.As I want to print the information on UI,I am using servlet for it. Nov 27, 2013 at 11:54

3 Answers 3

0
public class EmployeeReader {

    public static void main(String[] args) throws IOException {
        File file = new File("employees.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));

        String line;
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        Map<String, String> map = new HashMap<String, String>();
        while ((line = br.readLine()) != null) {
            if (!line.trim().equals("")) {
                String[] tokens = line.split(":");
                if (tokens[0].trim().equals("Name") || tokens[0].trim().equals("Salary")) {
                    map.put(tokens[0].trim(),tokens[1].trim());                 
                }
            } else {
                list.add(map);
                map = new HashMap<String, String>();
            }
        }
        list.add(map);

        printList(list);
        br.close();
    }

    public static void printList(List<Map<String, String>> list) {
        for (Map<String, String> employee : list) {
            System.out.println("EMPLOYEE");
            for (Entry<String, String> entry : employee.entrySet()) {
                System.out.println("Key: " + entry.getKey());
                System.out.println("Value: " + entry.getValue());
            }
        }   
    }
}
2
  • Hey it worked..thank you so much..Do you know any simpler solution than this?As I am a beginner, first I would like to learn easy things. Nov 27, 2013 at 13:08
  • @user2741604 What parts do you consider advanced? The file i/o (reading the file) is necessary. I guess the Map/List are slightly complex, but they will make life a lot easier. Nov 27, 2013 at 17:44
0

This will help you

public void readData() {
    String filePath = "/home/sharathgc/IdeaProjects/src/abc";
    File file = new File(filePath);
    String line = null;
    String pattern = "((Id :)(\\d)*)|(Salary :)(\\d)*";
    Pattern r = Pattern.compile(pattern);

    try {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        while ((line = reader.readLine()) != null) {
            Matcher m = r.matcher(line);
            if (m.find()) {
                System.out.println(m.group());
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    } catch (IOException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
}
4
  • Thank you for help.But its not working.Program is not getting terminated & not printing anything on the console. Nov 27, 2013 at 12:55
  • Just an added trick, create the Matcher outside of the loop, Matcher matcher = Pattern.compile("").matcher(""). Then inside the loop simply reset the matcher before m.find() like this matcher.reset(line). If you have to match thousands or tens of thousands of things, it's faster not to create and throw away a Matcher on every iteration.
    – brettw
    Nov 27, 2013 at 12:56
  • The regex was a little off, I'd use: (Id\\s*:)(\\d*)|(Salary\\s*:)(\\d*)
    – brettw
    Nov 27, 2013 at 13:01
  • @brettw thats a better regex. Thanks for the improvisation. Nov 27, 2013 at 15:05
0
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmployeeData{
public static void main(String args[]){
   EmployeeData e=new EmployeeData();
   e.readData();
}
public void readData(){
String filePath = "Employees";
File file = new File(filePath);
String line = null;
String pattern = "(Id\\s*:)(\\d*)|(Salary\\s*:)(\\d*)";
Matcher matcher=Pattern.compile(pattern).matcher("");

try {
    BufferedReader reader = new BufferedReader(new FileReader(file));
    while ((line = reader.readLine()) != null) {
        matcher=matcher.reset(line);
        if (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();  //To change body of catch statement use File | Settings |    File Templates.
} catch (IOException e) {
    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
}
}
}
1
  • I have made the necessary modifications, now u can this should work. Nov 27, 2013 at 15:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.