當前位置:首頁 » 辦公資訊 » 怎樣判斷文件是否可讀

怎樣判斷文件是否可讀

發布時間: 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 瀏覽:1458
知道電壓如何算一小時多少電 發布:2023-08-31 21:46:20 瀏覽:1446
金手鐲54號圈周長是多少厘米 發布:2023-08-31 21:44:28 瀏覽:1619