Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a complete program to read a series of sets of values from the file \"valu

ID: 3855427 • Letter: W

Question

Write a complete program to read a series of sets of values from the file "values.txt". The sets of values consist of an ID number (such as 1234) and an amount (such as 150.75). As the program reads in each set of values, it will write them out to a file called "output.txt". It will also sum the amounts and print the total amount to the output file at the end of processing. There is no header value in the file: use the eof() method (also called the reading-to-input failure method) to determine the end of data. Be sure to declare, open, and close both files. There is no need to format the output. Example: input file: output file: 1234 150.75 2345 22.00 1456 86.10 2345 22.00 1456 86.10 258.85

Explanation / Answer

// file name is::- FileHandling.java

import java.io.*;
public class FileHandling {

   public static void main(String args[]) throws IOException {
       BufferedReader br = null;
       float count = 0;
       FileInputStream in = null;
       FileOutputStream out = null;
       Writer output = null;
       int i=0, c;
       String[] characters = new String[1024];

        try {
            String sCurrentLine;
            in = new FileInputStream("input.txt");
            out = new FileOutputStream("output.txt");
            br = new BufferedReader(new FileReader("input.txt"));
            while ((sCurrentLine = br.readLine()) != null) {
                String[] arr = sCurrentLine.split(" ");
                count = count + Float.parseFloat(arr[1]);
                characters[i] = arr[0];
                i++;
            }
            // copy values from input file to output file
             while ((c = in.read()) != -1) {
                out.write(c);
             }
             // Append output at end of the output file
             output = new BufferedWriter(new FileWriter("output.txt", true));
             output.append(" "+ count);
           
             System.out.println("Success");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
                if (in != null) in.close();
                if (out != null) out.close();
                if(output != null) output.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote