用Java制作多个文件

用Java制作多个文件,java,file,Java,File,我用Java生成多个文件时遇到问题。我希望它使n个相同的文件,将被放置在一个定义的目录。出于某种原因,现在它将生成一个文件,然后继续生成具有第一个文件名的新文件,这基本上会刷新文件。我认为这可能是因为它没有更新我的全局名称变量。以下是我目前的代码: import java.io.*; public class Filemaker{ //defining our global variables here static String dir = "/Users/name/De

我用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++;
        }
    }
}
import java.io.*;
公共类文件生成器{
//在这里定义全局变量
静态字符串dir=“/Users/name/Desktop/foldername/”;//我们将放置文件的目录
静态字符串ext=“.txt”//在此处定义扩展类型
static int i=1;//我们的name变量(我们以1开头,这样我们的第一个文件名就不会是“0”)
静态字符串s1=”“+i;//将名称变量类型从整数转换为字符串
静态字符串finName=dir+s1+ext;//生成完整文件名
静态字符串content=“Hello World”;
publicstaticvoidcreate(){//实际创建文件
试一试{
BufferedWriter out=new BufferedWriter(new FileWriter(finName));//告诉它如何调用文件
out.write(content);//我们文件的内容
out.close();
System.out.println(“文件制作”);//只是为了让我们放心,我们想要的已经发生了
}捕获(IOE异常){
System.out.println(“不起作用”);
}
}
公共静态void main(字符串[]args){
int x=0;

while(x在初始化时设置
finName
一次。相反,您应该在
create()
函数中更新它。例如:

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");
    }
}

首先,您在
s1
变量的静态定义中使用了
i
变量。这在我看来很奇怪,让我觉得您希望一次又一次地重新定义它

相反,在
create()
函数中重新定义s1变量,使其实际递增


此外,将来在对此类问题进行故障排除时,可以使用
System.out.println()
语句将输出打印到控制台以跟踪程序的执行情况。对于更高级的需要,请使用日志解决方案或IDE的调试器跟踪程序执行情况并插入断点。

字符串是不可变的,因此在第一次初始化后,
finName
永远不会更改


在创建方法中,您必须重新创建文件路径。

谢谢,这真的很有帮助!很抱歉我没有调试习惯,我是非web开发/编程新手。不用担心,我只是认为这可能是一个对您有用的提示。没有调试器,我没用;)