简体   繁体   English

为什么我的测试文件系统仅在内存中工作?

[英]Why does my test filesystem only work in memory?

I am working on a concept of a filesystem for a program. 我正在研究程序文件系统的概念。 I am writing in Java (using JDK 7 u17). 我正在用Java编写(使用JDK 7 u17)。

To get started I built off of some tutorial that were showing my how to create a zip based filesystem using the FileSystemProvider class. 首先,我建立了一些教程,这些教程向我展示了如何使用FileSystemProvider类创建基于zip的文件系统。

When I execute the code I have it do similar task to the examples which is copy a text file from the my desktop and place it in the zip file. 当我执行代码时,它会执行与示例类似的任务,即从我的桌面复制文本文件并将其放在zip文件中。 The problem is once it copies the file it does not write it into the zip file, it seems to leave the file in memory which is destroyed when the program is terminated. 问题是,一旦它复制了文件但没有将其写入zip文件,则似乎将文件留在了内存中,该内存在程序终止时被破坏了。

The problem is I cannot understand why, as far as I can tell everything looks to be in order but something is clearly not! 问题是我不明白为什么,据我所知一切看起来都是井井有条的,但显然不是!

Oh yeah the same thing goes for directories too. 哦,是的,目录也一样。 If I tell the filesystem to make a new directory it just creates it in memory and there is nothing in the zip file. 如果我告诉文件系统创建一个新目录,它将在内存中创建它,而zip文件中没有任何内容。

Anyhow here is my working code; 无论如何,这是我的工作代码;

import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

public class Start {

    public static void main(String[] args) {

        Map <String, String> env = new HashMap<>();
        env.put("create", "true");
        env.put("encoding", "UTF-8");

        FileSystem fs = null;

        try {
            fs = FileSystems.newFileSystem(URI.create("jar:file:/Users/Ian/Desktop/test.zip"), env);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Path externalTxtFile = Paths.get("/Users/Ian/Desktop/example.txt");
        Path pathInZipFile = fs.getPath("/example.txt");

        try {
            Files.createDirectory(fs.getPath("/SomeDirectory"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (Files.exists(fs.getPath("/SomeDirectory"))) {
            System.out.println("Yes the directory exists in memory.");
        } else {
            System.out.println("What directory?");
        }       

        // Why is the file only being copied into memory and not written out the jar/zip archive?
        try {
            Files.copy(externalTxtFile, pathInZipFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // The file clearly exists just before the program ends, what is going on?
        if (Files.exists(fs.getPath("/example.txt"))) {
            System.out.println("Yes the file has been copied into memory.");
        } else {
            System.out.println("What file?");
        }

    }

}

I just want to add something. 我只想添加一些内容。 Perhaps the example that you found was incomplete (I can not check since you do not references it) but in all examples I found the FileSystem instance is closed properly. 也许您发现的示例不完整(由于您未引用它,所以无法检查),但是在所有示例中,我都发现FileSystem实例已正确关闭。

The FileSystem abstract class implements Closeable, so the close() method is called (automatically) leaving the try in the following code: FileSystem抽象类实现Closeable,因此将(自动)调用close()方法,并在以下代码中进行尝试:

try (final FileSystem fs = FileSystems.newFileSystem(theUri, env)) {
    /* ... do everything you want here ; do not need to call fs.close() ... */
}

http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM