Write a JAVA program that reads a stream of integers from a

Write a JAVA 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. You have been supplied JUnit tests for the process()method, as well as the output for several example file contents. Please use files. Please do not use classes and objects.

Solution

Please follow the code and comments for description :

CODE :

import java.io.File; // required imports
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class ReadWrite { // class to run the code

public static void main(String[] args) { // driver method

Scanner sc = new Scanner(System.in); // scanner class to get the data

String inputfile; // required locla variables
String outputfile;
FileInputStream fin = null;
PrintWriter pOut = null;

System.out.println(\"Enter the file name of the input file : \"); // prompt for the user
inputfile = sc.nextLine(); // get the data

System.out.println(\"Enter the file name of the output file: \"); // prompt for the user
outputfile = sc.nextLine(); // get the data

try { // try catch block of code
File inFile = new File(inputfile + \".txt\"); // create a file object for the input file
fin = new FileInputStream(inFile); // get the stream of the data
try { // try catch block of code
File outFile = new File(outputfile + \".txt\"); // create a file object for the output file
pOut = new PrintWriter(outFile); // get the print writer ready
} catch (Exception e) { // catch the exception
System.out.println(\"Unable to open file \'\" + outputfile + \"\'\" + e); // print to console
}
process(fin, pOut); // call the method to process the data
fin.close(); // close the file
} catch (Exception ex) {
System.out.println(\"Unable to open file \'\" + inputfile + \"\'\" + ex);
}
}

public static void process(FileInputStream fin, PrintWriter pOut) throws IOException { // method that takes the parameters

int readData; // local varaibles
Scanner scanner = new Scanner(fin); // scanner class
while (scanner.hasNextInt()) { // check for the int data
readData = scanner.nextInt(); // get the data
if (readData > 0) { // check for the positive number
pOut.print(readData); // write the data
pOut.print(\"\ \"); // new line character
}
}
pOut.flush(); // flush out the data
}
}

OUTPUT :

Enter the file name of the input file :
input
Enter the file name of the output file:
out

input.txt :

1
0
-8
65
-9
35
2
-654
15
-85

out.txt :

1
65
35
2
15


Hope this is helpful.

Write a JAVA 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
Write a JAVA 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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site