File §2

本文介绍了如何使用Java进行文件和目录的操作,包括创建多级目录、判断文件是否存在以及创建文件等。详细对比了mkdir()和mkdirs()的区别,并通过实例演示了两者的用法。

Previously speaking,File can be seen as one ducument, also can be seen as list of documents like directory.

Here,File also can be seen as one directory which does't exist, also can be seen as  a whole path of directory which does't exist.

 

Example1:

File f = new File("C:/Users/caich5/Desktop/test");

if(!f.exist()){

f.mkdirs();

}

 If succeed,Directory 'test' will show in the path of 'C:/Users/caich5/Desktop';

 

File f = new File("C:/Users/caich5/Desktop/test/test/test");

if(!f.exist()){

f.mkdirs();

}

If succeed,Directory 'test/test/test' will show in the path of 'C:/Users/caich5/Desktop';

 

Tips:difference between file.mkdirs() and file.mkdir();

file.mkdirs(): it means that you can create multi-layer path;

file.mkdir(): it means that you can only create the single-layer path;

 

Example2:

File f = new File("C:/Users/caich5/Desktop/test.txt");

if(!f.exist()){

   f.createNewFile();

}

 

转载于:https://www.cnblogs.com/pickKnow/p/8508656.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值