如何检查文件是否存在于 Eiffel 中
How to check if file exists in Eiffel
feature
open_file_sample
local
l_file: UNIX_FILE_INFO
l_path: STRING
do
make
l_path := "/var/log/syslog"
l_file.update (l_path)
if l_file.parent_directory.exists and then l_file.parent_directory.is_writtable then
create l_file.make
end
-- AS the above statement doesn't exist!
check
syslog_file_exists_and_is_readable: l_file.exists and then l_file.is_readable
end
end
这是在 Eiffel 中检查文件是否存在的正确方法吗?
我想知道是否有办法不创建 2 个对象。我将使用以下语句完成检查:
- 定义路径`l_file_path := "/some/path/with_file.log"
- 检查父目录是否存在并有权写入
- 创建日志文件
do
if not l_file.exists then
print ("error: '" + l_path + "' does not exist%N")
else
...
你可以像这样
你可以使用
{FILE_UTILITIES}.file_exists (the_file_name)
或
(创建 {RAW_FILE}.make_with_name (the_file_name)).exists
我的最终解决方案如下,并且受到批评,我个人认为与更多低级语言和库相比它非常复杂(如 bash for ex)
log_file_path: detachable PATH
-- Attached if can be created
local
l_file: UNIX_FILE_INFO
l_path, l_parent_dir: PATH
l_fu: FILE_UTILITIES
do
create l_fu
-- Parent directory check
create l_path.make_from_string ({APP_CONFIGURATION}.application_log_file_path)
l_parent_dir := l_path.parent
if not l_fu.directory_exists (l_parent_dir.out) then
l_fu.create_directory_path (l_parent_dir)
end
create l_file.make
l_file.update (l_parent_dir.out)
if not l_file.exists or
l_file.is_access_writable
then
io.putstring ("Error: " + log_file_path_string + " parent directory is not writtable and cannot be created")
check
parent_dir_exists_and_is_writtable: False
end
else
Result := l_path
end
ensure
file_name_could_be_created: Result /= Void
end
访问文件系统时出现的问题是文件或目录的属性可能在您查询它和您要使用它的时间之间发生了变化(即使它只是一小部分一秒钟)。因此,Eiffel 中的断言形式为:
f (a_file: RAW_FILE)
require
a_file.is_writable
do
a_file.open_write
可能会违规。在 Gobo Eiffel 库中,没有在实际打开文件之前检查文件是否可以写入模式打开,而是选择了 revert 方法:尝试打开文件,并检查它是否打开成功。
f (a_pathname: STRING)
local
l_file: KL_TEXT_OUTPUT_FILE
do
create l_file.make (a_pathname)
l_file.recursive_open_write
if l_file.is_open_write then
-- Write to the file.
l_file.close
else
-- Report the problem.
end
请注意,它使用 recursive_open_write
而不仅仅是 open_write
,因此也会创建路径中缺少的目录。
feature
open_file_sample
local
l_file: UNIX_FILE_INFO
l_path: STRING
do
make
l_path := "/var/log/syslog"
l_file.update (l_path)
if l_file.parent_directory.exists and then l_file.parent_directory.is_writtable then
create l_file.make
end
-- AS the above statement doesn't exist!
check
syslog_file_exists_and_is_readable: l_file.exists and then l_file.is_readable
end
end
这是在 Eiffel 中检查文件是否存在的正确方法吗?
我想知道是否有办法不创建 2 个对象。我将使用以下语句完成检查:
- 定义路径`l_file_path := "/some/path/with_file.log"
- 检查父目录是否存在并有权写入
- 创建日志文件
do
if not l_file.exists then
print ("error: '" + l_path + "' does not exist%N")
else
...
你可以像这样
你可以使用 {FILE_UTILITIES}.file_exists (the_file_name)
或 (创建 {RAW_FILE}.make_with_name (the_file_name)).exists
我的最终解决方案如下,并且受到批评,我个人认为与更多低级语言和库相比它非常复杂(如 bash for ex)
log_file_path: detachable PATH
-- Attached if can be created
local
l_file: UNIX_FILE_INFO
l_path, l_parent_dir: PATH
l_fu: FILE_UTILITIES
do
create l_fu
-- Parent directory check
create l_path.make_from_string ({APP_CONFIGURATION}.application_log_file_path)
l_parent_dir := l_path.parent
if not l_fu.directory_exists (l_parent_dir.out) then
l_fu.create_directory_path (l_parent_dir)
end
create l_file.make
l_file.update (l_parent_dir.out)
if not l_file.exists or
l_file.is_access_writable
then
io.putstring ("Error: " + log_file_path_string + " parent directory is not writtable and cannot be created")
check
parent_dir_exists_and_is_writtable: False
end
else
Result := l_path
end
ensure
file_name_could_be_created: Result /= Void
end
访问文件系统时出现的问题是文件或目录的属性可能在您查询它和您要使用它的时间之间发生了变化(即使它只是一小部分一秒钟)。因此,Eiffel 中的断言形式为:
f (a_file: RAW_FILE)
require
a_file.is_writable
do
a_file.open_write
可能会违规。在 Gobo Eiffel 库中,没有在实际打开文件之前检查文件是否可以写入模式打开,而是选择了 revert 方法:尝试打开文件,并检查它是否打开成功。
f (a_pathname: STRING)
local
l_file: KL_TEXT_OUTPUT_FILE
do
create l_file.make (a_pathname)
l_file.recursive_open_write
if l_file.is_open_write then
-- Write to the file.
l_file.close
else
-- Report the problem.
end
请注意,它使用 recursive_open_write
而不仅仅是 open_write
,因此也会创建路径中缺少的目录。