Java 如何在eclipse输出中显示文本文件?
我有一个文本文件,就像在帖子上一样,程序需要获得与特定代码对应的名称。例如,我放置了A11-0002,程序输出将是Lala,Seth M。我如何为那个特殊的代码获得那个名称?Java 如何在eclipse输出中显示文本文件?,java,eclipse,file-io,readfile,Java,Eclipse,File Io,Readfile,我有一个文本文件,就像在帖子上一样,程序需要获得与特定代码对应的名称。例如,我放置了A11-0002,程序输出将是Lala,Seth M。我如何为那个特殊的代码获得那个名称? 我的代码在上面,我认为readFile()方法中的代码是错误的,我无法获得正确的代码。您可以基于字段分隔符解析文件及其行记录,并将详细信息作为empcode存储在map-key中,将值作为employee details存储在map-key中。通过这种方式,您可以通过将empcode作为键传递给map来获取employee
我的代码在上面,我认为readFile()方法中的代码是错误的,我无法获得正确的代码。您可以基于字段分隔符解析文件及其行记录,并将详细信息作为empcode存储在map-key中,将值作为employee details存储在map-key中。通过这种方式,您可以通过将empcode作为键传递给map来获取employeedetails
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();
}
}
导入java.io.File;
导入java.util.HashMap;
导入java.util.Map;
导入java.util.Scanner;
公共类案件1A{
专用静态字符串行分隔符=“\\t”;
公共静态void main(字符串[]args)引发异常{
File File=新文件(“C:\\temp\\Employee.txt”);
Map employeeMap=newhashmap();
字符串lineData=null;
字符串[]empDetails=新字符串[10];
扫描仪sc=新扫描仪(文件);
while(sc.hasNextLine()){
lineData=sc.nextLine();
empDetails=lineData.split(行分隔符);
if(empDetails!=null&&empDetails.length>=2){
employeeMap.put(empDetails[0],empDetails[1]);
}
}
sc.close();
System.out.println(employeeMap.toString());
}
}
BufferedReader
从文件中读取内容str.substring()
从该索引中分割行。现在你有两个字符串employees.get(“A11-0003”)
获取它即可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());
}
}
代码:
package测试;
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.HashMap;
导入java.util.Map;
公共类案件1A{
私有静态映射employees=newhashmap();
公共静态void main(字符串[]args)引发IOException{
setEmployeeData();
System.out.println(employees.get(“A11-0003”);
}
//从文件设置员工数据以映射员工
私有静态void setEmployeeData()引发IOException{
BufferedReader br=null;
字符串行[]=新字符串[2];
int空间索引;
试一试{
弦电流线;
br=新的BufferedReader(新的文件阅读器(“C:\\fakelocation\\employee.txt”);
而((sCurrentLine=br.readLine())!=null){
spaceIndex=sCurrentLine.indexOf(“”);
行[0]=sCurrentLine.substring(0,spaceIndex);
行[1]=sCurrentLine.substring(spaceIndex+1);
雇员。put(第[0]行,第[1]行);
}
}catch(ioe){}finally{
如果(br!=null)br.close();
}
}
}
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);
}
我已尝试编写您希望代码执行的操作:- 输入:- A11-11 输出:- 员工姓名:-Suraj Kumar 测试数据:- 苏拉杰库马尔A11-11 A22-11拉尔,巴力 A33-33泰瑞,华纳
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();
}
}
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.*;
公共类解析文本{
静态LinkedList=新建LinkedList();
公共静态void main(字符串[]args)引发异常{
readFile();
getEmployee(“A11-11”);//这是为了测试,您可以得到
//从命令行读取id
}
public static void readFile()引发FileNotFoundException{
File File=新文件(“C:\\test\\textTest.txt”);
扫描仪sc=新扫描仪(文件);
字符串温度;
while(sc.hasNext()){
温度=sc.nextLine();
//系统输出打印项次(“温度”+温度);
字符串[]s=临时拆分(“”);
字符串[]名称=s[1]。拆分(“,”;
字符串id=s[0];
字符串lastName=name[0];
字符串firstName=name[1];
员工emp=新员工(名字、姓氏、id);
列表。添加(emp);
}
sc.close();
}
公共静态void getEmployee(字符串id){
迭代器itr=list.Iterator();
while(itr.hasNext()){
员工emp=itr.next();
如果(emp.id.equals(id)){
System.out.println(“员工姓名:-“+emp.firtName+”+emp.lastName);
}
}
}
}
班级员工{
字符串firtName;
字符串lastName;
字符串id;
员工(字符串名、字符串名、字符串id){
this.firtName=firstName;
this.lastName=lastName;
this.id=id;
}
公共字符串getFirtName(){
返回firtName;
}
公共字符串getLastName(){
返回姓氏;
}
公共字符串getId(){
返回id;
}
}
您的代码有什么问题?你的<代码>主< /代码>方法?考虑执行<代码> EngEnEnaby= SCNCOND.NEXT LITER()/<代码>以获取整个名称。个人而言,我将读取整行,然后根据代码和名称之间的任何字符进行拆分,如果行记录在EMP代码之后只有一个空间,则会出现问题。例如,像A11-230I这样的行记录我刚刚给了一个开始的位置。根据不同的输入,可以在代码中进行许多修改;类似于(i)修剪子字符串的结果,(ii)将try catch
块放置在行[1]=sCurrentLine.substring(spaceIndex+1)上
查找NullPointerException
等…映射的含义是什么?它的目的是什么?我只是一个编程新手。参考这里-它有键,值组合和键是唯一的。想象一下像个前男友
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();
}
}
}
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;
}
}