File类:
1 File 构造函数摘要
//可以把已存在,或者不存在的文件或者目录封装成file对象。
File f1 = new File("c:\\a.txt");
File f2 = new File("c:\\","a.txt");
File f = new File("c:\\");
File f3 = new File(f,"a.txt");
File f4 = new File("c:"+File.separator+"abc");//File.separator 路径分隔符 相当于System.getProperty("file.separator");
2 常见功能
获取
2.1 获取文件名称
File file = new File("a.txt");
String name = file.getName();
2.2 获取文件路径
String abspathString = file.getAbsolutePath();//获取绝对路径(磁盘开头的)
String path = file.getPath();//获取相对路径
2.3 获取文件大小
long len = file.length();
2.4 获取文件次最后一次修改时间
long time = file.lastModified();
)
删除
boolean b1 = file.delete();(在被流操作时返回的是false)
创建文件(无,则创建,有,则不创建)
boolean b = file.createNewFile();
创建文件夹
File file1 = new File("abc");
boolean a = file1.mkdir();
创建多层目录的文件夹
File file2 = new File("abc//a//c//b");
boolean a1 = file2.mkdirs();
文件夹无法直接删除:windows 都是从里向外删除的
判断
判断是否存在
boolean b = file.exists();
判断是否为文件和目录(先得判断是否存在,不存在的话全部返回为false)
boolean b1 = file.isFile();
boolean b2 = file.isDirectory();
重命名
同一个磁盘下重命名,不同磁盘剪切
File f = new File("c:\\0.txt");
File f2 = new File("c:\\9.txt");
f.renameTo(f2);
获取磁盘
File[] files = File.listRoots();
for(File file2:files){
System.out.println(file2);// 输出 C:\D:\E:\F:\
}
获取磁盘可用,总共,已用空间
File file = new File("d:\\");
System.out.println(file.getFreeSpace());
System.out.println(file.getTotalSpace());
System.out.println(file.getUsableSpace());
获取目录的文件以及文件夹名称(包含隐藏文件)
File file = new File("d:\\");
String[] names = file.list();
for(String name:names){
System.out.println(name);
}
调用list方法的File对象中封装的必须是目录,否则会发生NullPointerException
如果目录存在但是没有内容,会返回一个数组,但是长度为0
过滤器
File file = new File("d:\\");
String[] names = file.list(new FilterByJava());
for(String name:names){
System.out.println(name);
}
//建造过滤器
public class FilterByJava implements FilenameFilter {
public boolean accept(File dir, String name) {
return name.endsWith(".java");
}
}
过滤隐藏名字的文件夹
File dir = new File("d:\\");
File[] files = dir.listFiles(new FilterByHidden());
for(File file1:files){
System.out.println(file1);
}
public class FilterByHidden implements FileFilter {
public boolean accept(File arg) {
return arg.isHidden();
}
}
指定后缀名的过滤器:
public class SuffixFilter implements FilenameFilter {
private String suffix;
public SuffixFilter(String suffix) {
super();
this.suffix = suffix;
}
public boolean accept(File arg0, String arg1) {
return arg1.endsWith(suffix);
}
}
对指定目录进行所有内容的列出
深度遍历
public class FileTest {
public static void main(String[] args){
File dir = new File("c:\\");
ListAll(dir,0);
}
public static void ListAll(File dir,int level) {
System.out.println(getSpace(level)+dir.getAbsolutePath());
level++;
File[] files = dir.listFiles();
for(int x = 0;x<dir.length();x++){
if(dir.isDirectory()){//如果是目录就进行递归调用
ListAll(files[x],level);
}
else
System.out.println(getSpace(level)+files[x].getAbsolutePath());
}
}
//层级显示
private static String getSpace(int level) {
StringBuilder sb = new StringBuilder();
for(int x = 0;x<level;x++){
sb.append(" ");
}
return sb.toString();
}
}
/*递归:
* 函数自身直接调用或间接调用自己
* 一个功能被重复使用,并每次使用时,参与运算的结果和上一次调用有关,
* 这时可以用递归来解决
* 注意:
* 1 递归一定要明确条件
* 2 注意一下递归的次数
* */
练习:
删除一个目录 (深度遍历)
public class RemoveDirTest {
public static void main(String[] args)
{
File dir = new File("abc");
removeAll(dir);
}
public static void removeAll(File dir) {
File[] files = dir.listFiles();
for(File file:files){
if(file.isDirectory()){
removeAll(file);
}
else{
file.delete();
}
}
dir.delete();
}
}
Properties集合:
* 特点:
* 1 该集合中的键和值都是字符串类型
* 2 集合中的数据可以保存到流中,或者从流获取
*
* 通常该集合用于操作以键值对形式存在的配置文件
1 存和取
Properties prop = new Properties();
prop.setProperty("zhangsan", "30");
prop.setProperty("lisi", "29");
Set<String> names = prop.stringPropertyNames();
for(String name:names){
String value = prop.getProperty(name);
System.out.println(name+value);
}
与流对象的结合:
prop.list(System.out);
持久性保存(其实就是写入到硬盘)
void | store(OutputStream out,String comments) 以适合使用 load(InputStream) 方法加载到Properties 表中的格式,将此Properties 表中的属性列表(键和元素对)写入输出流。 |
例:
Properties prop = new Properties();
prop.setProperty("zhangsan", "30");
prop.setProperty("lisi", "29");
FileOutputStream fos = new FileOutputStream("ins.txt");
prop.store(fos, "ins");
读取一个文件到集合中:
集合中的数据来自于一个文件
注意必须保证该文件中的数据是键值对
/需要用到读取流
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("ins.txt");
prop.load(fis);
prop.list(System.out);
应用:
对已有的配置文件中的信息进行修改
读取这个文件
并将这个文件中的键值数据存储到集合中
在通过集合对数据进行修改
再通过流将修改后的数据存储到文件中
//读取文件
File file = new File("ins.txt");
if(!file.exists()){
file.createNewFile();
}
FileReader fr = new FileReader(file);
//创建集合存储配置信息
Properties prop = new Properties();
//存储到集合中
prop.load(fr);
//修改
prop.setProperty("wangwu", "16");
//存储到文件中
FileWriter fw = new FileWriter(file);
prop.store(fw, "");
练习:程序运行5次 不可以继续运行
* 思路:
* 读取一个配置文件,读取到文件中,文件中记录一个变量,记录获取的次数
* 操作这个变量,运行一次就自增一次,自增完后再存储到集合中。然后再把集合中数据写入到文件中。
(伪代码)
//1 读取一个文件
File file = new File("count.properties");
if(!file.exists()){
file.createNewFile();
}
FileInputStream fis = new FileInputStream(file);
//存储到集合中
Properties prop = new Properties();
prop.load(fis);
//获取次数
String value = prop.getProperty("time");
//定义计数器,记录获取到的次数
int count = 0;
if(value!=null){
count = Integer.parseInt(value);
if(count>5){
throw new RuntimeException("程序已经运行5次!");
}
}
count++;
//将改变后的值重新存储到集合中
prop.setProperty("time", “count++”);
FileOutputStream fos = new FileOutputStream(file);
prop.store(fos, "");
综合练习
获取指定目录下,指定扩展名的文件(包含子目录中的)
这些文件的绝对路径写入到一个文本文件中。
简单说,就是建立一个指定扩展名的文件的列表
思路:
1 必须进行深度遍历
2 要在遍历的过程中进行过滤,将符合条件的内容都存储到容器中。
3 对容器中的内容进行遍历并将绝对路径写入到文件中
public class SynthesizeTest {
public static void main(String[] args) {
File file = new File("e:\\javaDev");
FilenameFilter filter = new FilenameFilter(){
public boolean accept(File arg0, String arg1) {
return arg1.endsWith(".java");
}
};
List<File> list = new ArrayList<File>();
getFiles(file,filter,list);
File file1 = new File("abs.txt");
writeFile(list,file1);
}
public static void getFiles(File dir,FilenameFilter filter,List<File> list){
//1 深度遍历
File[] files = dir.listFiles();
for(File file:files){
if(file.isDirectory()){
//递归
getFiles(file,filter,list);
}
else{
//过滤
if(filter.accept(file, file.getName())){
list.add(file);
}
}
}
}
public static void writeFile(List<File> list,File desFile){
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(desFile));
for(File file:list){
bw.write(file.getAbsolutePath());
bw.newLine();
bw.flush();
}
} catch (IOException e) {
throw new RuntimeException("失败");
}
finally{
try {
bw.close();
} catch (IOException e) {
throw new RuntimeException("关闭失败");
}
}
}
}