
Construct a Java program that will read a list of integers from a file named number.txt. The program will sum all the integers and keep the total in another file named total.txt. Assume that each integer in the input file is in individual row. Use classes and methods from the java.io package for all input and output.

Java File
import java.io.*;
public class ReadFile{
public static void main(String[] args) throws Exception {
int sum=0;
File f = new File("C:/numbers.txt");
FileReader fr = new FileReader(f);
BufferedReader reader = new BufferedReader(fr);
String st = "";
while((st = reader.readLine()) != null) {
int num=Integer.parseInt(st);
sum+=num;
}
File out=new File("C:/total.txt");
BufferedWriter bw=new BufferedWriter(new FileWriter(out));
bw.write(Integer.toString(sum));
bw.close();
System.out.println("File is created.");
}
}
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.