您的位置:首页 > 编程语言 > Java开发

java中的File类常用方法

2017-05-11 15:48 405 查看
本文file类示例的方法如下:

renameTo(File dest)

mkdirs()

mkdir()

lastModified()

isHidden()

isFile()

isDirectory()

isAbsolute()

getPath()

getParentFile()

getParent()

getName()

getCanonicalPath()

getAbsolutePath()

exists()

deleteOnExit()

delete()

createTempFile(String prefix, String suffix)

createNewFile()

canWrite()

canRead()

package com.Ckinghan.CollectionDemo;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileDemo0 {

public static void main(String[] args) throws IOException {
//创建一个文件对象
File file = new File(".","Java_IO_File.txt");

//如果文件 已经存在,则删除
if(file.exists()){
file.delete();
}

boolean success = file.exists();

//判断文件是否存在,不存在则创建
if(!success){
success = file.createNewFile();
System.out.println("文件创建前不存在,现已创建");
}else
System.out.println("文件在创建前已存在");

//判断文件是否创建成功
if(!success){
System.out.println("创建文件失败");
}else
System.out.println("创建文件成功");

//获取文件的父级目录
String string = null;
File getFile  = file.getParentFile();
if (!getFile.isDirectory()) {
System.out.println("获取目录失败");
}else
System.out.println("获取目录值为:"+getFile);

//检查文件是否可读
success = file.canRead();
if(!success)
System.out.println("文件不是可读的");
else
System.out.println("文件是可读的");

//检查文件是否可写
success = file.canWrite();
if(success)
System.out.println("文件可写");
else
System.out.println("文件不可写");

//在缓存中创建文件
File file3 = file.createTempFile("java", ".aaa");
System.out.println(file3);

//当JVM虚拟机退出时删除文件
file3.deleteOnExit();

//直接删除文件
success = file3.delete();
if(success)
System.out.println("文件删除成功");
else
System.out.println("文件删除失败");

//判断文件是否存在
success = file.exists();
if(success)
System.out.println("文件存在");
else
System.out.println("文件不存在");

/**
*  file.getAbsolutePath():文件的绝对路径,但注意,如果是使用了相对路径 ,它得出的结果为:
* File file = new File(".","Java_IO_File.txt");
* C:\Workspace\project1\.\Java_IO_File.txt
* 这个是不对的,正确 的是使用file.getCanonicalPath();
*
* file.getCanonicalPath():得出的结果如下:
*  C:\Workspace\project1\Java_IO_File.txt
*
* getPath():得出的结果不增不减,是相对路径
* .\Java_IO_File.txt
*/

//文件的绝对路径 ,但使用相对路径创建文件时,得到的绝对路径可能是错误 的
string = file.getAbsolutePath();
System.out.println("文件的绝对路径为:"+string);

//文件的绝对路径 ,这个是正确的
string = file.getCanonicalPath();
System.out.println("文件的绝对路径 为:"+string);

//获取文件路径 ,创建文件时写的什么样就是什么样
string = file.getPath();
System.out.println("文件的相对路径 为:"+string);

//获取文件名
string = file.getName();
System.out.println("获取的文件名为:"+string);

//获取文件的路径 (如果是相对路径 ,就获取相对路径 名)
string = file.getParent();
System.out.println("获取文件的路径:"+string);

//判断文件是否是绝对路径
success = file.isAbsolute();
System.out.println("判断文件的路径是否是绝对的:"+success);

//判断文件是否是目录
success = file.isDirectory();
System.out.println("判断文件是否是目录:"+success);

//判断是否是文件
success = file.isFile();
System.out.println("判断是否是文件:"+success);

//判断文件是否为隐藏的
success = file.isHidden();
System.out.println("判断文件是否为隐藏的"+success);

//获取文件的最后修改时间
Long long1 = file.lastModified();
Date date = new Date(long1);
SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
string = sFormat.format(date);
System.out.println("获取文件的最后修改时间:"+string);

/**
* 获取文件 的大小(字节 数)
* 换算方式:1TB = 1024*1024GB = 1024*1024*1024KB =1024*1024*1024*1024字节 = 1024*1024*1024*1024*8位
*/
long1 = file.length();
System.out.println("获取文件 的大小(字节 数):"+long1);

//创建一层文件夹
file3 = new File("E:/JAVA_IO");
success = file3.mkdir();
System.out.println("创建一层文件夹:"+file3);

//创建多层文件夹
file3 = new File("E:/JAVA_IO/目录一/目录二/目录三");
success = file3.mkdirs();
System.out.println("创建多层文件夹:"+file3);

//重命名
success = file.renameTo(new File("JAVA_IO_NEW.JAVA"));
System.out.println("重命名是否成功:"+success);

/**
* 其它还有一些方法,有兴趣可以查阅API
*/

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息