当前位置: 首页 > 知识库问答 >
问题:

读取两个文本文件并将这两个文本文件中的特定行写入第三个文件

燕刚毅
2023-03-14

我正在创建一个医院管理系统,其中我有2个类,即Add医生和Add病人,从用户那里输入他们的详细信息,并将其存储到各自的文件中。我现在想创建一个约会类,在这个类中,我可以将具有特定ID的患者分配给从文件中读取的具有特定ID的医生。如果Java支持多重继承,这将非常容易,但是因为它不支持,所以我无法完成这项任务。

下面是我的AddDoctor课程

class AddDoctor{
    int did;
    int dage;
    long dphno;
    String dname;
    String dgender;
    String dqualification;

    InputStreamReader in = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(in);

    void input() throws IOException{
        System.out.print("Enter Doctor's Name:");
        dname = br.readLine();

        Random rand = new Random();
        did = rand.nextInt((9999 - 100) + 1) + 10;

        System.out.print("Enter Doctor's Phone Number:");
        dphno = Long.parseLong(br.readLine());

        System.out.print("Enter Doctor's Age:");
        dage = Integer.parseInt(br.readLine());

        System.out.print("Enter Doctor's Gender:");
        dgender = br.readLine();

        System.out.print("Enter Doctor's Qualification:");
        dqualification = br.readLine();
    }

