Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Use Java 8 FileInputStream: sum Use FileInputStream to complete method sumAll that takes a path to a file that contains a sequence of numbers from
Use Java 8
FileInputStream: sum Use FileInputStream to complete method sumAll that takes a path to a file that contains a sequence of numbers from 0 to 9 on a single line without any whitespace and returns the sum of those numbers. import java.io.*; public class FileInputStreamExercise { public static int sumall(String path) throws IOException int sum = 0; FileInputStream in = null; try { in = new FileInputStream(path); } finally { if (in !- null) { in.close(); return sum; 19 } History Submit FileInputStream: sum by line Use FileInputStream to complete method sumLineByLine that takes a path to a file that contains one number per line and returns the sum of those numbers. For example, if a file contains "123 7 ", sumLineByLine should return 130. import java.io.*; public class FileInputStreamExercise { public static int sumLineByLine(String path) throws IOException { int sum = 0; FileInputStream in = null; try { in = new FileInputStream(path); } finally { if (in !- null) { in.close(); return sum; 19 History Submit BufferedReader: sum by line Use BufferedReader to complete method sumLineByLine that takes a path to a file that contains one number per line and returns the sum of those numbers. For example, if a file contains "123 7 ", sumLineByLine should return 130. Jimport java.io.*; public class BufferedReaderExercise { public static int sumLineByLine(String path) throws IOException { int sum = 0; BufferedReader in - null; try { in = new BufferedReader(new FileReader(path)); } finally { if (in != null) { in.close(); return sum; FileOutputStream Complete method sunLineByLine. It is the same as the previous method except it stores its result in a file instead of returning its result. import java.io.*; public class FileOutputStreamExercise ( public static void sumLineByLine(String path) throws IOException { int sum = 0; BufferedReader in = null; FileOutputStream out = null; try ( in = new BufferedReader (new FileReader(path)); out = new FileOutputStream("result.txt"); } finally { if (in != null) { in.close(); if (out != null) { out.close()Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started