Java程序,int i (i=0)默认值被使用,尽管它每次循环增加1

标签 java

我正在创建一个 JAVA 程序来自动将某些文件夹复制到新位置,为此,我创建了一个带有循环的函数,为每个给定的文件夹源和目标使用相同的函数。问题是该函数只会多次将第一个文件夹复制到新位置,而不是复制一次然后复制下一个文件夹。文件夹位置保存在字符串数组中,并通过更改值 [i] 来选择特定位置。每次函数循环时,[i] 都会增加,但循环不会选择 [i] 值以及下一个要复制的文件夹。

有人可以帮助我吗,我正在使用的代码如下,谢谢。

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

public class Application {

static String[] saves = {
        "C:\\Users\\Lucas\\Documents\\My Games\\Halo",
        "C:\\Users\\Lucas\\Documents\\My Games\\Terraria",
        "C:\\Users\\Lucas\\Documents\\My Games\\Borderlands 2",
        "C:\\Users\\Lucas\\Documents\\My Games\\Rocket League"
};

private static int i = 1;

File source = new File(saves[i]);

static File folder = new File("Saves\\");

File dest = new File(String.valueOf(folder) + "\\" + source.getName());

private void Start() throws IOException {
    MakeDirectory(folder);
    Copy();
}

private void Copy() throws IOException {
    copyFileUsingJava7Files(source, dest);
    Add();
}

private void Add() throws IOException {
    i++;
    System.out.println("Value of i = " + i);
    System.out.println("");
}

private static void copyFileUsingJava7Files(File source, File dest)
        throws IOException {

    if (!dest.exists()) {
        System.out.println("Copying files from: " + "'" + source + "'");
        System.out.println("");
        copyFolder(source, dest);

        System.out.println("File copied");
    } else {
        copyFolder(source, dest);
    }
}

private static void copyFolder(File source, File dest) throws IOException {
    if (source.isDirectory()) {
        if (!dest.exists()) {
            dest.mkdir();
            System.out.println("Directory created :: " + dest);
        }

        String files[] = source.list();
        for (String file : files) {
            File srcFile = new File(source, file);
            File destFile = new File(dest, file);

            copyFolder(srcFile, destFile);
        }
    } else {
        if (source.lastModified() > dest.lastModified()) {
            Files.copy(source.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
            System.out.println("File copied :: " + dest);
        } else {
            System.out.println("A newer version exists of: " + "'" + dest + "'");
        }
    }
}

private static void MakeDirectory(File folder) {

    if (!folder.exists()) {
        System.out.println("Creating directory: " + "'" + folder + "'");
        folder.mkdir();

        System.out.println("Directory created");
    } else {
        System.out.println("Directory already exists: " + "'" + folder + "'");
    }
}

public static void main(String[] args) throws IOException {
    Application app = new Application();

    int l;
    for (l = 0; l < 3; l++) {
        app.Start();
    }
}
}

最佳答案

看起来您从未更改过 source初始设置后的字段。您将其设置为第二个文件,但稍后不再更改。递增i不会自动更新source因为source只是一个 File .

另外,您从 i = 1 开始。在Java中,数组是零索引的,这意味着数组中的第一项实际上是项0 ,所以你应该从 i = 0 开始相反。

关于Java程序,int i (i=0)默认值被使用,尽管它每次循环增加1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38834998/

相关文章:

java - 无法在 IntelliJ 中启动 Spring Boot +thymeleaf 应用程序

java - 每隔一分钟向数据库发送一次查询

java - 用Java解压压缩的ubuntu lz4文件

java - 终止由其他 Web 请求创建的 Web 请求中的线程

java - 通过 Apache Commons 上传流文件时,数据存储在哪里?

java - 黑莓 Java TableView : calculateVerticalScrollAmount Exception?

java - 测试 Spring MVC 时不断收到 404 错误

java - Java 中的 IncompatibleClassChangeError

java - Jackson YAML - 将 null 序列化为空值

java jdbc gui应用程序-需要允许用户输入