Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从文件更改位置_Java_Windows_Nio - Fatal编程技术网

Java 从文件更改位置

Java 从文件更改位置,java,windows,nio,Java,Windows,Nio,情景:我有一个游戏,在这个游戏中,您可以使用游戏提供的ingame记录器录制ingame,但无法设置录制文件的目标。所有文件将保存在游戏文件夹中。所以我决定写一个程序,如果创建了一个*.avi文件,它可以实时识别,如果没有更多的使用记录,它将被移动到另一个目的地。检查文件是否在使用中可以正常工作,但是当我想要移动文件的目标时,我得到一个java.nio.file.FileReadyExistsException,这是我当前的代码: package ca.filemover; import ja

情景:我有一个游戏,在这个游戏中,您可以使用游戏提供的ingame记录器录制ingame,但无法设置录制文件的目标。所有文件将保存在游戏文件夹中。所以我决定写一个程序,如果创建了一个*.avi文件,它可以实时识别,如果没有更多的使用记录,它将被移动到另一个目的地。检查文件是否在使用中可以正常工作,但是当我想要移动文件的目标时,我得到一个java.nio.file.FileReadyExistsException,这是我当前的代码:

package ca.filemover;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.List;

public class CAWatcher {


    public static void main(String[] args) {

        System.out.println("Scan the combat arms folder...");

        while (true) {
            Path path = Paths.get("C:\\Nexon\\Combat Arms EU");

            try {
                WatchService watcher = path.getFileSystem().newWatchService();
                path.register(watcher, StandardWatchEventKinds.ENTRY_CREATE);

                WatchKey watckKey = watcher.take();

                List<WatchEvent<?>> events = watckKey.pollEvents();
                for (WatchEvent<?> event : events) {

                    String fileName = event.context().toString();
                    String filePath = path.toString();
                    String source = filePath + "\\" + fileName;

                    while (true) {
                        if (event.context().toString().contains(".avi")) {

                            if (isFileUnlocked(source)) {

                                changeFileDest(source, fileName);
                                break;

                            }
                        }
                    }

                }

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

    }

    public static boolean isFileUnlocked(String sourceFile) {
        File fileLoc = new File(sourceFile);
        return fileLoc.renameTo(fileLoc);

    }

    public static boolean changeFileDest(String source, String fileName) {

        Path moveSourcePath = Paths.get(source);
        Path moveTargetPath = Paths.get("D:\\Desktop\\CA Videos\\Reports\\" + fileName);

        try {
            Files.move(moveSourcePath, moveTargetPath);

        } catch (IOException e) {

            e.printStackTrace();
        }
        return false;

    }

}   
java.nio.file.FileAlreadyExistsException:D:\Desktop\CA 视频\报告\CA_2015_03_30_42.avi sun.nio.fs.WindowsFileCopy.moveUnknown源位于 sun.nio.fs.WindowsFileSystemProvider.moveUnknown源位于 java.nio.file.Files.moveUnknown Source at ca.filemover.CAWatcher.changefiledestcawtcher.java:73 at ca.filemover.CAWatcher.maincawtcher.java:44


提前谢谢。

好的,我不明白问题是什么。该文件已存在。这有什么不可预料的?目标文件夹是空的,它怎么可能已经存在?您的错误表示您正在尝试创建一个文件夹,该文件夹已经存在,并且是空的empty@tnwREPLACE_EXISTING无法解析为variableidk im不适用于java nio,我只需使用普通文件即可,但错误是这样说的:D,尝试tnw所说的