- A:File类的概述
- File更应该叫做一个路径
- 文件路径或者文件夹路径
- 路径分为绝对路径和相对路径
- 绝对路径是一个固定的路径,从盘符开始
- 相对路径相对于某个位置,在eclipse下是指当前项目下,在dos下
- 查看API指的是当前路径
- 文件和目录路径名的抽象表示形式
- File更应该叫做一个路径
- B:构造方法
- File(String pathname):根据一个路径得到File对象
- File(String parent, String child):根据一个目录和一个子文件/目录得到File对象
- File(File parent, String child):根据一个父File对象和一个子文件/目录得到File对象
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
logE("未挂载");
String path = Environment.getExternalStorageDirectory().getPath()+"/DCIM/Camera";
File file1 = new File(path);
logE(file1.exists()+"");
logE(file1.getAbsolutePath()+"");
logE(file1.isFile()+"");
logE(file1.isDirectory()+"");
logE("----------------------------------------");
path+="/20180823_123209.jpg";
File file2 = new File(path);
logE(file2.exists()+"");
logE(file2.getAbsolutePath()+"");
logE(file2.isFile()+"");
logE(file2.isDirectory()+"");
File file3 = new File(Environment.getExternalStorageDirectory(),"/DCIM/Camera/20180823_123209.jpg");
logE(file3.exists()+"");
logE(file3.getAbsolutePath()+"");
logE(file3.isFile()+"");
logE(file3.isDirectory()+"");
}else {
logE("挂载");
}
08-23 12:48:49.441 23362-23362/com.chinamall21.mobile.javastudy E/study:: 未挂载
08-23 12:48:49.441 23362-23362/com.chinamall21.mobile.javastudy E/study:: true
08-23 12:48:49.441 23362-23362/com.chinamall21.mobile.javastudy E/study:: /storage/sdcard0/DCIM/Camera
08-23 12:48:49.441 23362-23362/com.chinamall21.mobile.javastudy E/study:: false
08-23 12:48:49.441 23362-23362/com.chinamall21.mobile.javastudy E/study:: true
08-23 12:48:49.441 23362-23362/com.chinamall21.mobile.javastudy E/study:: ----------------------------------------
08-23 12:48:49.446 23362-23362/com.chinamall21.mobile.javastudy E/study:: true
08-23 12:48:49.446 23362-23362/com.chinamall21.mobile.javastudy E/study:: /storage/sdcard0/DCIM/Camera/20180823_123209.jpg
08-23 12:48:49.446 23362-23362/com.chinamall21.mobile.javastudy E/study:: true
08-23 12:48:49.446 23362-23362/com.chinamall21.mobile.javastudy E/study:: false
08-23 12:48:49.446 23362-23362/com.chinamall21.mobile.javastudy E/study:: true
08-23 12:48:49.446 23362-23362/com.chinamall21.mobile.javastudy E/study:: /storage/sdcard0/DCIM/Camera/20180823_123209.jpg
08-23 12:48:49.446 23362-23362/com.chinamall21.mobile.javastudy E/study:: true
08-23 12:48:49.446 23362-23362/com.chinamall21.mobile.javastudy E/study:: false
- A:创建功能
- public boolean createNewFile():创建文件 如果存在这样的文件,就不创建了
- public boolean mkdir():创建文件夹 如果存在这样的文件夹,就不创建了
- public boolean mkdirs():创建文件夹,如果父文件夹不存在,会帮你创建出来
建立的是一个文件:
File file = new File(Environment.getExternalStorageDirectory(),"test.txt");
logE(file.exists()+"");
try {
logE(file.createNewFile());
logE(file.exists());
} catch (IOException e) {
logE("catch");
e.printStackTrace();
}
建立的是一个文件夹:
File file = new File(Environment.getExternalStorageDirectory(),"aaa");
file.mkdir();
建立的是多级文件夹
File file = new File(Environment.getExternalStorageDirectory(),"aaa/ddd");
file.mkdirs();
网友评论