【问题标题】:Increment a text file name in java在java中增加一个文本文件名
【发布时间】:2014-02-20 00:17:20
【问题描述】:

我正在尝试增加一个文本文件名,以便创建的所有文本文件都将具有唯一的名称并按升序排列。这是我到目前为止的代码。我希望你能理解我在这里尝试的逻辑。问题是程序被锁定或这段代码什么都不做。谢谢。

increase 是 0 的全局整数

    String name = String.valueOf(increase);
    File file = new File("E:\\" + name + ".txt");

    while(file.exists()){
         increase++;


    if(!file.exists()) {

        try {

            String content = textfile.toString();
            file.createNewFile();

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();

            System.out.println("Done");

            }catch (IOException e){
                e.printStackTrace();
                }
        }
    }

【问题讨论】:

  • 首先锁定如果文件存在,它将进入while,但不会进入if和无限循环,因为文件永远不会重新分配..其次,什么都不做,文件不会进入while,然后甚至永远不会到达 if 并结束
  • 嗨。对不起,但我不明白你的意思。能不能换个方式解释一下,或者把应该做的事情写成代码格式?谢谢你。
  • 用代码添加了答案
  • 你可以将文件名表示为"E:/" + name,Java会在Windows上将/翻译成`\`。

标签: java int increment


【解决方案1】:

用代码解释我的评论

String name = String.valueOf(increase);
File file = new File("E:\\" + name + ".txt");

while(file.exists()){
     increase++;
     // reassign file this while will terminate when #.txt doesnt exist
     name = String.valueOf(increase);
     file = new File("E:\\" + name + ".txt");
} // the while should end here
// then we check again that #.txt doesnt exist and try to create it
if(!file.exists()) {

try {

    String content = textfile.toString();
    file.createNewFile();

    FileWriter fw = new FileWriter(file.getAbsoluteFile());
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(content);
    bw.close();

    System.out.println("Done");

}catch (IOException e){
    e.printStackTrace();
}
// you had a extra close bracket here causing the issue
}
// this program will now create a new text file each time its run in ascending order

【讨论】:

  • 谢谢您。很高兴看到我的思维过程没有偏离目标一百万英里。这现在在我的程序中完美运行。我可以从 cmets 中看到文件文件也需要更改。
【解决方案2】:

当您更新您的 int 变量 increase 时,您不会更改您的 File file。这就是你以无限循环结束的原因。

【讨论】:

  • 另外name 不会自动更改。
【解决方案3】:

我为我的项目需求编写的下面的代码适用于所有/没有扩展。希望对您有所帮助!

public static File getValidFile(String filePath) {
    File file = new File(filePath);

    if (!file.exists()) {
        return file;
    } else {
        int extAt = filePath.lastIndexOf('.');
        String filePart = filePath;
        String ext = "";

        if(extAt > 0) { //If file is without extension
            filePart = filePath.substring(0, extAt);
            ext = filePath.substring(extAt);
        }

        if (filePart.endsWith(")")) {
            try {
                int countStarts = filePart.lastIndexOf('(');
                int countEnds = filePart.lastIndexOf(')');
                int count = Integer.valueOf(filePart.substring(countStarts + 1, countEnds));

                filePath = filePart.substring(0, countStarts + 1) + ++count + ")" + ext;
            } catch (NumberFormatException | StringIndexOutOfBoundsException ex) {
                filePath = filePart + "(1)" + ext;
            }
        } else {
            filePath = filePart + "(1)" + ext;
        }
        return getValidFile(filePath);
    }
}

【讨论】:

    猜你喜欢
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    • 2017-09-14
    相关资源
    最近更新 更多