Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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 从jar复制文件并写入文件而不存储在内存中_Java_File_Memory_File Io_Jar - Fatal编程技术网

Java 从jar复制文件并写入文件而不存储在内存中

Java 从jar复制文件并写入文件而不存储在内存中,java,file,memory,file-io,jar,Java,File,Memory,File Io,Jar,我在jar文件中有一个文件,我需要复制该文件并将其存储在一个目录中。我可以使用什么java api来同时读/写文件,而不必先将其缓存在内存中 我知道如何从jar中单独复制一个文件,然后写入一个文件,但首先要将其缓存在内存中 下面的答案看起来好像不是缓存在内存中,但我想知道缓存与非缓存在内存中的区别 下面是一个使用内存缓存复制文件的简短示例: try { Path source = Paths.get(getClass().getClassLoader().getResou

我在jar文件中有一个文件,我需要复制该文件并将其存储在一个目录中。我可以使用什么java api来同时读/写文件,而不必先将其缓存在内存中

我知道如何从jar中单独复制一个文件,然后写入一个文件,但首先要将其缓存在内存中

下面的答案看起来好像不是缓存在内存中,但我想知道缓存与非缓存在内存中的区别


下面是一个使用内存缓存复制文件的简短示例:

    try {
        Path source = Paths.get(getClass().getClassLoader().getResource("resource").toURI());
        Path target = Paths.get("targetfile");

        // copy with in memory caching
        // read all bytes to memory
        byte[] data = Files.readAllBytes(source);
        // write bytes from memory to target file
        Files.write(target, data);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    try {
        Path source = Paths.get(getClass().getClassLoader().getResource("resource").toURI());
        Path target = Paths.get("targetfile");

        // copy without in memory caching
        Files.copy(source, target);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
同样的例子没有内存缓存:

    try {
        Path source = Paths.get(getClass().getClassLoader().getResource("resource").toURI());
        Path target = Paths.get("targetfile");

        // copy with in memory caching
        // read all bytes to memory
        byte[] data = Files.readAllBytes(source);
        // write bytes from memory to target file
        Files.write(target, data);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    try {
        Path source = Paths.get(getClass().getClassLoader().getResource("resource").toURI());
        Path target = Paths.get("targetfile");

        // copy without in memory caching
        Files.copy(source, target);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

重要的区别在于,在第一种情况下,文件的完整内容被读取到一个字节数组中,该数组存储在内存中。根据文件的大小,这可能非常糟糕。在第二种情况下,复制操作知道源和目标,因此能够将文件分为小部分复制,直到文件被完全复制为止。例如,一次可以复制2048字节,就像链接问题的已接受答案一样。

尝试链接到您的链接问题。@fscore链接的代码没有进行任何缓存,那么您在说什么?@Andreas如何确定代码是否在缓存?@fscore答案使用了一个小的2k缓冲区。这不是缓存。在术语“分配零内存”中,这不是答案(这里分配是隐藏的),但询问者必须理解这一点