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

时间:2017-01-12 04:13:34

标签: java eclipse file-io readfile

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();
    }


}

TextFile

我有一个文本文件,就像帖子一样,程序需要获取与特定代码对应的名称。例如,我输入A11-0002,程序输出将是Lamina,Seth M.如何获得该特定代码的名称? 我的代码在上面,我认为错误的代码在readFile()方法中,我无法获得正确的代码。

4 个答案:

答案 0 :(得分:2)

  1. 您可以使用BufferedReader从文件中读取内容。
  2. 找到第一个空格字符的位置。
  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 :(得分:2)

1)使用fileinputstream读取文件,然后使用缓冲读取器创建文件内存。 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();
        }

    }
}

Output

sample text file

答案 2 :(得分:1)

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

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());
    }
}

答案 3 :(得分:1)

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

<强>输入: -  A11-11

<强>输出: - 员工姓名: - Suraj Kumar

enter image description here

测试数据: - A11-11 Kumar,Suraj A22-11 Laal,Baal 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;
}
    }