    void delete() throws FileNotFoundException, IOException{
        Scanner in = new Scanner(System.in);
        File inputFile = new File("DoctorDetails.txt");
        File tempFile = new File("myTemp.txt");

        BufferedReader reader = new BufferedReader(new FileReader(inputFile));
        BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

        String currentLine;

        String lineToRemove;
        System.out.println("Enter the ID of the Doctor you wish to delete: ");
        lineToRemove = in.next();

        while((currentLine = reader.readLine()) != null) {
            String trimmedLine = currentLine.trim();
            if(trimmedLine.startsWith(lineToRemove)) continue;          
            writer.write((currentLine) + System.getProperty("line.separator"));
        }
        writer.close();
        reader.close();
        Files.move(tempFile.toPath(), inputFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
    }

    void search() throws IOException{
        Scanner scan=new Scanner(System.in);
        System.out.println("Enter the ID of the Doctor To Search:");
        String did=scan.next();
        String line="";
        try{
            FileInputStream fin = new FileInputStream("DoctorDetails.txt");
            Scanner sc = new Scanner(fin);
            while(sc.hasNextLine()){
                line=sc.nextLine();
                if(line.startsWith(did))
                    System.out.println(line+" ");
            }
            sc.close();
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }
    
    void display(){
        try{
            BufferedReader br=new BufferedReader(new FileReader("DoctorDetails.txt"));
            String s="";
            while((s=br.readLine())!=null){
                String data[]=new String[6];
                data=s.split(" ");
                for(int i=0;i<6;i++){
                    System.out.print(data[i]+"\t");
                }
                System.out.println();
            }
            br.close();
        }
        catch(Exception e){
        }
    }
};


//Class WriteD to Write Doctor Details in a text file where the details are fetched from the Class AddDoctor
class WriteD extends AddDoctor {
        void write() {
            try(FileWriter fw = new FileWriter("DoctorDetails.txt",true);
                BufferedWriter bw = new BufferedWriter(fw);
                PrintWriter out = new PrintWriter(bw))
                {
                    out.println(did + " " + dname + " " + dphno + " " + dage + " " + dgender + " " + dqualification);
                }catch(IOException e){
                e.printStackTrace();
                }
        }
    };

下面是我的AddPatient课程

package patient;

import java.io.*;
import java.util.*;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;


public class AddPatient{
    int pid;
    int page;
    long pphno;
    String pname;
    String pgender;
    String pillness;
    String padmitdate;

    InputStreamReader in = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(in);

public  void input() throws IOException{
        System.out.print("Enter Patient's Name:");
        pname = br.readLine();

        Random rand1 = new Random();
        pid = rand1.nextInt((9999 - 100) + 1) + 10;

        System.out.print("Enter Patient's Phone Number:");
        pphno = Long.parseLong(br.readLine());

        System.out.print("Enter Patient's Age:");
        page = Integer.parseInt(br.readLine());

        System.out.print("Enter Patient's Gender:");
        pgender = br.readLine();

        System.out.print("Enter Patient's Illness:");
        pillness = br.readLine();

        System.out.print("Enter Patient's Admit Date:");
        padmitdate = br.readLine();
    }

public  void delete() throws FileNotFoundException, IOException{
        Scanner in = new Scanner(System.in);
        File inputFile = new File("PatientDetails.txt");
        File tempFile = new File("myTemp2.txt");

        BufferedReader reader = new BufferedReader(new FileReader(inputFile));
        BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

        String currentLine;

        String lineToRemove;
        System.out.println("Enter the ID of the Patient you wish to delete: ");
        lineToRemove = in.next();

        while((currentLine = reader.readLine()) != null) {
            String trimmedLine = currentLine.trim();
            if(trimmedLine.startsWith(lineToRemove)) continue;
            writer.write((currentLine) + System.getProperty("line.separator"));
        }
        writer.close();
        reader.close();
        Files.move(tempFile.toPath(), inputFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
    }

public  void search() throws IOException{
        Scanner scan=new Scanner(System.in);
        System.out.println("Enter the ID of the Patient To Search:");
        String did=scan.next();
        String line="";
        try{
            FileInputStream fin = new FileInputStream("PatientDetails.txt");
            Scanner sc = new Scanner(fin);
            while(sc.hasNextLine()){
                line=sc.nextLine();
                if(line.startsWith(did))
                    System.out.println(line);
            }
            sc.close();
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }

public  void display(){
        try{
            BufferedReader br=new BufferedReader(new FileReader("PatientDetails.txt"));
            String s="";
            while((s=br.readLine())!=null){
                String data[]=new String[7];
                data=s.split(" ");
                for(int i=0;i<7;i++){
                    System.out.print(data[i]+"\t");
                }
                System.out.println();
            }
            br.close();
        }
        catch(Exception e){
        }
    }
};

共有1个答案

葛兴发
2023-03-14

简单的解决方案是在appointment类中使用composition创建doc和patient对象,并从用户处获取doc和patient id,或者使用objects创建一个对象并将其写入第三个文件。

如果是Id,你需要从文件中搜索信息。在新信息的情况下,您将创建医生和患者的对象。首先在文件中为它们设置权限,然后根据需要将数据存储在第三个文件中。

如果它能回答你的问题,请告诉我。

 类似资料:
  • 我正在创建一个医院管理系统,其中我有两个类,即AddDoctor和AddPatient,这两个类从用户那里获取有关其详细信息的输入,并将其存储到各自的文件中。现在,我想创建一个预约类,在这个类中,我可以将具有特定ID的患者分配给从文件中读取的具有特定ID的医生。如果JAVA支持多重继承,这是非常容易的,但因为它不支持多重继承,我一直在思考如何完成这项任务。请帮帮我。下面是我的AddDoctor课程

  • 我有两个XML文件,需要仅使用XSLT 1.0将其合并为第三个XML文件 文件一: 文件二: 最终结果应该输出到第三个名为output的文件中。xml,应该如下所示 我正在使用记事本将XSLT应用于第二个文件,我尝试使用 添加了编辑XSLT

  • 我有两个文件-file1.txt和file2.txt。我想用powershell比较这两个文件,并生成第三个文件(file3.txt),它包含从file1.txt开始的所有行减去file2.txt中的行 你能想出什么办法来做这件事吗?

  • 我正在尝试转换一个大的文本文件(大小为5 gig),但得到了一个从这篇文章中,我设法将文本文件的编码格式转换为可读的格式: 这里的问题是,当我试图转换一个大尺寸(5 GB)的文本文件时。我会得到这个错误 我知道它无法读取这么大的文件。我从几个链接中发现,我可以逐行阅读。 那么,我如何应用于我必须让它逐行读取的代码呢?我对逐行读取的理解是,我需要从中读取一行,并将其添加到中,直到行尾,对吗?

  • 档案员。txt包含两类员工的详细信息,即月薪和小时津贴。如果是月薪员工,则该文件包含名字、姓氏、性别、职级、类型和基本工资,如果是小时薪员工,则包含小时工资和工作小时数。该文件的示例如下所示: 约翰·史密斯M经理每月45000.00 Sunil Bates M高级每小时700.00 45 梁爱娃F警官每月30500.00 我要写一个程序,会看每个员工,计算奖金占基本工资的百分比,对于一个按小时计酬