Java的File对象
来源:互联网 发布:js获取frame 编辑:程序博客网 时间:2024/05/22 03:40
File对象在Java开发中经常需要碰到,其构造方法大家都比较熟悉了:
File(File dir,String name)
使用File对象路径和文件名进行创建句柄
File(String path)
只使用路径创建句柄
File(String dirPath,String name)
使用字符路径和文件名来创建句柄
File(URI uri)
使用URI路径来创建句柄
在使用构造函数之后,这个File对象就存在了,你可以使用File.exists()来对其进行判断,存在则返回true。
Java使用介乎于UNIX和Windows的路径方法,前斜线“/”可以正确解释,而如果要使用反斜线“\”则需要进行转义序列“\\”,才能被解释器正确解释。
很重要的一点就是,Java的File对象可以是文件也可以是文件夹。所以当你使用
File file=new File("/java");后
你返回的是文件夹,但是你在后面加一句
File fileNext=new File("/java/text.txt");
file使用isFile();的返回值就会变成了false。它变成了文件夹,也就是“目录”。
对于目录属性的File,我们可以使用list()来显示目录下的内容。
其他方法摘要,可以参考以下文档
boolean
canRead()
Indicates whether the current context is allowed to read from this file.
boolean
canWrite()
Indicates whether the current context is allowed to write to this file.
int
compareTo(File another)
Returns the relative sort ordering of the paths for this file and the file
another
. boolean
createNewFile()
Creates a new, empty file on the file system according to the path information stored in this file.
static File
createTempFile(String prefix,String suffix)
Creates an empty temporary file using the given prefix and suffix as part of the file name.
static File
createTempFile(String prefix,String suffix,File directory)
Creates an empty temporary file in the given directory using the given prefix and suffix as part of the file name.
boolean
delete()
Deletes this file.
void
deleteOnExit()
Schedules this file to be automatically deleted once the virtual machine terminates.
boolean
equals(Object obj)
Compares
obj
to this file and returns true
if they represent thesame object using a path specific comparison. boolean
exists()
Returns a boolean indicating whether this file can be found on the underlying file system.
File
getAbsoluteFile()
Returns a new file constructed using the absolute path of this file.
String
getAbsolutePath()
Returns the absolute path of this file.
File
getCanonicalFile()
Returns a new file created using the canonical path of this file.
String
getCanonicalPath()
Returns the absolute path of this file with all references resolved.
String
getName()
Returns the name of the file or directory represented by this file.
String
getParent()
Returns the pathname of the parent of this file.
File
getParentFile()
Returns a new file made from the pathname of the parent of this file.
String
getPath()
Returns the path of this file.
int
hashCode()
Returns an integer hash code for the receiver.
boolean
isAbsolute()
Indicates if this file's pathname is absolute.
boolean
isDirectory()
Indicates if this file represents a directory on the underlying file system.
boolean
isFile()
Indicates if this file represents a file on the underlying file system.
boolean
isHidden()
Returns whether or not this file is a hidden file as defined by the operating system.
long
lastModified()
Returns the time when this file was last modified, measured in milliseconds since January 1st, 1970, midnight.
long
length()
Returns the length of this file in bytes.
String[]
list()
Returns an array of strings with the file names in the directory represented by this file.
String[]
list(FilenameFilter filter)
Gets a list of the files in the directory represented by this file.
File[]
listFiles()
Returns an array of files contained in the directory represented by this file.
File[]
listFiles(FileFilter filter)
Gets a list of the files in the directory represented by this file.
File[]
listFiles(FilenameFilter filter)
Gets a list of the files in the directory represented by this file.
static File[]
listRoots()
Lists the file system roots.
boolean
mkdir()
Creates the directory named by the trailing filename of this file.
boolean
mkdirs()
Creates the directory named by the trailing filename of this file, including the complete directory path required to create this directory.
boolean
renameTo(File dest)
Renames this file to the name represented by the
dest
file. boolean
setLastModified(long time)
Sets the time this file was last modified, measured in milliseconds since January 1st, 1970, midnight.
boolean
setReadOnly()
Marks this file or directory to be read-only as defined by the operating system.
String
toString()
Returns a string containing a concise, human-readable description of this file.
URI
toURI()
Returns a Uniform Resource Identifier for this file.
URL
toURL()
Returns a Uniform Resource Locator for this file.
阅读全文
0 0
- Java的File对象
- Java的file对象的构造方法
- 把一个file对象的内容带上行号复制到另一个file对象(java)
- java中的File对象的renameTo方法的理解
- File对象的路径
- JS的File对象
- File对象的创建
- 151205学习记录 java学习基础 File对象的使用
- java file对象获取文件大小
- Java IO流 File对象
- Java file对象 删除目录
- File对象的简单操作
- 50.File对象的使用
- File对象的常用方法
- JavaScript File对象的初始化
- Java面向对象——File类
- Java面向对象——File类
- java中将file文件对象转string
- 最小生成树
- 多“维”优化——前端高并发策略的更深层思考
- php小工具类php下将XML转换为数组
- 写了一个计时器
- SpringBoot常用注解
- Java的File对象
- (转)王国斌:牛市还是熊市应该在意吗?
- mysql 错误 SQL Error: 1366: Incorrect string value: "\xE8\xAF\xA6\xE7\xBB\x86…" for column "address" a
- CF#418 Div2 D. An overnight dance in discotheque
- BZOJ 2456: mode(巧取众数)
- RTOS里的消息机制
- dbus-glib 详解
- Linux下查看系统版本信息
- RTOS里的任务切换