1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Alenkinab [10]
4 years ago
5

Write a program that reads a stream of integers from a file and writes only the positive numbers to a second file. The user shou

ld 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.

Computers and Technology
1 answer:
borishaifa [10]4 years ago
5 0

Answer:

See explaination for thr program code.

Explanation:

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");

}

}

Please check attachment for screenshot and output

You might be interested in
Match the metric units with these measurements
zavuch27 [327]
Force:newtons
distance:meters
work:joules
power:either watts or horsepower
energy:joules
time:kiloseconds and milliseconds
6 0
3 years ago
Being nice take the points​
MArishka [77]

Answer:

Sure!!! and thank you very much

4 0
3 years ago
Social networking websites, such as Facebook, Twitter, and Instagram, are best suited for which of the following types of resear
Nostrana [21]
Your answer would be A. getting leads on current topics, developing trends, and subject matter experts.
5 0
3 years ago
Read 2 more answers
Continuing with the program in problem #4: Box #10 has identical attributes to Box #07. Create box #10. Then, write two statemen
ahrayia [7]

The program illustrates the use of conditional statements i.e. the if keyword

<h3>The two statements</h3>

The statements that compare the boxes written in Python are as follows:

#Statement 1

if box07 > box10:

  print("Box 07")

else

  print("Box 10")

#Statement 2

if box21 > box23:

  print("Box 21")

else

  print("Box 23")

Read more about python programs at:

brainly.com/question/13246781

#SPJ1

6 0
2 years ago
Why should cloud computing not be a set of silos? Provide real or made up examples of why incompatible solutions will fail in IT
lesya [120]
2 is tha answer to Why should cloud computing not be a set of silos? Provide real or made up examples of why incompatible solutions will fail in IT.?
8 0
3 years ago
Other questions:
  • To type the letter address, _________ space from the dateline
    9·2 answers
  • What is the highest payed country
    9·1 answer
  • Using data from a tal distribution database, define a view named toplevelcust. it consists of the number, name, address, balance
    14·2 answers
  • 10^4+10-2=<br>10^4+10-2=
    12·2 answers
  • Users report that the network access is slow. After questioning the employees, the network administrator learned that one employ
    10·1 answer
  • The D:\ drive in your computer has been formatted with NTFS. The Mary user account has been assigned the following permissions:
    10·1 answer
  • Why do we use compliment method?
    13·2 answers
  • Which feature is used to help identify the appropriate content for particular form fields?
    9·1 answer
  • Jolly 4 pls link in description
    7·1 answer
  • status is a non-volatile memory chip used for store translation data between personal computer PC and digital device ​
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!