如何让文本文件显示在 eclipse 输出中?

How to get a textfile to be showed in eclipse output?

import java.io.File;
import java.util.Scanner;

import java.io.*;

public class Case1A {

    private static Scanner scnCode;

    public static void openFile() {
        try{
            scnCode = new Scanner(new File("employee.txt"));
        }catch(Exception e){
            System.out.println("You've got an error!");
        }
    }

    public static void readFile(String EmpCode){
        while(scnCode.hasNext()) {
            String EmployeeCode = scnCode.next();
            String EmployeeName = scnCode.next();

        System.out.printf("Employee Name: %s\n", EmployeeName);
        }
    }

    public static void closeFile() {
        scnCode.close();
    }


}

我有一个和post一样的文本文件,程序需要获取对应于特定代码的名称。例如,我输入 A11-0002,程序输出将是 Lamina, Seth M。我怎样才能得到那个特定代码的名称? 我的代码在上面,我认为错误的代码在readFile()方法中,我无法得到正确的代码。

您可以根据字段分隔符解析文件及其行记录,并将详细信息存储在map中-key为empcode,value为员工详细信息。这样,您可以通过将 empcode 作为键传递给映射来获取员工详细信息。

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Case1A {
    private static String LINE_SEPERATOR = "\t";

    public static void main(String[] args) throws Exception {
        File file=new File("C:\temp\Employee.txt");
        Map<String, String> employeeMap = new HashMap<>();
        String lineData = null;
        String[] empDetails = new String[10];
        Scanner sc = new Scanner(file);
        while(sc.hasNextLine()){
            lineData = sc.nextLine();
            empDetails = lineData.split(LINE_SEPERATOR);
            if(empDetails != null && empDetails.length >= 2){
                employeeMap.put(empDetails[0],empDetails[1]);
            }
        }
        sc.close();
        System.out.println(employeeMap.toString());
    }
}
  1. 您可以使用BufferedReader从文件中读取内容。
  2. 找到第一个 space 字符的位置。
  3. 使用 str.substring() 从该索引中划分线。现在你有 2 个字符串。
  4. 你可以把这个键值对放在一个Map中。
  5. 每次您需要映射中的键值时,只需使用 employees.get("A11-0003").
  6. 获取它

输出:

Roda, Ronamy M.

代码:

package apachecommonstest;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Case1A {
    private static Map<String, String> employees = new HashMap<>();

    public static void main(String[] args) throws IOException {
        setEmployeeData();
        System.out.println(employees.get("A11-0003"));
    }

    //set employee data from file to Map employees
    private static void setEmployeeData() throws IOException {
        BufferedReader br = null;
        String line[] = new String[2];
        int spaceIndex;
        try {
            String sCurrentLine;
            br = new BufferedReader(new FileReader("C:\fakelocation\employee.txt"));
            while ((sCurrentLine = br.readLine()) != null) {
                spaceIndex = sCurrentLine.indexOf(" ");
                line[0] = sCurrentLine.substring(0, spaceIndex);
                line[1] = sCurrentLine.substring(spaceIndex+1);
                employees.put(line[0], line[1]);
            }
        } catch (IOException e) {} finally {
            if(br!=null)br.close();
        }
    }
}

1) 使用fileinputstream 读取文件,然后使用缓冲区reader 创建文件内存。 2)使用读取行读取文件中的行。 3) 我使用子字符串创建了分隔线,然后进行比较。

主要:

 public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Please Enter: ");
            String in = scanner.next();
            TextFile t = new TextFile();
            t.Employee(in);
        }

节目:

public class TextFile {
    public void Employee(String in) {
        FileInputStream f;
        try {
            f = new FileInputStream("D:/sample1.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(f));
            String strLine;
            int space;
            while ((strLine = br.readLine()) != null) {
                space = strLine.indexOf(" ");
                String s=strLine.substring(0,space);
                String s1=strLine.substring(space+1);
                if(in.equals(s)){
                    System.out.println(s1.trim());
                }
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

我已经尝试编写您希望您的代码执行的操作:-

输入:- A11-11

输出:- 员工姓名:- Suraj Kumar

enter image description here

测试数据:- A11-11 库马尔,苏拉杰 A22-11 拉尔、巴尔 A33-33 泰瑞,华纳

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.*;


    public class ParseText {
static LinkedList<Employee> list = new LinkedList<>();

public static void main(String[] args) throws Exception{
    readFile();
    getEmployee("A11-11"); //This is for test, you get the
                            //read the id from command line as well

     }
public static void readFile() throws FileNotFoundException {
    File file=new File("C:\test\textTest.txt");
    Scanner sc = new Scanner(file);    
    String temp;
    while(sc.hasNext()){
        temp = sc.nextLine();
        //System.out.println("temp "+ temp);
        String[] s = temp.split(" ");
        String[] name = s[1].split(",");
        String id = s[0];
        String lastName = name[0];
        String firstName = name[1];
        Employee emp = new Employee(firstName, lastName, id);
        list.add(emp);
    }
    sc.close();
     }

public static void getEmployee(String id) {
    Iterator<Employee> itr = list.iterator();
    while(itr.hasNext()) {
        Employee emp = itr.next();
        if(emp.id.equals(id)){
            System.out.println("Employee Name:- "+emp.firtName+" "+emp.lastName);
        }
    }
}
    }

    class Employee {
String firtName;
String lastName;
String id;

Employee(String firstName, String lastName, String id) {
    this.firtName = firstName;
    this.lastName = lastName;
    this.id = id;
}

public String getFirtName() {
    return firtName;
}

public String getLastName() {
    return lastName;
}

public String getId() {
    return id;
}
    }