简体   繁体   English

如何使用TrueVFS for Java通过路径访问文件?

[英]How can I access a file through a path using TrueVFS for java?

Here's my code. 这是我的代码。

/**
 * Load an Object from disk.
 */
public static <T> T load(String path) throws IOException {
    File entry = new TFile(path);
    char[] chars = new char[(int)entry.length()];
    InputStream in = new TFileInputStream(entry);
    BufferedReader fIn = new BufferedReader(new InputStreamReader(in, "UTF-8"));
    fIn.read(chars);
    fIn.close();
    //converts the string to an object using the Base64Coder
    return decode(new String(chars));
}


/**
 * Save an object to disk as a compressed file.
 */
public static void save(String path, Serializable o) {
    //converts the object to a string
    String content = encode(o);
    File entry = new TFile(path);
    Writer writer;
    try {
        writer = new TFileWriter(entry);
        writer.write(content);
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

I'm trying to use TrueVFS to load and save objects from memory into a compressed file. 我正在尝试使用TrueVFS将对象从内存中加载并保存到压缩文件中。 This code here works, but only when i reference the file directly. 此代码在这里有效,但是仅当我直接引用该文件时。 (So the path given is something like "data.zip/data Entries/0/2/data.txt") (因此给定的路径类似于“ data.zip/data Entries / 0/2 / data.txt”)

The problem i have is that as soon as i try to put this file into a path (So the path given is something like "Save Files/World 1/data.zip/dataEntry/0/2/data.txt"), i get a NoSuchFileException error. 我的问题是,一旦我尝试将此文件放入路径中(因此给定的路径类似于“保存文件/世界1 / data.zip / dataEntry / 0/2 / data.txt”),我得到一个NoSuchFileException错误。

How should i go about putting my archives into a specified folder? 我应该如何将档案保存到指定的文件夹中?

Solved the problem, here's the new code. 解决了问题,这是新代码。

/**
 * Load an Object from disk.
 */
public static <T> T load(String directory, String fileName, String archiveDirectory) throws IOException {
    File entry = new TFile(directory + fileName, archiveDirectory);
    char[] chars = new char[(int)entry.length()];
    InputStream in = new TFileInputStream(entry);
    BufferedReader fIn = new BufferedReader(new InputStreamReader(in, "UTF-8"));
    fIn.read(chars);
    fIn.close();
    return decode(new String(chars));
}


/**
 * Save an object to disk as a compressed file.
 */
public static void save(String directory, String fileName, String archiveDirectory, Serializable o) {
    String content = encode(o);
    Path parent = new TPath(directory);
    Writer writer;
    try {
        if (!Files.exists(parent)) {
            Files.createDirectory(parent);
        }
        File entry = new TFile(directory + fileName, archiveDirectory);
        writer = new TFileWriter(entry);
        writer.write(content);
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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

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