读取两个文本文件并将这两个文本文件中的特定行写入第三个文件
Read two text files and write specific lines from those two text files into a third file
我正在创建一个医院管理系统,其中有 2 个 classes,即 AddDoctor 和 AddPatient,它们从用户那里获取关于他们的详细信息的输入,并将它们存储到各自的文件中。我现在想创建一个约会 class,我可以在其中将具有特定 ID 的患者分配给具有从文件中读取的特定 ID 的医生。如果 Java 支持多重继承,这将非常容易,但由于它不支持,我陷入了如何完成这项任务的困境。
以下是我的 AddDoctor class
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 Class
class AddPatient extends People{
String pillness;
String pregisterdate;
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
void input() throws IOException{
System.out.print("Enter Patient's Name:");
name = br.readLine();
Random rand1 = new Random();
id = rand1.nextInt((9999 - 100) + 1) + 10;
System.out.print("Enter Patient's Phone Number:");
phno = Long.parseLong(br.readLine());
System.out.print("Enter Patient's Age:");
age = Integer.parseInt(br.readLine());
System.out.print("Enter Patient's Gender:");
gender = br.readLine();
System.out.print("Enter Patient's Illness:");
pillness = br.readLine();
System.out.print("Enter Patient's Registration Date:");
pregisterdate = br.readLine();
}
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);
}
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();
}
}
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){
}
}
};
class WriteP extends AddPatient {
void write() {
try(FileWriter fw = new FileWriter("PatientDetails.txt",true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw))
{
out.println(String.format("%-1s %-1s %-1s %-1s %-1s %-1s %-1s",id,name,phno,age,gender,pillness,pregisterdate));
}catch(IOException e){
e.printStackTrace();
}
}
};
简单的解决方案是在约会中使用组合创建医生和患者的对象class并从用户那里获取医生和患者 ID,或者使用对象创建一个并将其写入第三个文件。
如果是 Id,您需要从文件中搜索信息。
如果有新信息,您将创建医生和患者的对象。首先在他们的文件中将它们正确,然后根据需要将数据存储在第三个文件中。
如果它回答了你的问题,请告诉我。
创建约会 class 并使用接受要查找的 ID 的搜索方法(看起来与您已有的搜索方法相似)然后使用与您的 write 相似的代码写入新的文件.
我正在创建一个医院管理系统,其中有 2 个 classes,即 AddDoctor 和 AddPatient,它们从用户那里获取关于他们的详细信息的输入,并将它们存储到各自的文件中。我现在想创建一个约会 class,我可以在其中将具有特定 ID 的患者分配给具有从文件中读取的特定 ID 的医生。如果 Java 支持多重继承,这将非常容易,但由于它不支持,我陷入了如何完成这项任务的困境。
以下是我的 AddDoctor class
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 Class
class AddPatient extends People{
String pillness;
String pregisterdate;
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
void input() throws IOException{
System.out.print("Enter Patient's Name:");
name = br.readLine();
Random rand1 = new Random();
id = rand1.nextInt((9999 - 100) + 1) + 10;
System.out.print("Enter Patient's Phone Number:");
phno = Long.parseLong(br.readLine());
System.out.print("Enter Patient's Age:");
age = Integer.parseInt(br.readLine());
System.out.print("Enter Patient's Gender:");
gender = br.readLine();
System.out.print("Enter Patient's Illness:");
pillness = br.readLine();
System.out.print("Enter Patient's Registration Date:");
pregisterdate = br.readLine();
}
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);
}
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();
}
}
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){
}
}
};
class WriteP extends AddPatient {
void write() {
try(FileWriter fw = new FileWriter("PatientDetails.txt",true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw))
{
out.println(String.format("%-1s %-1s %-1s %-1s %-1s %-1s %-1s",id,name,phno,age,gender,pillness,pregisterdate));
}catch(IOException e){
e.printStackTrace();
}
}
};
简单的解决方案是在约会中使用组合创建医生和患者的对象class并从用户那里获取医生和患者 ID,或者使用对象创建一个并将其写入第三个文件。
如果是 Id,您需要从文件中搜索信息。 如果有新信息,您将创建医生和患者的对象。首先在他们的文件中将它们正确,然后根据需要将数据存储在第三个文件中。
如果它回答了你的问题,请告诉我。
创建约会 class 并使用接受要查找的 ID 的搜索方法(看起来与您已有的搜索方法相似)然后使用与您的 write 相似的代码写入新的文件.