I want it in Java Please Concatenate two files input1txt a

I want it in ( Java ) Please

Concatenate two files \"input1.txt\" and \"input2.txt\". That is make a third file named \"output.txt\" that has first input1 and then input2 in it. Delete the output file if it already exists.

Solution

FileCreateTest.java

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


public class FileCreateTest {

   public static void main(String[] args) throws FileNotFoundException {
       File file1 = new File(\"input1.txt\");
       File file2 = new File(\"input2.txt\");
       File outputFile = new File(\"output.txt\");
       if(outputFile.exists()){
           System.out.println(\"output.txt file already existed. Deleted\");
           outputFile.delete();
       }
       PrintStream ps = new PrintStream(outputFile);
       if(file1.exists() && file2.exists()){
           Scanner scan1 = new Scanner(file1);
           Scanner scan2 = new Scanner(file2);
           while(scan1.hasNext()){
               ps.println(scan1.nextLine());
           }
           while(scan2.hasNext()){
               ps.println(scan2.nextLine());
           }
           ps.flush();
           ps.close();
           System.out.println(\"output.txt has been generated with concatenate two files input1.txt and input2.txt\");
       }
       else{
           System.out.println(\"Input files do not exist\");
       }
   }

}

Output:

output.txt file already existed. Deleted
output.txt has been generated with concatenate two files input1.txt and input2.txt

I want it in ( Java ) Please Concatenate two files \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site