试一试这两个方法,可以得到文件的读写状态: File f = new File(); f.canRead( ); f.canWrite( );
import java.io.*; File f=new File("file.txt");boolean canRead() Tests whether the application can read the file denoted by this abstract pathname. boolean canWrite() Tests whether the application can modify to the file denoted by this abstract pathname. boolean isHidden() Tests whether the file named by this abstract pathname is a hidden file. long lastModified() Returns the time that the file denoted by this abstract pathname was last modified. boolean setLastModified(long time) Sets the last-modified time of the file or directory named by this abstract pathname. boolean setReadOnly() Marks the file or directory named by this abstract pathname so that only read operations are allowed.
java.lang.Object | +--java.io.File boolean canRead() Tests whether the application can read the file denoted by this abstract pathname. boolean canWrite() Tests whether the application can modify to the file denoted by this abstract pathname. more...
File f = new File();
f.canRead( );
f.canWrite( );
File f=new File("file.txt");boolean canRead()
Tests whether the application can read the file denoted by this abstract pathname. boolean canWrite()
Tests whether the application can modify to the file denoted by this abstract pathname. boolean isHidden()
Tests whether the file named by this abstract pathname is a hidden file. long lastModified()
Returns the time that the file denoted by this abstract pathname was last modified. boolean setLastModified(long time)
Sets the last-modified time of the file or directory named by this abstract pathname. boolean setReadOnly()
Marks the file or directory named by this abstract pathname so that only read operations are allowed.
2.java.io.File.canWrite()
3.java.nio.channels.FileLock
和 canWrite().
|
+--java.io.File
boolean canRead()
Tests whether the application can read the file denoted by this abstract pathname.
boolean canWrite()
Tests whether the application can modify to the file denoted by this abstract pathname. more...
file.canRead( );
file.canWrite( );
至于你说的判断这个文件是不是正在使用中,是不是可以用异常来判断呢?
有待调查。