file java详解_File 类详解 - Java知其所以然的个人空间 - OSCHINA - 中文开源技术交流社区...

File 类入门

File 类是什么

jdk文档中的解释

文件和目录路径名的抽象表示形式。

我的理解

File 其实可以理解为一个桥梁,它建立起了 java 程序和操作系统(windows或linux等)之间的一个关系,方便我们通过面向对象的思维操作操作系统上的文件。

两个常量

1、路径分隔符 ;

2、名称分隔符 / (windows)     \ (linux 等)

代码如下:

public class Demo01 {

/**

* @param args

*/

public static void main(String[] args){

System.out.println(File.pathSeparator);

System.out.println(File.separator);

//路径表示形式

String path ="E:\\xp\\test\\2.jpg";

path="E:"+File.separator+"xp"+File.separator+"test"+File.separator+"2.jpg";

//推荐方式

path="E:/xp/test/2.jpg";

}

}

File 类进阶

相对路径与绝对路径构造 File 对象

1、相对路径

File(String parent, String child) ==>File("E:/xp/test","2.jpg")

File(File parent, String child) ==> File(new File("E:/xp/test"),"2.jpg")

2、绝对路径

File(String name);

代码如下:

public class Demo02 {

/**

* @param args

*/

public static void main(String[] args){

String parentPath ="E:/xp/test";

String name ="2.jpg";

//相对路径

File src =new File(parentPath,name);

src =new File(new File(parentPath),name);

//输出

System.out.println(src.getName());

System.out.println(src.getPath());

//绝对路径

src =new File("E:/xp/test/2.jpg");

System.out.println(src.getName());

System.out.println(src.getPath());

//没有盘符: 以 user.dir 构建(也就是项目的根路径,大家动手运行一下就知道了)

src =new File("test.txt");

System.out.println(src.getName());

System.out.println(src.getPath());

System.out.println(src.getAbsolutePath());

}

}

File 类常用方法

1、文件名

getName() 文件名、路径名

getPath() 路径名

getAbsoluteFile() 绝对路径所对应的 File 对象

getAbsolutePath() 绝对路径名

getParent() 父目录 ,相对路径的父目录,可能为 null

2、判断信息

exists()

canWrite()

canRead()

isFile()

isDirectory()

3、长度(字节数)

不能读取文件夹的长度

length()

4、创建、删除

createNewFile() 不存在创建新文件,存在返回 false

delete() 删除文件

static createTempFile(前缀3个字节长,后缀默认.temp) 默认临时空间

deleteOnExit() 退出虚拟机删除,常用于删除临时文件

代码如下:

public class Demo03 {

/**

* @param args

*/

public static void main(String[] args){

test2();

try {

test3();

} catch (IOException e) {

e.printStackTrace();

System.out.println("文件操作失败");

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

//创建、删除文件

public static void test3() throws IOException, InterruptedException{

//createNewFile() 不存在时创建新文件

//String path="E:/xp/test/con"; // con 系统关键字

String path="E:/xp/test/200.jpg";

//String path="E:/xp/test/1.jpg";

File src =new File(path);

if(!src.exists()){

boolean flag =src.createNewFile();

System.out.println(flag?"成功":"失败");

}

//删除文件

boolean flag =src.delete();

System.out.println(flag?"成功":"失败");

//static createTempFile(前缀3个字节长,后缀默认.temp) 默认临时空间

//static createTempFile(前缀3个字节长,后缀默认.temp,目录)

File temp= File.createTempFile("tes", ".temp",new File("e:/xp/test"));

Thread.sleep(10000);

//退出即删除

temp.deleteOnExit();

}

//2、判断信息

//3、长度 length()

public static void test2(){

//String path ="2.txt";

String path="E:/xp/test/200.jpg";

//String path="E:/xp/test/200.jpg";

File src =new File(path);

//是否存在

System.out.println("文件是否存在:"+src.exists());

//是否可读 写 canWrite() canRead()

System.out.println("文件是否可写"+src.canWrite());

System.out.println("============");

//isFile()

//isDirectory()

if(src.isFile()){

System.out.println("文件");

}else if(src.isDirectory()){

System.out.println("文件夹");

}else{

System.out.println("文件不存在");

}

System.out.println("是否为绝对路径"+src.isAbsolute());

System.out.println("长度为:"+src.length());

}

//1、名称

public static void test1(){

//File src =new File("E:/xp/test/2.jpg");

//建立联系

File src =new File("2.txt");

//返回名称

System.out.println(src.getName());

//如果是绝对路径,返回完整路径,否则返回相对路径

System.out.println(src.getPath());

//返回绝对路径

System.out.println(src.getAbsolutePath());

//返回上一级目录,如果是相对路径,可能会返回 null

System.out.println(src.getParent());

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值