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]
3 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]3 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
Regression testing should be a normal part of integration testing because as a new module is added to the system new
nikklg [1K]
It's E) both A and B A) Control logic is invoked B) Data flow paths are established
7 0
2 years ago
1. Which of the following are examples of applied art? Select all that apply. (2 points)
Svetlanka [38]

Answers

1. Examples of applied in the list art are Architecture, Drawing and industrial design.

2. The following can be considered an artist’s media; substrate onto which art is created (ex: canvas, paper) and material with which to create art (ex: paint, clay)

Explanations

Applied art is the application of both artistic and design principles to practice and practical use. This type of art can be fine art and theoretical in practice. Examples of applied art are drawing and painting illustrations, woodblock prints, Print based art, industrial and interior design, architecture, car and toy design, landscaping, tattoos and pottery. An art media is the material used by an artist or designer to create a piece of work.





6 0
3 years ago
Read 2 more answers
What helps to identify trends?
vagabundo [1.1K]
Pie graph but idk if thats the answer
4 0
3 years ago
Read 2 more answers
2.3.<br> Why is a foundation of particular importance when building and designing<br> structures?
bearhunter [10]

Answer:

A foundation plays three major roles in the construction of a structure. A good and strong foundation keeps the building standing while the forces of nature wreak havoc. Well-built foundations keep the occupants of the building safe during calamities such as earthquake, floods, strong winds etc.

6 0
2 years ago
Sam's manager would like him to create and distribute copies of a budget report for each department. The budget report should sh
almond37 [142]
<span>a spreadsheet and a printer</span>
7 0
3 years ago
Read 2 more answers
Other questions:
  • If i find an image and copy and paste it into Paint.
    5·1 answer
  • If you have long column labels with columns so wide that they affect the readability of a worksheet you should first
    6·2 answers
  • What are an administrator's choices for managing file permissions on a drive formatted as fat32?
    10·1 answer
  • Fuel-pressure regulators on fuel-return-type fuel-injection systems are installed
    8·2 answers
  • In a _____ network, each device on the network is attached to a central server. If the server fails, then the other devices will
    8·1 answer
  • (5 points) Create a user-defined data structure (i.e., struct) called Point that represents a two-dimensional Cartesian coordina
    5·1 answer
  • Beyond personal self-directed learning, engaging the management team of the technology group is a process of strategic learning
    12·2 answers
  • Take the average of some numbers. Show all the numbers that are below average. You can assume that there will not be more than 2
    10·1 answer
  • Suppose that the host with IP address 10.0.1.19 sends an IP datagram destined to host 128.119.160.183. The source port is 3324,
    7·1 answer
  • Write a function sumOfMultiples, that inputs two integers - seed and cap. The function should return the sum of all the multiple
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!