Write a program for: In a text file INPUT.TXT integers separated by a space, perhaps in a few lines. In a single file view to create a list of these numbers and find the arithmetic mean of the list elements. The resulting value is recorded in a text file OUTPUT.TXT.
Code : AverageWriter.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class AverageWriter {
public static void main(String[] args) {
AverageWriter t = new
AverageWriter();
// Used to find average line by
line and write in different line of output file
//
t.findAverageByLineWriteTo("src/INPUT.TXT","src/OUTPUT.TXT");
// Used to find whole avarage and
write in output file
t.findAverageWriteTo("src/INPUT.TXT","src/OUTPUT.TXT");
}
public void findAverageWriteTo(String inputFile,
String outputFile) {
BufferedReader br = null;
try {
br = new
BufferedReader(new FileReader(inputFile));
StringBuilder sb = new
StringBuilder();
String line = br.readLine();
int sum = 0;
int totalNum = 0;
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
if(line != null) {
String[]
strArray = line.split(" ");
int[] nums = new
int[strArray.length];
for (int i=0;
i<strArray.length; i++) {
nums[i] =
Integer.parseInt(strArray[i]);
sum +=
nums[i];
}
totalNum +=
nums.length;
}
line = null;
line = br.readLine();
}
float average = sum/totalNum;
AverageWriter t = new
AverageWriter();
t.writeToFile(outputFile,
String.valueOf(average));
System.out.println(average);
String everything =
sb.toString();
System.out.println(everything);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch
(IOException e) {
e.printStackTrace();
}
}
}
public void findAverageByLineWriteTo(String
inputFile, String outputFile) {
BufferedReader br = null;
try {
br = new
BufferedReader(new FileReader(inputFile));
StringBuilder sb = new
StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
if(line != null) {
String[]
strArray = line.split(" ");
int[] nums = new
int[strArray.length];
int sum =
0;
for (int i=0;
i<strArray.length; i++) {
nums[i] =
Integer.parseInt(strArray[i]);
sum +=
nums[i];
}
float average =
sum/nums.length;
AverageWriter t
= new AverageWriter();
t.appendToFile(outputFile, String.valueOf(average));
System.out.println(average);
}
line = null;
line = br.readLine();
}
String everything =
sb.toString();
System.out.println(everything);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch
(IOException e) {
e.printStackTrace();
}
}
}
public void writeToFile(String fileName, String
content) {
BufferedWriter bw = null;
FileWriter fw = null;
try {
fw = new
FileWriter(fileName);
bw = new
BufferedWriter(fw);
bw.write(content);
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch
(IOException ex) {
ex.printStackTrace();
}
}
}
public void appendToFile(String fileName, String
content) {
BufferedWriter bw = null;
FileWriter fw = null;
try {
fw = new
FileWriter(fileName, true);
bw = new
BufferedWriter(fw);
bw.write(content);
bw.newLine();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch
(IOException ex) {
ex.printStackTrace();
}
}
}
}
Comment : Keep the INPUT.TXT in right path and execute the file in java.
Get Answers For Free
Most questions answered within 1 hours.