Skip to content
Advertisement

Using both FileInputStream and FileOutputStream

I am stuck on my problem where I have to read a file given by the user and then calculate names, grades, and such. But the issue I am having is that I need to take that data and store it in a text file names report.txt and then take the data from the first file and then compute the averages of all the scores. I am having trouble outputting anything, but when I do System.out it prints out correctly but I don’t think I have correctly stored it in the report.txt file. Any help would be much appreciated. Code:

import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.IOException;

public class LabProgram {
public static void main(String[] args) throws IOException {
  Scanner scnr = new Scanner(System.in);
  
  /* TODO: Declare any necessary variables here. */
  FileInputStream fileByteStream = null; // File input stream
  Scanner inFS = null;                   // Scanner object
  FileOutputStream fileStream = null;
  PrintWriter outFS = null;
  String userInput = scnr.nextLine(); 
  String firstName; 
  String lastName; 
  int midTerm1Score; 
  int midTerm2Score;
  int finalScore; 
  int avgScore; 
  char letterGrade; 
  int midTerm1Total = 0; 
  int midTerm2Total = 0; 
  int finalTotal = 0; 
  double midTerm1Avg; 
  double midTerm2Avg; 
  double finalAvg; 
  int counter = 0; 
  
  
  
  /* TODO: Read a file name from the user and read the tsv file here. */
  // Try to open file
  fileByteStream = new FileInputStream(userInput); 
  inFS = new Scanner(fileByteStream); 
  
  
  
  /* TODO: Compute student grades and exam averages, then output results to a text file here. */
  fileStream = new FileOutputStream("report.txt"); 
  outFS = new PrintWriter(fileStream); 
  
  while (inFS.hasNext()) { 
     firstName = inFS.next(); 
     lastName = inFS.next(); 
     midTerm1Score = inFS.nextInt(); 
     midTerm1Total = midTerm1Total + midTerm1Score; 
     midTerm2Score = inFS.nextInt();  
     midTerm2Total = midTerm2Total + midTerm2Score; 
     finalScore = inFS.nextInt(); 
     finalTotal = finalTotal + finalScore; 
     avgScore = (midTerm1Score + midTerm2Score + finalScore) / 3; 
     if (avgScore >= 90) { 
        letterGrade = 'A'; 
     } 
     else if ( avgScore >= 80) { 
        letterGrade = 'B'; 
     } 
     else if (avgScore >= 70) { 
        letterGrade = 'C'; 
     } 
     else if (avgScore >= 60) { 
        letterGrade = 'D'; 
     } 
     else  { 
        letterGrade = 'F'; 
     } 
     outFS.println(firstName + "   " + lastName + "   " + midTerm1Score + "  " + midTerm2Score + "  " 
                          + finalScore + "  " + letterGrade); 
                          counter++; 
      
  } 
  midTerm1Avg = (double) midTerm1Total / counter; 
  midTerm2Avg = (double) midTerm2Total / counter; 
  finalAvg = (double) finalTotal / counter; 
  outFS.println(""); 
  
  outFS.printf("Averages: Midterm1 %.2f", midTerm1Avg); 
  outFS.printf(", Midterm2 %.2f", midTerm2Avg); 
  outFS.printf(", Final %.2f", finalAvg); 
  
  outFS.close();
  fileByteStream.close(); // close() may throw IOException if fails
     
  

} }

Advertisement

Answer

PrintWriter doesn’t directly write the data to disk. Instead, it stores the data in an internal buffer until flush() is called because writing a large chunk of data to disk at once is faster than repeatedly writing small portions of data. Call outFS.flush() before closing the PrintWriter.

BTW, it’s a good practice to put streams, writers etc. into try-with-resources blocks instead of closing them manually.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement