
There will be 5 numbers (each in a new line) written in a text file. Text file name will be ?input2.txt?. Your Java code should read that text file, add all the numbers and print the output in a new file named ?output2.txt?.

import java.io.*;
class ReadNumbersFromFile
{
public static void main(String[] args)
{
try{
BufferedReader br=new BufferedReader(new FileReader("input.txt"));
String str="";
int sum=0;
while((str=br.readLine())!=null){
int num=Integer.parseInt(str);
sum+=num;
}
br.close();
BufferedWriter bw=new BufferedWriter(new FileWriter("output.txt"));
bw.write(Integer.toString(sum));
bw.close();
System.out.println("Data has been written in the file.");
}
catch(Exception E){}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.