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
Which of the following is a precaution that helps to protect you from electric shock when working with electric power tools?
olga2289 [7]
A A is the best answer to this question.
5 0
3 years ago
Read 2 more answers
Wich type of operating system is usually used in personal computers
Anvisha [2.4K]
The three most common operating systems for personal computers are Microsoft Windows, Apple Mac OS X, and Linux.
4 0
3 years ago
Read 2 more answers
What can multivariable calculus show for a data set.
dalvyx [7]

Answer:

They can help to explain the relationship between the output and input variables.

Hope this Helps!

5 0
2 years ago
Eric is working on a computer that has a device driver error. Eric can find the name of the device driver however the actual dev
wel

Answer:

Windows\System32\drivers folder.

Explanation:

Eric operates on such a computer which has some failure on system's driver. Although the specific system may not be operational, he will locate the system driver title. Inappropriately he has no Connectivity to the internet.

Windows\System32\drivers directory could move to him to show the file of the system driver. Thus, the following answer is correct according to the given scenario.

3 0
3 years ago
Ergonomia este știința care studiază ....
maria [59]

Answer:

Ergonomia (sau factorii umani) este disciplina științifică preocupată de înțelegerea interacțiunilor dintre om și alte elemente ale unui sistem și profesia care aplică teorie, principii, date și metode pentru a proiecta pentru a optimiza bunăstarea umană și în general performanta sistemului."

Explanation:

4 0
3 years ago
Other questions:
  • Carlos own a hardware store.He currently is not using any software to track what he has in the store. .In one to two sentences,
    10·1 answer
  • What are three softwares for cloud computing
    8·1 answer
  • Practice problems on functions. Write C function(s) to carry out the specified tasks. For each problem, also write the suggested
    15·1 answer
  • How does cryptocurrency exchange software works?
    14·1 answer
  • It is used to select specific menu options, drag and drop options and to draw something on screen.
    6·1 answer
  • What is GIGO ?<br>plz answer me​
    7·1 answer
  • When you login to your blogging account. The first screen with all controls, tools and functions is called .... Select one: a. D
    10·1 answer
  • What should be entered to make the loop print
    6·1 answer
  • If you want to remove the autocorrect options button from the screen, you can press the ____ key.
    15·1 answer
  • A 4"x6" photo is digitized using 10,000 pixels. An 11"x7" photo is digitized using 30,000 pixels. Which image will have the bett
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!