I'm having trouble making multiple files with Java. I want it to make n number of identical files that will be placed in a defined directory. For some reason, right now it will make 1 file and then keep making new files with the name of the first file made, which basically refreshes the file. I think it may happen because it is not updating my global name variable. Here's my code so far:
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++;
}
}
}
Just see if you can find any errors in my code that would be causing this to happen.