java
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.
View Answers
March 7, 2011 at 3:02 PM
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.");
}
}
Ads
Related Tutorials/Questions & Answers:
java
java diff bt core
java and
java
Advertisements
java
java why iterator in
java if we for loop
java
java explain technologies are used in
java now days and structure
java
java
java different between
java & core
java
Java
Java Whether
Java is pure object oriented Language
JAVA
JAVA how the name came for
java language as "
JAVA
java
java what is
java reflection
java
java in
java does not pointers concept but what is nullpointers in
java?
nullpointer is a runtime Exception
java
what is the size of array in
java ? what is the size of array in
java ?
what is the mean of finalize in
java
Java
Java how to do
java in command prompt
java
java give a simple example for inheritance in
java
java
java why to set classpath in
java
java
java why to set classpath in
java
java
java why to set classpath in
java
java
java Write a
java code to print "ABABBABCABABBA
java
java write a program in
java to acess the email
java
java send me
java interview questions
java
java how use
java method
java
java is
java purely object oriented language
java
java why multiple inheritance is not possible in
java
java
java write a
java program using filenotfoundexception
java
java Does
java allows multiline comments
java
java give a simple example for inheritance in
java
java
java why to set classpath in
java
java
java what are
JAVA applications development tools
Java
Java Whether
Java is Programming Language or it is SOftware
java
java explain object oriented concept in
java
java
java difference between class and interface
Java
Java how to draw class diagrams in
java
java
java How to set
java Policy for applet using jdk 6