我无法使用 Java 制作多个文件。我希望它制作 n 个相同的文件,这些文件将放置在定义的目录中。出于某种原因,现在它会创建一个文件,然后继续使用第一个文件的名称创建新文件,这基本上会刷新文件。我认为它可能会发生,因为它没有更新我的全局名称变量。到目前为止,这是我的代码:
import java.io.*;
public class Filemaker{
//defining our global variables here
static String dir = "/Users/name/Desktop/foldername/"; //the directory we will place the file in
static String ext = ".txt"; //defining our extension type here
static int i = 1; //our name variable (we start with 1 so our first file name wont be '0')
static String s1 = "" + i; //converting our name variable type from an integer to a string
static String finName = dir + s1 + ext; //making our full filename
static String content = "Hello World";
public static void create(){ //Actually creates the files
try {
BufferedWriter out = new BufferedWriter(new FileWriter(finName)); //tell it what to call the file
out.write(content); //our file's content
out.close();
System.out.println("File Made."); //just to reassure us that what we wanted, happened
} catch (IOException e) {
System.out.println("Didn't work");
}
}
public static void main(String[] args){
int x = 0;
while(x <= 10){ //this will make 11 files in numerical order
i++;
Filemaker.create();
x++;
}
}
}
看看你是否能在我的代码中找到任何会导致这种情况发生的错误。