相关疑难解决方法(0)

用Java复制文件的标准简洁方法?

一直困扰我的是,用Java复制文件的唯一方法是打开流,声明一个缓冲区,读取一个文件,循环遍历它,然后将其写入另一个文件.Web上充斥着类似但仍然略有不同的此类解决方案的实现.

有没有更好的方法保持在Java语言的范围内(意味着不涉及执行特定于操作系统的命令)?也许在一些可靠的开源实用程序包中,这至少会掩盖这个底层实现并提供单行解决方案?

java copy file

417
推荐指数
13
解决办法
26万
查看次数

将整个目录内容复制到另一个目录?

将整个目录内容复制到java或groovy中的另一个目录的方法?

java file-io grails groovy

63
推荐指数
6
解决办法
13万
查看次数

如何将文件从一个位置复制到另一个位置?

我想将文件从一个位置复制到Java中的另一个位置.

这是我到目前为止:

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
public class TestArrayList {
    public static void main(String[] args) {
        File f = new File(
            "D:\\CBSE_Demo\\Demo_original\\fscommand\\contentplayer\\config");
        List<String>temp=new ArrayList<String>();
        temp.add(0, "N33");
        temp.add(1, "N1417");
        temp.add(2, "N331");
        File[] matchingFiles = null;
        for(final String temp1: temp){
            matchingFiles = f.listFiles(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.startsWith(temp1);
                }
            });
            System.out.println("size>>--"+matchingFiles.length);

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这不会复制文件,执行此操作的最佳方法是什么?

java io

56
推荐指数
4
解决办法
19万
查看次数

如何将文件夹及其所有子文件夹和文件复制到另一个文件夹中

如何将文件夹及其所有子文件夹和文件复制到另一个文件夹中?

java

18
推荐指数
2
解决办法
7万
查看次数

如何将文件从目录复制到Java中的另一个目录

我在用JDK 6.

我有2个文件夹名称Folder1Folder2.

Folder1 有以下文件

TherMap.txt

TherMap1.txt

TherMap2.txt
Run Code Online (Sandbox Code Playgroud)

每次Folder2只有一个名称为的文件TherMap.txt.

我想要的是,

从中复制任何文件folder1并将其粘贴Folder2TherMap.txt.如果已经TherMap.txt存在Folder2,则删除并粘贴它.

因为我写了下面的代码.但它不起作用

public void FileMoving(String sourceFilePath, String destinationPath, String fileName) throws IOException {
    File destinationPathObject = new File(destinationPath);
    File sourceFilePathObject = new File(sourceFilePath);
    if ((destinationPathObject.isDirectory()) && (sourceFilePathObject.isFile()))
    //both source and destination paths are available 
    {
        //creating object for File class
        File statusFileNameObject = new File(destinationPath + "/" + fileName);
        if …
Run Code Online (Sandbox Code Playgroud)

java

6
推荐指数
1
解决办法
3万
查看次数

使用java将所有文件从文件夹移动到其他文件夹

可能重复:
使用Java将文件从一个目录复制到另一个目录

如何使用java将所有文件从一个文件夹移动到其他文件夹?我正在使用此代码:

import java.io.File;

    public class Vlad {

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            // File (or directory) to be moved
            File file = new File("C:\\Users\\i074924\\Desktop\\Test\\vlad.txt");

            // Destination directory
            File dir = new File("C:\\Users\\i074924\\Desktop\\Test2");

            // Move file to new directory
            boolean success = file.renameTo(new File(dir, file.getName()));
            if (!success) {
                System.out.print("not good");
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

但它仅适用于一个特定文件.

谢谢!!!

java

5
推荐指数
3
解决办法
4万
查看次数

Junit无法使用Spark Structured Streaming创建的文件删除@TempDir

我为管道创建了集成测试,以检查是否生成了正确的CSV文件:

class CsvBatchSinkTest {

    @RegisterExtension
    static SparkExtension spark = new SparkExtension();

    @TempDir
    static Path directory;

    //this checks if the file is already available
    static boolean isFileWithSuffixAvailable(File directory, String suffix) throws IOException {
        return Files.walk(directory.toPath()).anyMatch(f -> f.toString().endsWith(suffix));
    }

    //this gets content of file
    static List<String> extractFileWithSuffixContent(File file, String suffix) throws IOException {
        return Files.readAllLines(
                Files.walk(file.toPath())
                        .filter(f -> f.toString().endsWith(suffix))
                        .findFirst()
                        .orElseThrow(AssertionException::new));
    }

    @Test
    @DisplayName("When correct dataset is sent to sink, then correct csv file should be generated.")
    void testWrite() throws IOException, InterruptedException …
Run Code Online (Sandbox Code Playgroud)

java junit tempdir temporary-directory

5
推荐指数
1
解决办法
174
查看次数

将文件从一个文件夹复制到另一个文件夹

我正在尝试将文件从一个文件夹复制到另一个文件夹.

这是我在我的代码中得到的:

public static void copyFile(String path) throws IOException{
   newPath = path;    
   File destination = new File ("E:/QA/chart.js"); 
   FileUtils.copyFile(destination, new File(newPath));      
}
Run Code Online (Sandbox Code Playgroud)

但它并未将所需文件复制到其位置.需要什么,从E盘复制chart.js并复制到newPath变量位置.有没有其他方法将文件从一个地方复制到另一个地方?

java file-copying

4
推荐指数
1
解决办法
3万
查看次数

标签 统计

java ×8

copy ×1

file ×1

file-copying ×1

file-io ×1

grails ×1

groovy ×1

io ×1

junit ×1

tempdir ×1

temporary-directory ×1