FileInputStream: sum Use FileInputStream to complete method sumAll that takes a path to a file that contains
Fantastic news! We've Found the answer you've been seeking!
Question:
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();
Posted Date: