簡體   English   中英

使用 for 循環和 PrintWriter,如何創建多個隨機命名的文件,每個文件中都有一組自己的代碼?

[英]Using a for-loop and PrintWriter, how do I create multiple randomly named files each that have a set of their own code in them?

基本上問題是在編譯運行並輸入輸入之后。 僅創建一個文件。 一切正常,除了當我輸入一個大於一的數字時。 輸入大於 1 的數字應該允許我擁有多個文件,每個文件都根據我的理解生成隨機名稱,對吧? 相反,它選擇只創建一個具有隨機名稱的文件並正常執行所有操作,除了創建我告訴它的那幾個額外文件。

這是為了個人娛樂,我使用記事本++來創建它,然后我在命令提示符下使用 javac 和 java 編譯和運行它。 我基本上是在使用我們幾天前在 class 中學到的東西來創建這個,還有很多我必須研究的額外東西,但我真的很想知道如何做到這一點並且找不到太多關於這樣做的內容一個for循環,這就是我想要的方式。

import java.util.Scanner;
import java.io.PrintWriter;
import java.io.FileNotFoundException;

public class rangen {

    static String getnumlet(int n) {

        String numlet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr" +
            "stuvwxyz0123456789";
        StringBuilder sb = new StringBuilder(n);

        for (int i = 0 ; i < n ; i++) {
            int index = (int)(numlet.length() * Math.random());

            sb.append(numlet.charAt(index));
        }
        return sb.toString();
    }

    public static void main(String[] args) {

        final int length = 10;
        int amount = 0;

        String printername = (rangen.getnumlet(length) + ".java");
        String name = (printername.substring(0 ,
            printername.indexOf('.')).trim());
        Scanner input = new Scanner(System.in);
        PrintWriter tofile = null;

        System.out.print("Enter the amount of files you want to create: ");
        amount = input.nextInt();

        for (int i = 1 ; i <= amount ; i++) {

            try {

                tofile = new PrintWriter(printername);

                tofile.print("// Creator: Unknown...\n");
                tofile.print("// Date Created: 9-28-2019\n");
                tofile.print("// Last Modified: 9-28-2019\n");
                tofile.print("// Description: This file was \n" + 
                    "//     randomly generated.\n");
                tofile.print("public class " + name + " {\n");
                tofile.print("  public static void main(String[] args) {\n");
                tofile.print("      private static final double inf = \n" +
                    "           Double.POSITIVE_INFINITY;\n");
                tofile.print("      for (int i = 1 ; i <= inf ; i++) {\n");
                tofile.print("          System.out.print(\"yeet \" + i);\n");
                tofile.print("      }\n");
                tofile.print("  }\n");
                tofile.print("}\n");
                for (int x = 0 ; x <= 5; x++ ) {

                    tofile.print("// LOL " + x + "\n");
                }
            }
            catch (FileNotFoundException e) {
                System.out.println("couldn't create " + printername);
            }
            tofile.close();
        }
    }
}

假設您在控制台中輸入了 3 的值。 然后我希望它創建三個單獨的文件,每個文件中都有我告訴它寫入文件的代碼。 不會出現錯誤消息,但只為我制作了一個文件而不是 3 個。

您需要在for循環的每次迭代中生成一個新的隨機文件名。 如所寫,您的程序會生成一個隨機文件名,並在每次迭代時使用相同的名稱。

for (int i = 1 ; i <= amount ; i++) {
    String printername = (rangen.getnumlet(length) + ".java");
    String name = printername.substring(0, printername.indexOf('.')).trim();

    try {
        tofile = new PrintWriter(printername);
        ...
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM