0

这是我的代码。

/**
 * 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();
    }
}

我正在尝试使用 TrueVFS 将对象从内存加载并保存到压缩文件中。此处的代码有效,但仅当我直接引用该文件时。(所以给出的路径类似于“data.zip/data Entries/0/2/data.txt”)

我遇到的问题是,一旦我尝试将此文件放入路径中(因此给出的路径类似于“Save Files/World 1/data.zip/dataEntry/0/2/data.txt”),我收到 NoSuchFileException 错误。

我应该如何将我的档案放入指定的文件夹中?

4

1 回答 1

0

解决了问题,这是新代码。

/**
 * 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();
    }
}
于 2014-06-11T11:02:37.740 回答