如何根据用户输入创建输入和输出文件?

时间:2015-12-17 22:24:43

标签: java text-files

输入文件DATA1.txt将包含7行,每行包含一个正整数,用于描述每种类型的糖果数量。输出文件OUT1.txt将包含与输入的数字相同的星数。 即: 输入DATA1.txt将显示:

12
8
10
5
20
3
7

输出OUT1.txt将显示:

12: ************
8: ********
10: **********
5: *****
20: ********************
3: ***
7: *******

当我运行我的代码时,它只创建一个OUT1.txt文件,但它有数字而不是星号。我怎么会有DATA1.txt?我不太明白这一点,因为我还没有学习输入或输出文件。任何帮助将不胜感激。我会在代码中修改什么?

import javax.swing.*;
import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class CandyLand{
    public static void main(String str[]){
        int num1,num2,num3,num4,num5,num6,num7;

        String input1 = JOptionPane.showInputDialog("Please, enter the number of candies in the first pile");
        num1 = Integer.parseInt(input1);
        String input2 = JOptionPane.showInputDialog("Please, enter the number of candies in the second pile");
        num2 = Integer.parseInt(input2);
        String input3 = JOptionPane.showInputDialog("Please, enter the number of candies in the third pile");
        num3 = Integer.parseInt(input3);
        String input4 = JOptionPane.showInputDialog("Please, enter the number of candies in the fourth pile");
        num4 = Integer.parseInt(input4);
        String input5 = JOptionPane.showInputDialog("Please, enter the number of candies in the fifth pile");
        num5 = Integer.parseInt(input5);
        String input6 = JOptionPane.showInputDialog("Please, enter the number of candies in the sixth pile");
        num6 = Integer.parseInt(input6);
        String input7 = JOptionPane.showInputDialog("Please, enter the number of candies in the seventh pile");
        num7 = Integer.parseInt(input7);

        String fileName = JOptionPane.showInputDialog("Please, enter the name of the file");
        try{

            DataOutput f = new DataOutputStream(new FileOutputStream(fileName +".txt"));
            f.writeBytes(String.valueOf(num1));
            f.writeBytes("\r\n");
            f.writeBytes(String.valueOf(num2));
            f.writeBytes("\r\n");
            f.writeBytes(String.valueOf(num3));
            f.writeBytes("\r\n");
            f.writeBytes(String.valueOf(num4));
            f.writeBytes("\r\n");
            f.writeBytes(String.valueOf(num5));
            f.writeBytes("\r\n");
            f.writeBytes(String.valueOf(num6));
            f.writeBytes("\r\n");
            f.writeBytes(String.valueOf(num7));
        }
        catch(Exception e){
            String msg = e.toString();
            System.out.println(msg);
        }

        Scanner file = null;
        ArrayList<Integer> list = new ArrayList<Integer>();

        try {
            file = new Scanner(new File("OUT1.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        while(file.hasNext()){
            if (file.hasNextInt()) list.add(file.nextInt());
            else file.next();
        }   
        for (Integer j: list){
             System.out.print(j + ": ");
             for(int i=1; i<=j; i++){
                System.out.print("* ");
             }
             System.out.println("");
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

您是否预先创建了任何文件?如果我在没有预制的文件的情况下运行它,我输入七个数字和文件名“test”。该计划停在:

  

java.io.FileNotFoundException:OUT1.txt(系统找不到指定的文件)

我现在看到test.txt是用里面的7个数字创建的。这是在第一次运行时没有我之前手动创建的文件。没有创建OUT1.txt。

当文件OUT1.txt不存在时,如何运行while循环?

while(file.hasNext()){
    if (file.hasNextInt())
        list.add(file.nextInt());
    else file.next();
}

我正在努力修复我将在编辑时编辑的程序,但我希望您先了解。

代码:(我已对评论进行了评论)

import javax.swing.*;
import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class CandyLand{
   public static void main(String str[]){
      int num1,num2,num3,num4,num5,num6,num7;

      String input1 = JOptionPane.showInputDialog("Please, enter the number of candies in the first pile");
      num1 = Integer.parseInt(input1);
      String input2 = JOptionPane.showInputDialog("Please, enter the number of candies in the second pile");
      num2 = Integer.parseInt(input2);
      String input3 = JOptionPane.showInputDialog("Please, enter the number of candies in the third pile");
      num3 = Integer.parseInt(input3);
      String input4 = JOptionPane.showInputDialog("Please, enter the number of candies in the fourth pile");
      num4 = Integer.parseInt(input4);
      String input5 = JOptionPane.showInputDialog("Please, enter the number of candies in the fifth pile");
      num5 = Integer.parseInt(input5);
      String input6 = JOptionPane.showInputDialog("Please, enter the number of candies in the sixth pile");
      num6 = Integer.parseInt(input6);
      String input7 = JOptionPane.showInputDialog("Please, enter the number of candies in the seventh pile");
      num7 = Integer.parseInt(input7);

      String fileName = JOptionPane.showInputDialog("Please, enter the name of the file");
      try{
         DataOutput f = new DataOutputStream(new FileOutputStream(fileName + ".txt"));
         f.writeBytes(String.valueOf(num1));
         f.writeBytes("\r\n");
         f.writeBytes(String.valueOf(num2));
         f.writeBytes("\r\n");
         f.writeBytes(String.valueOf(num3));
         f.writeBytes("\r\n");
         f.writeBytes(String.valueOf(num4));
         f.writeBytes("\r\n");
         f.writeBytes(String.valueOf(num5));
         f.writeBytes("\r\n");
         f.writeBytes(String.valueOf(num6));
         f.writeBytes("\r\n");
         f.writeBytes(String.valueOf(num7));
      }
      catch(Exception e){
         String msg = e.toString();
         System.out.println(msg);
      }

      Scanner file = null;
      ArrayList<Integer> list = new ArrayList<Integer>();

      try {
        //You should be reading from the first file you created, not OUT1.txt
         file = new Scanner(new File(fileName + ".txt"));
      } 
      catch (FileNotFoundException e) {
         e.printStackTrace();
      }

      while(file.hasNext()){
         if (file.hasNextInt())
            list.add(file.nextInt());
         else file.next();
      }
      try {
        //Instead using System.out.println, use DataOutput like you were using before
         DataOutput f2 = new DataOutputStream(new FileOutputStream("OUT1.txt"));
         for (Integer j: list){
            f2.writeBytes(j + ": ");
            for(int i=1; i<=j; i++){
               f2.writeBytes("* ");
            }
            f2.writeBytes("\r\n");
         }
      }
      catch(Exception e){
         e.printStackTrace();
      }
   }
}