同时使用 FileInputStream 和 FileOutputStream

Using both FileInputStream and FileOutputStream

我遇到了一个问题,我必须读取用户提供的文件,然后计算姓名、成绩等。但我遇到的问题是我需要获取该数据并将其存储在文本文件名 report.txt 中,然后从第一个文件中获取数据,然后计算所有分数的平均值。我在输出任何内容时遇到问题,但是当我执行 System.out 时它打印正确但我认为我没有将它正确存储在 report.txt 文件中。任何帮助将非常感激。 代码:

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
     
  

} }

PrintWriter 不直接将数据写入磁盘。相反,它将数据存储在内部缓冲区中,直到 flush() 被调用,因为一次将大块数据写入磁盘比重复写入一小部分数据要快。在关闭 PrintWriter.

之前调用 outFS.flush()

顺便说一句,将流、编写器等放入 try-with-resources 块而不是手动关闭它们是一个很好的做法。