0

这是一个家庭作业,我必须编写一个程序来读取“input.txt”文件并查看打印称呼的次数(例如:Donald Duck, 4 所以它打印 Hello Donald Duck 四次) m 是将称呼输出到一个名为“output.txt”的文本文件中,由于某种原因,它只打印出 input.txt 中最后一行的称呼,而不是全部。这是我到目前为止所拥有的:

   import java.util.*;
   import java.io.*;

   public class Driver0 {

      public static void main (String [] args) {
         Scanner userInput = new Scanner(System.in);
         System.out.println("Would you like the output to be written to a file?");
         Scanner read;
         String input = userInput.nextLine(); //user input
         if(input.equals("yes")) {
            System.out.println("Writing to \"output.txt\" now..");
         }
         else {
            System.out.println("Printing to screen:"); 
         }        
            try {
            read = new Scanner(new File("input.txt")); //scanner reading file
            } 
            catch (FileNotFoundException e){
               System.out.println("File not found.");
               return;
            }
         while(read.hasNextLine()) {
            String newInput = read.nextLine();
            String [] inputArray = newInput.split(","); //spliting
            int num = 0;
            num = Integer.parseInt(inputArray[2].trim());
                for(int i = 0; i < num; i++) {
                if(input.equals("yes")) {  //writing to file 
                    Driver0.outputting(inputArray);
                  }
               else { //writing to screen
                  System.out.println("Hello" + inputArray[1] + inputArray[0]); 
               }
            } 
         }
      }
      public static void outputting (String [] inputArray) {
         PrintWriter output = null;
         String fileName = "output.txt";
            try {
            output = new PrintWriter(new FileWriter(fileName)); 
            } 
            catch(IOException error){
               System.out.println("Sorry, can't open for writing.");
               return;
            }
            output.println("Hello" + inputArray[1] + inputArray[0]);
                output.close();       
      } 
   }

我的问题很可能在输出方法中,或者它有一条注释说写到它旁边的文件

4

3 回答 3

4

每次通过while循环时,您都会创建一个新的PrintWriter,向其中写入一行,然后关闭它。这只是每次都覆盖文件。您可能应该只创建一个PrintWriter您保持打开并附加到它的内容。

于 2012-09-06T00:07:25.237 回答
2

该类FileWriter第二个构造函数,它接受一个boolean参数来告诉它追加到现有文件的末尾,而不是从头开始覆盖它。

output = new PrintWriter(new FileWriter(fileName, true));
于 2012-09-06T00:09:37.483 回答
1

这是因为您每次都在使用新的 PrintWriter 创建一个新文件。

对您的解决方案的最小更改,快速修复将是(尽管您的代码中还存在许多设计问题):

import java.util.*;
import java.io.*;

public class Driver0 {

  public static void main (String [] args) {
     Scanner userInput = new Scanner(System.in);
     System.out.println("Would you like the output to be written to a file?");
     Scanner read;
     String input = userInput.nextLine(); //user input
     if(input.equals("yes")) {
        System.out.println("Writing to \"output.txt\" now..");
     }
     else {
        System.out.println("Printing to screen:"); 
     }        
        try {
        read = new Scanner(new File("input.txt")); //scanner reading file
        } 
        catch (FileNotFoundException e){
           System.out.println("File not found.");
           return;
        }
        ////////////////////////
        PrintWriter output;
        String fileName = "output.txt";
        try {
        output = new PrintWriter(new FileWriter(fileName)); 
        } 
        catch(IOException error){
           System.out.println("Sorry, can't open for writing.");
           return;
        }
        ////////////////////////
     while(read.hasNextLine()) {
        String newInput = read.nextLine();
        String [] inputArray = newInput.split(","); //spliting
        int num = 0;
        num = Integer.parseInt(inputArray[2].trim());
            for(int i = 0; i < num; i++) {
            if(input.equals("yes")) {  //writing to file 
                Driver0.outputting(output,inputArray);
              }
           else { //writing to screen
              System.out.println("Hello" + inputArray[1] + inputArray[0]); 
           }
        } 
     }
     ///////////////////////////////
     output.close();       
     ///////////////////////////////
  }
  public static void outputting (PrintWriter output,String [] inputArray) {
        output.println("Hello" + inputArray[1] + inputArray[0]);
  } 
}
于 2012-09-06T00:08:51.260 回答