Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.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_Error Handling - Fatal编程技术网

在java中移动文件?

在java中移动文件?,java,error-handling,Java,Error Handling,我正在尝试使用以下代码将文件从目录workspce/TextInput移动到workspace/CPUInput: String Direct = System.getProperty("user.dir"); File f1 = new File(Direct + "/FirstChar.txt"); f1.renameTo("../CPUInput"); 但是在f1.renameTo(“../CPUInput”)它给我一个编译器错误类型文件中的方法renameTo(文件

我正在尝试使用以下代码将文件从目录
workspce/TextInput
移动到
workspace/CPUInput

String Direct = System.getProperty("user.dir");

    File f1 = new File(Direct + "/FirstChar.txt");

    f1.renameTo("../CPUInput");
但是在
f1.renameTo(“../CPUInput”)它给我一个编译器错误
类型文件中的方法renameTo(文件)不适用于参数(字符串)


如果我不能为
renameTo()
使用字符串参数,那么我应该向方法传递哪些参数?

您需要传递一个
文件
实例,如:

f1.renameTo(new File("../CPUInput.txt"));

您需要传递一个
文件
实例,如:

f1.renameTo(new File("../CPUInput.txt"));
试试这个

public void renameFile(String strOldFileName, String strNewFileName) {
    File oldName = new File(strFilePath + "/" + strOldFileName);
    File newName = new File(strFilePath + "/" + strNewFileName);
    if (oldName.renameTo(newName)) {
        System.out.println(strOldFileName + "renamed to " + strNewFileName);
    } else {
        System.out.println("Error");
    }
}
试试这个

public void renameFile(String strOldFileName, String strNewFileName) {
    File oldName = new File(strFilePath + "/" + strOldFileName);
    File newName = new File(strFilePath + "/" + strNewFileName);
    if (oldName.renameTo(newName)) {
        System.out.println(strOldFileName + "renamed to " + strNewFileName);
    } else {
        System.out.println("Error");
    }
}
使用JDK6 让我们从纯Java JDK6解决方案开始:

@Test
public void givenUsingJDK6_whenMovingFile_thenCorrect() throws IOException {
    File fileToMove = new File("src/test/resources/toMoveFile_jdk6.txt");
    boolean isMoved = fileToMove.renameTo(new File("src/test/resources/movedFile_jdk6.txt"));
    if (!isMoved) {
        throw new FileSystemException("src/test/resources/movedFile_jdk6.txt");
    }
}
使用NIO和JDK7 现在让我们看看如何使用NIO和JDK 7实现同样的功能:

@Test
public void givenUsingJDK7Nio2_whenMovingFile_thenCorrect() throws IOException {
    Path fileToMovePath = 
      Files.createFile(Paths.get("src/test/resources/" + randomAlphabetic(5) + ".txt"));
    Path targetPath = Paths.get("src/main/resources/");

    Files.move(fileToMovePath, targetPath.resolve(fileToMovePath.getFileName()));
}
与Commons IO 最后,让我们看一看使用Apache Commons IO的解决方案—可能是最简单的解决方案:

@Test
public void givenUsingApache_whenMovingFile_thenCorrect() throws IOException {
    FileUtils.moveFile(
      FileUtils.getFile("src/test/resources/fileToMove.txt"), 
      FileUtils.getFile("src/test/resources/fileMoved.txt"));
}
这一行当然允许移动或重命名,这取决于目标目录是否相同

或者–这里有一个专门移动的解决方案,还允许我们在目标目录不存在时自动创建目标目录:

@Test
public void givenUsingApache_whenMovingFileApproach2_thenCorrect() throws IOException {
    FileUtils.moveFileToDirectory(
      FileUtils.getFile("src/test/resources/fileToMove.txt"), 
      FileUtils.getFile("src/main/resources/"), true);
}
我们在这些代码片段中查看了重命名,但是移动当然是完全相同的,只是目标目录需要不同

资源链接:

使用JDK6 让我们从纯Java JDK6解决方案开始:

@Test
public void givenUsingJDK6_whenMovingFile_thenCorrect() throws IOException {
    File fileToMove = new File("src/test/resources/toMoveFile_jdk6.txt");
    boolean isMoved = fileToMove.renameTo(new File("src/test/resources/movedFile_jdk6.txt"));
    if (!isMoved) {
        throw new FileSystemException("src/test/resources/movedFile_jdk6.txt");
    }
}
使用NIO和JDK7 现在让我们看看如何使用NIO和JDK 7实现同样的功能:

@Test
public void givenUsingJDK7Nio2_whenMovingFile_thenCorrect() throws IOException {
    Path fileToMovePath = 
      Files.createFile(Paths.get("src/test/resources/" + randomAlphabetic(5) + ".txt"));
    Path targetPath = Paths.get("src/main/resources/");

    Files.move(fileToMovePath, targetPath.resolve(fileToMovePath.getFileName()));
}
与Commons IO 最后,让我们看一看使用Apache Commons IO的解决方案—可能是最简单的解决方案:

@Test
public void givenUsingApache_whenMovingFile_thenCorrect() throws IOException {
    FileUtils.moveFile(
      FileUtils.getFile("src/test/resources/fileToMove.txt"), 
      FileUtils.getFile("src/test/resources/fileMoved.txt"));
}
这一行当然允许移动或重命名,这取决于目标目录是否相同

或者–这里有一个专门移动的解决方案,还允许我们在目标目录不存在时自动创建目标目录:

@Test
public void givenUsingApache_whenMovingFileApproach2_thenCorrect() throws IOException {
    FileUtils.moveFileToDirectory(
      FileUtils.getFile("src/test/resources/fileToMove.txt"), 
      FileUtils.getFile("src/main/resources/"), true);
}
我们在这些代码片段中查看了重命名,但是移动当然是完全相同的,只是目标目录需要不同

资源链接:

我认为这不会满足OP的要求。这将相对于
user.dir
环境变量来解决,而OP希望将文件移动到
workspace/CPUInput
@DavidWallace。这只是为了了解
renameTo
的参数是
文件
实例,而不是
字符串
。当然,他需要为解决问题找到正确的途径,但是如果你要用这样的相对途径给出一个解决方案,包括解释相对途径是如何解决的有什么害处?我认为这不会满足OP的要求。这将相对于
user.dir
环境变量来解决,而OP希望将文件移动到
workspace/CPUInput
@DavidWallace。这只是为了了解
renameTo
的参数是
文件
实例,而不是
字符串
。当然,他需要给出正确的解决方法,但是如果你要用这样一个相对路径来给出一个解决方案,那么包括对相对路径如何得到解决的解释有什么害处呢?