Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I posted the same question earlier but I didn\'t fully explain what I needed to

ID: 3550315 • Letter: I

Question

    I posted the same question earlier but I didn't fully explain what I needed to do very well. So heres my second attempt.


Write a program in Java that reads a file of numbers of type int and outputs all the numbers to another file, but without there being any duplicate numbers. Assume that the input file is sorted from the smallest numbers first to largest numbers last. After the program is run, the new file will contain all the numbers in the original file, but no number will appear more than once in the file. The numbers in the output file should also be listed from smallest to largest. Your program should obtain both file names from the user. This should be done with a text file and a binary file. For the text version of the file, the file should be a text file with one number per line. For the binary version, the file should be a binary file that has numbers of type int that were written using writeInt. Use the Scanner class to let the user enter the names of the files. Last but not least, the code should catch and handle exceptions when necessary.


     Also, I have no idea what a binary file is, or even how to create one, so it would be appreciated if you could show me an example based on these instructions. Since this is asking for a text file and a binary file, it wouldn't hurt if you could show me a text file as well.



This is the code I have thus far for the main program, so hopefully the revisions are minimal. I have code for the binary and text versions but am positive that the code for my binary file is far from correct.




import java.io.*;

class FileCopier {

public static void main(String args[])

throws IOException

{

//Declare variables

int i;

FileInputStream fin;

FileOutputStream fout;

Scanner s = new Scanner(System.in);

String oldfile = s.nextLine;

new FileReader(oldFile);


  {


try {

     // Open the input file

      try {

           fin = new FileInputStream(args[0]);

           }

      catch(FileNotFoundException e)

      {

       System.out.println("Input File Not Found");

       return;

      }

// Open the output file

      try {

           fout = new FileOutputStream(args[1]);

           }

      catch(FileNotFoundException e)

           {

           System.out.println("Error Opening Output File");

            return;

            }

            }

      catch(ArrayIndexOutOfBoundsException e)

      {

       System.out.println("Usage: CopyFile From To");

       return;

       }

// Copy the file

try {

do {

i = fin.read();

if(i != -1) fout.write(i);

} while(i != -1);

} catch(IOException e) {

System.out.println("File Error");

}

fin.close();

fout.close();

}

}



Explanation / Answer

For use of Scanner Class Firstly Please import java.util.* package in your program .