代号一个 InputStream 和 OutputStream

Codename One InputStream and OutputStream

我对 StorageFileSystemStorage 有点困惑。我写了下面的方法,但我确信它们没有按预期工作,因为 .contains("/") 不足以区分我们使用的是 Storage 还是 FileSystemStorage

你能帮我解决一下吗?谢谢

/**
     * Get an InputStream for the given sourceFile, it automatically chooses
     * FileSystem API or Storage API
     *
     * @param sourceFile
     * @return
     * @throws java.io.IOException
     */
    public static InputStream getInputStream(String sourceFile) throws IOException {
        if (sourceFile.contains("/")) {
            return FileSystemStorage.getInstance().openInputStream(sourceFile);
        } else {
            // Storage is a flat file system
            return Storage.getInstance().createInputStream(sourceFile);
        }
    }

    /**
     * Get an OutputStream for the given sourceFile, it automatically chooses
     * FileSystem API or Storage API
     *
     * @param destFile
     * @return
     * @throws java.io.IOException
     */
    public static OutputStream getOutputStream(String destFile) throws IOException {
        if (destFile.contains("/")) {
            return FileSystemStorage.getInstance().openOutputStream(destFile);
        } else {
            // Storage is a flat file system
            return Storage.getInstance().createOutputStream(destFile);
        }
    }

其实应该还不错吧。理论上,存储允许您使用 / 作为文件名的一部分,但老实说,这不是我们测试过的东西,我不确定这样做是否正确。

FileSystemStorage 需要一个绝对路径,因此将始终包含一个斜杠字符。所以这应该可以正常工作。从技术上讲,FileSystemStorage 路径应该以 file:// 开头,但 API 经常在没有它的情况下工作,以使本机代码集成更容易,因此这不是区分 API 的好方法。