如何从java中的文本文件中读取特定字段?

时间:2013-11-27 11:41:12

标签: java

我有一个文本文件如下。

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

我想阅读Id&每个员工记录的薪水?我怎样才能使用文件I / O或其他任何方式?

3 个答案:

答案 0 :(得分: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());
            }
        }   
    }
}

答案 1 :(得分:0)

这会对你有所帮助

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.
    }
}

答案 2 :(得分: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.
}
}
}