Kronos
Kronos

Reputation: 21

Making Multiple Files in Java

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.

Upvotes: 2

Views: 125

Answers (3)

user973999
user973999

Reputation:

String are immutable so finName is never change after the first initialization.

In your create method you have to recreate your file path.

Upvotes: 0

jamesmortensen
jamesmortensen

Reputation: 34038

First, you are using your i variable in the static definition of your s1 variable. This looks odd to me and makes me think that you're expecting that to be redefined again and again.

Instead, redefine your s1 variable inside your create() function so that it actually increments.

Also, in the future, when troubleshooting problems like this, you can use System.out.println() statements to print output to the console to trace execution of your program. For more advanced needs, use a logging solution or your IDE's debugger to trace the program execution and insert breakpoints.

Upvotes: 1

Jon Newmuis
Jon Newmuis

Reputation: 26492

You set finName once, when it is initialized. Instead, you should update it in your create() function. For example:

public static void create(){  //Actually creates the files 
    String finName = dir + i + ext; 
    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");
    }
}

Upvotes: 2

Related Questions