如何在不覆盖以前数据的情况下创建多个java txt文件?

时间:2012-06-15 18:35:20

标签: java

  

可能重复:
  Writing to a file without overwriting or appending

您好我正在创建一个执行数据并打印到txt文件的程序。我遇到的问题是每次运行程序都会覆盖我以前的txt文件。我不希望它覆盖也不想要追加数据。我想创建一个新的txt文件,每次生成它创建的日期或时间。有人可以帮我吗?

这是我的代码:

private static PrintWriter outFile;
    /**
     * @param args
     */
    //Main Method
     public static void main(String[] args) throws IOException
        {



         //creates the new file to be saved
        outFile = new PrintWriter(new FileWriter("trilogy.txt"));
        //Create a generator object to create random numbers
         Random gen = new Random ();

        //Create a scanner object to scan user input.
         Scanner scan = new Scanner(System.in);

         DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
         //get current date time with Date()
         Date date = new Date();
         outFile.println(dateFormat.format(date));
         outFile.println();

        //Prompts the user to input how many lock combinations he/she wants generated.
         System.out.print ("Enter quantity: "); 
         int quantity = scan.nextInt();


        int count = 1;
        //Loop - Only numbers 1 - 5 are used
        //       Only five numbers are generated per one lock combination 
         for (int i = 0; i < quantity; i++)
         {

                int n1 = gen.nextInt(5)+1;
                int n2 = gen.nextInt(5)+1;
                int n3 = gen.nextInt(5)+1;
                int n4 = gen.nextInt(5)+1;
                int n5 = gen.nextInt(5)+1;


    //While loop making sure that NO numbers are repeated
        while (n2==n1)
         {
            n2 = gen.nextInt(5)+1;
         }

               while (n3==n2 || n3 == n1 || n3==n4||n3==n5)
               {
                   n3 = gen.nextInt(5)+1;
               }

                    while (n4 == n3 || n4 == n2 || n4 == n1||n4==n5)
                    {
                        n4 = gen.nextInt(5)+1;
                    }

                        while (n5== n1 || n5==n2 || n5==n3 || n5==n4)
                        {
                            n5 = gen.nextInt(5)+1;
                        }

            //If statements that output the random lock combinations in different formats.
                        if (n1==1){
                            outFile.println ("(" + count +") "  +   (n1*10 +n2) +"-"+ (n3*10+n4)+"-"+n5);}
                        else if (n2==2){
                            outFile.println ("(" + count +") "  +   n2 + "-" + (n1*10 + n3)+ "-" + (n4*10+ n5));}
                        else if (n3==3){
                            outFile.println ("(" + count +") "  +   (n3*10 +n2) +"-"+ n1+ "-" +(n4*10+n5));}
                        else if (n4 == 4){
                            outFile.println ("(" + count +") "  +   (n4 +"-"+ (n2*100 +n3*10+n1)+"-"+n5));}
                        else
                            outFile.println ("(" + count +") "  +   (n5) +"-"+ (n2) +"-"+ (n3) + "-"+ (n4) +"-" +(n1));

                        count++;

            //Spaces one line in between each lock combination
            outFile.println();

         }
         outFile.close();
        }

}

2 个答案:

答案 0 :(得分:1)

尝试更改此内容:

        outFile = new PrintWriter(new FileWriter("trilogy.txt"));

为此附加文字:

        outFile = new PrintWriter(new FileWriter("trilogy.txt",true));

:)

答案 1 :(得分:0)

您需要更改以下行:

 outFile = new PrintWriter(new FileWriter("trilogy.txt"));

到有时间/日期的东西:

 outFile = new PrintWriter(new FileWriter("trilogy" + DATE_TIME + ".txt"));