输出到Java中的文本文件

时间:2012-09-05 23:55:50

标签: java file

这是一个家庭作业,我必须写一个程序,读取一个“input.txt”文件,看看多少次打印称呼(例如:唐老鸭,4所以它打印你好唐老鸭四次) 我遇到的问题是将称呼输出到名为“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();       
      } 
   }

我的问题很可能是在输出方法中,或者它有一个评论说写在旁边的文件

3 个答案:

答案 0 :(得分:4)

每次通过while循环,你都会创建一个新的PrintWriter,给它写一行,然后关闭它。这只是每次覆盖文件。你可能只应该创建一个PrintWriter来保持打开,而追加代替它。

答案 1 :(得分:2)

FileWriter类有一个second constructor,它带有一个boolean参数,告诉它追加到现有文件的末尾,而不是从一开始就覆盖它。

output = new PrintWriter(new FileWriter(fileName, true));

答案 2 :(得分: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]);
  } 
}