当前位置:首页 » 办公资讯 » 怎样判断文件是否可读

怎样判断文件是否可读

发布时间: 2022-04-04 12:40:24

1. 求高手解答c#如何判断文件是否可读!

思路:对该文件进行移动、改名、打开等操作,如果出错,说明文件被独占。比如:
try{File.Move(path,path);}catch(){return True;}

2. 在Java里面如何判断一个文件为可读可写文件啊

File file = new File("路径");
file.canRead() 返回boolean值表示是否可读
file.canWrite() 返回boolean值表示是否可写

3. 判断文件可读性时: while((i=a.read())=-1) 这句话如何理解(尽量说通俗点)

c及c++读取文件遇到文件末尾时的返回值就是-1,i=a.read() 将读取到的值赋给i,并且(i=a.read() )这个表达式的值就是刚 i 得到的值,整个意思是如果读取文件没有读完就循环
将-1改成EOF比较好,这是一个宏,值也是-1,但是这样意思更明确

4. 如何使用linux系统调用查看文件是否可读

$ls -l filename
将显示例如下格式:
--rwx-rw-r
r--可读(Read)
w--可写(Write)
x--可执行(eXecute)
前面三个表示文件所有者的权限,中间三个表示文件所属组拥有的权限,最后三个表示其他用户拥有的权限。
也可以用数字表达1表示有执行权限,2表示有写权限 ,4表示有读取权限,三者相加就是所拥有的权限,如上面的--rwx--rw-r换成数字则是764
希望能帮到你。

5. 如何用VC++检查文件夹是否可读,可写

用access函数。

#include <io.h>
#include <errno.h>

原型:int access(char* pathname, int mode);
参数:mode
-- 00(仅检查是否存在)
-- 02(检查写权限)
-- 04(检查读权限)
-- 06(检查读写权限)

返回值:如果文件具有指定模式则返回0,如果返回-1则表示指定文件不存在或不能按指定模式进行存取。

///////////////////////////////////////////////////////////////

如果是在Windows下,则可以使用GetFileAttributes() API来获取文件夹的属性:

DWORD GetFileAttributes(
LPCTSTR lpFileName // name of file or directory
);

返回值:

FILE_ATTRIBUTE_ARCHIVE
[The file or directory is an archive file or directory. Applications use this attribute to mark files for backup or removal. ]

FILE_ATTRIBUTE_COMPRESSED
[The file or directory is compressed. For a file, this means that all of the data in the file is compressed. For a directory, this means that compression is the default for newly created files and subdirectories.]

FILE_ATTRIBUTE_DEVICE
[Reserved; do not use.]

FILE_ATTRIBUTE_DIRECTORY
[The handle identifies a directory.]

FILE_ATTRIBUTE_ENCRYPTED
[The file or directory is encrypted. For a file, this means that all data streams in the file are encrypted. For a directory, this means that encryption is the default for newly created files and subdirectories.]

FILE_ATTRIBUTE_HIDDEN
[The file or directory is hidden. It is not included in an ordinary directory listing. ]

FILE_ATTRIBUTE_NORMAL
[The file or directory has no other attributes set. This attribute is valid only if used alone. ]

FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
[ The file will not be indexed by the content indexing service. ]

FILE_ATTRIBUTE_OFFLINE
[The data of the file is not immediately available. This attribute indicates that the file data has been physically moved to offline storage. This attribute is used by Remote Storage, the hierarchical storage management software. Applications should not arbitrarily change this attribute. ]

FILE_ATTRIBUTE_READONLY
[The file or directory is read-only. Applications can read the file but cannot write to it or delete it. In the case of a directory, applications cannot delete it. ]

FILE_ATTRIBUTE_REPARSE_POINT
[The file has an associated reparse point. ]

FILE_ATTRIBUTE_SPARSE_FILE
[ The file is a sparse file. ]

FILE_ATTRIBUTE_SYSTEM
[ The file or directory is part of, or is used exclusively by, the operating system. ]

FILE_ATTRIBUTE_TEMPORARY
[The file is being used for temporary storage. File systems attempt to keep all of the data in memory for quicker access rather than flushing the data back to mass storage. A temporary file should be deleted by the application as soon as it is no longer needed. ]

例如:检查文件夹是否只读

BOOL DirectoryIsReadonly(char* szPath)
{
DWORD dwAttr = GetFileAttributes(szPath);

//是文件夹 && 只读
return ((dwAttr & FILE_ATTRIBUTE_DIRECTORY) != 0) && ((dwAttr & FILE_ATTRIBUTE_READONLY) != 0);
}

检查其它属性类推。

6. VB中怎么判断.dat文件是否可读

应该有的吧,但我不知道。。。

7. 有没有办法只检测现在硬盘里的文件是否可读

如果用HDtune检测你都嫌浪费时间,那没有其他办法了。

8. 怎么判断文件是否打开

int _access( const char *path, int mode );
参数path:是所要判断状态的文件名。
参数mode:是判断文件状态的标志。
参数mode有以下几种形式:
00:表示判断文件是否存在
02:表示判断文件是否可写
04:表示判断文件是否可读
06:表示判断文件是否又可读又可写
------解决方案--------------------------------------------------------
HANDLE CreateFile(
LPCTSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
);用这个函数生成一个新文件,它的返回值是HANDLE型,如果函数调用成功就返回打开文件的句柄。如果调用前文件已经存在并且dwCreation Disposition参数使用CREATE_ALWAYS或OPEN_ALWAYS,则返回ERROR_ ALREADY_ EXISTS。

9. linux下如何测试某个用户对某个文件是否有读写权限

[ -r filename ]
echo $?
如果返回值为0,则说明该文件可读,如果返回值非0,则说明不具有读的权限
[ -w filename ]
echo $?
如果返回值为0,则说明该文件可写,如果返回值非0,则说明不具有写的权限

如果说不是编写脚本的话我们可以通过ls -l filename 从而来查看它的属主、属组和其他人的权限从而来判断该用户对文件是否具有读写的权限。

10. 用Shell编程,判断一文件是不是可读文件,如果是将其拷贝到/dev 目录下。

字符设备文件的第一个属性为[c],因此shell脚本如下:
myfile=/path/filename
#
换成你实际的文件全路径
fd=`ls
-l
myfile`
#
获取文件详细信息
fp=${fd:0:1}
#
截取第一个属性值
[
"$fp"
=
"c"
]
&&
cp
myfile
/dev
#
如果该属性值为c,则为字符设备文件,拷贝到/dev目录

热点内容
马路上汽车的噪音在多少分贝 发布:2023-08-31 22:08:23 浏览:1742
应孕棒多少钱一盒 发布:2023-08-31 22:08:21 浏览:1239
标准养老金一年能领多少钱 发布:2023-08-31 22:05:05 浏览:1526
湖北通城接网线多少钱一个月 发布:2023-08-31 21:59:51 浏览:1605
开随车吊车多少钱一个月 发布:2023-08-31 21:55:06 浏览:1366
京东付尾款怎么知道前多少名 发布:2023-08-31 21:52:58 浏览:1687
在学校租铺面一个月要多少钱 发布:2023-08-31 21:52:09 浏览:1823
2寸有多少厘米 发布:2023-08-31 21:50:34 浏览:1459
知道电压如何算一小时多少电 发布:2023-08-31 21:46:20 浏览:1446
金手镯54号圈周长是多少厘米 发布:2023-08-31 21:44:28 浏览:1619