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

Java学习笔记之 File构造方法

2015-05-07 21:05 435 查看
未做任何操作的电脑 E:\Test,如下图



开始

//      File(String pathname)  '\\'为了转义'\'
// 通过抽象路径pathname 创建一个新的文件或者目录
File parent = new File("E:\\Test\\aa");
//      exists 判断文件或目录是否存在;存在为true
if(!parent.exists()){
//          mkdirs 创建多级目录
parent.mkdirs();
}


结果:



//      File(String pathname)
File parent = new File("aaa");
//      exists 判断文件或目录是否存在;存在为true
if(!parent.exists()){
//          mkdirs 创建多级目录
parent.mkdirs();
}


前后对比:









“\aaa”或者“\aaa” 可以自己试一下。

File parent = new File("E:\\Test\\aaa");
if(!parent.exists()){
parent.mkdirs();
}

//      File(File parent, String child)
// 和上面的类似,通过 parent和child 创建一个文件或者目录
File child = new File(parent,"child");
if(!child.exists()){
child.mkdirs();
}


结果 看一下三张图







特别看一下 中下位置的两幅图, aaa 和 child 文件夹的创建时间。

//      File(String parent, String child) 和 File(File parent, String child) 基本一样
File child = new File("E:\\Test\\A","child");
if(!child.exists()){
child.mkdirs();
}


结果





感谢 /article/9071565.html

try {
Intent intent = getIntent();
Uri uri = intent.getData();   // 比如此时的uri为   file:///mnt/sdcard/external_sd/test.txt
File file = new File(new URI(uri.toString()));
Log.i(TAG, file.getAbsolutePath());
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: