2

说我有这个代码:

function doFileStuff(){
    var file = "";
    try {
        file = fileOpen(filePath);
        // do stuff with file
    }
    finally {
        fileClose(file);
    }
}

如果该fileOpen()过程失败,fileClose()调用将出错。我需要做的是这种事情(伪代码):

if (isFile(file)){
    fileClose(file);
}

我知道我可以测试文件是否仍然是一个空字符串,这对我有用,但它没有测试我应该测试的内容:是否file是文件句柄。我可以检查对象的 Java 类,但这对我来说又有点 hacky,应该有一个 CFML 方法来做这件事。

应该有类似 just 的东西isFile(),不是吗?我在文档中找不到这样的东西。

有什么想法/提示吗?我在我的博客上进行了更深入的调查。这里太罗嗦了。

4

3 回答 3

1

Interesting problem. I'd use the java.io.File class. It's what CF uses internally for its file operations, with the exception of the new vfs feature.

Leveraging java shouldn't be considered hacky. It's a down right necessity sometimes ;-)

于 2012-08-09T14:47:11.827 回答
0

我将此标记为已回答:基本上没有本地方法可以做到这一点。我已经向 Adob​​e 提出了一张票,以便对其进行排序。

感谢所有的反馈。

更新:根据我提出的那张票,这已isFileObject()在 ColdFusion 11 中实现。

于 2012-07-31T10:27:04.840 回答
0

我扩展了现有 TypeOf() UDF 的功能以添加支持以返回任何变量类型。您可以使用它来确定变量是否为“FileHandler”(或“FileHandlerJava”)类型,然后相应地关闭它。它适用于 ColdFusion 9+。我发布此解决方案是因为我们没有升级到 CF11。

TypeOf() UDF(和示例用法)

https://gist.github.com/JamoCA/1ed396431dfb8e0e9f58

 /* Detect CF's fileOpen() */
if (typeOf(theFile) is "fileHandler"){
    fileClose(file);

/* Detect createobject("java","java.io.FileReader") */
else if (typeOf(theFile) is "fileHandlerJava"){
    variables['file'].close();
}
于 2016-01-06T15:57:54.127 回答