Write a program that reads a stream of integers from a file and writes only the positive numbers to a second file. The user should be prompted to enter the names of both the input file and output file in main(), and then main() should attempt to open both files (providing an error if there is an error during this process). The main() method should then call the process() method to read all the integers from the input file and write only the positive integers to the output file. The process() method takes as arguments a Scanner to read from the input and a PrintWriter to write to the output. You can assume that if you are able to successfully open the input file, then there will only be integers in it.

Respuesta :

Answer:

Check the explanation

Explanation:

Code to create the program in the question above:

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.OutputStream;

import java.io.PrintWriter;

import java.util.Scanner;

public class ReadWriteInt

{

    public static void main(String args[]) throws IOException

    {

         Scanner input=new Scanner(System.in);

         System.out.println("Enter the name of the input file:");

         String inputFile=input.next();

         System.out.println("Enter the name of the output file:");

         String outputFile=input.next();

         File fin=new File(inputFile);

         File fout=new File(outputFile);

         //print error, if input file does not exist

         if(!fin.exists())

         {

             System.out.println("Error:Input file does not exist");

             System.exit(0);

         }

         //print error, if output file does not exist

         if(!fout.exists())

         {            

             System.out.println("Error:output file does not exist");

             System.exit(0);

         }

         //input stream to read

         Scanner read=new Scanner(fin);

         OutputStream os=new FileOutputStream(fout);

         //output stream to write

         PrintWriter write=new PrintWriter(os,true);

         //read from input file until there are no numbers

         while(read.hasNextLine())

         {

             String num=read.nextLine();

             //convert the string into number

             int number=Integer.parseInt(num);

             //if the number is positive , write it to out put file

             if(number>=0)

             {

                  write.println(number);

                  System.out.println(number);

             }

         }

         System.out.println("Positive numbers are copied successfully");

    }

}

Following are the program to the given question:

Program Explanation:

  • Importing the package.
  • Defined a class "Main".
  • Defining a method "process" that takes two parameters that is "o, c".
  • Inside the method, a string variable "l" is declared that use a while loop that inputs file value, and define the if block that check positive value and add into another file.
  • In the next step, the main method is declared, that defines two string variables "I_name,O_name".
  • After declaring the variable scanner class object is declared that inputs the file name.
  • It also define try and catch block, in the try block it takes file value and call the method process, and in the catch block it print the message.  

Program:

import java.io.*;//import package

import java.util.*;//import package

public class Main //defining Main class  

{

public static void process(Scanner o, PrintWriter c)//defining a process method that takes file values  

{

String l;//defining a String variable

while(o.hasNextLine())//defining while loop that reads file value  

{

l = o.nextLine();//using String variable that holds file value  

if(Integer.parseInt(l) > 0)//defining if block that checks value is positive value

{

c.println(l);//add value in output file

}

}

c.flush();//calling flush method

c.close();//calling close method

}

public static void main(String[] arx)//main method  

{

String I_name,O_name;    //defining a String variable

Scanner in = new Scanner(System.in);//creating the Scanner class Object to input value

System.out.print("Enter the input file name: ");//print message

I_name = in.next();//input file name

System.out.print("Enter the output file name: ");//print message

O_name= in.next();//input file name

try //defining try block

{

Scanner f_in = new Scanner(new File(I_name));//creating Scanner class Object that reads file value

PrintWriter f_out = new PrintWriter(new File(O_name));//creating PrintWriter class Object that prints file value

process(f_in, f_out);//calling process method that takes two parameters

}

catch (Exception ex)//defining catch block

{

System.out.println("File not found!");//print message

}

}

}

Output:

Please find the attached file.

Learn more:

brainly.com/question/15735985

Ver imagen codiepienagoya
Ver imagen codiepienagoya
Ver imagen codiepienagoya