java-IO流(中)
2015-06-04 20:33
453 查看
java-IO流(中)
在IO流(上)中不知道大家注意到没有就是我们操作的数据,那么当我们要操作文件夹时怎么办呢?之前我们虽然也操作过文件但是如果现在我们要操作文件的属性该怎么办呢?在java的io包下面有一个很重要的类就是File类。一、File类
File常用的构造函数就是三个可以看如下的代码<span style="font-size:14px;"><span style="font-size:14px;"><span style="font-size:14px;">public class HeiMaTest { public static void main(String[] args) throws Exception{ //通过给定的路径名字创造一个File对象实例 File f1 = new File("Heima.txt"); //通过parent路径名字符串和child路径字符串来创建File对象 File f2 = new File("c:\\","HeiMa.txt"); //通过parent抽象路径名和child路径字符串来创建File对象,注意第一个参数File对象 File f = new File("c:\\"); File f3 = new File(f,"HeiMa.txt"); } }</span></span></span>File类里面还有几个自带的比较重要的字段就是separator和pathSeparator在windows中分别表示的是//和;
下面我们来体验一下File类中的方法
(1)首先我们来看获取文件名,获取文件的路径,获取文件的大小,获取文件的最后的修改时间的方法
<span style="font-size:14px;"><span style="font-size:14px;"><span style="font-size:14px;">public class HeiMaTest { public static void main(String[] args) throws Exception{ File file = new File("D:\\HeiMa.txt"); //获取文件名称 String fileName= file.getName(); //获取文件的路径(绝对路径) String filePath = file.getAbsolutePath(); //获取文件的大小 long fileSize = file.length(); //获取文件的最后的修改时间 long lastModify = file.lastModified(); Date date = new Date(lastModify); DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); String str_date = dateFormat.format(date); System.out.println("文件名"+fileName+"\n文件绝对路径"+filePath+"\n文件的大小"+fileSize+"\n文件最后的修改日期"+str_date); } }</span></span></span>
运行结果截图
(2)文件的创建和删除的操作
需要注意的是创建文件和文件的输出流不同之处在于创建文件是在文件不存在的时候创建在文件存在的时候就不创建,而输出流是不管文件存不存在都会操作这个文件。
<span style="font-size:14px;"><span style="font-size:14px;"><span style="font-size:14px;">public class HeiMaTest { public static void main(String[] args) throws Exception{ //现在在d盘下面有一个文件夹我们来创建它 File file = new File("d:\\HeiMa.txt"); boolean isSuccess = file.createNewFile(); System.out.println(isSuccess); //创建一级目录 File dir = new File("D:\\HeiMa"); boolean isCreateSuccess = dir.mkdir(); System.out.println(isCreateSuccess); File dirs = new File("D:\\中关村\\黑马程序员训练营\\android"); boolean isDirsSuccess = dirs.mkdirs(); System.out.println(isDirsSuccess); //这句执行后只会删除掉android目录,如果只有一级目录例如上面的dir调用删除如果dir下面有文件则不会删除 dirs.delete(); } }</span></span></span>
这里还有一点就是在一级目录下面有一个文件那么调用删除方法的时候不会删除掉
<span style="font-size:14px;"><span style="font-size:14px;"><span style="font-size:14px;"><span style="font-size:14px;">public class HeiMaTest { public static void main(String[] args) throws Exception{ File dir = new File("D:\\HeiMa"); boolean isDirCreateSuccess = dir.mkdir(); System.out.println("目录是否创建成功"+isDirCreateSuccess); File f = new File(dir,"HeiMa.txt"); boolean isFileCreateSuccess = f.createNewFile(); System.out.println("文件是否创建成功"+isFileCreateSuccess); boolean isDeleteSuccess = dir.delete(); System.out.println("目录是否删除成功"+isDeleteSuccess); } }</span></span></span></span>运行截图
删除文件的顺序是从内往外删除的,如果目录下面有文件的话就不能直接删除目录,正如上面的多级目录调用了删除方法后只会删除android文件夹。
(3)File的判断方法
在File类中有这么几个判断的方法就是exists(),isFile(),isDirectory(),这三个方法分别是判断文件是不是存在,判断是不是文件,判断是不是目录
(4)文件操作中重命名也是挺常见的下面我们用代码来对文件进行重命名
<span style="font-size:14px;"><span style="font-size:14px;"><span style="font-size:14px;"><span style="font-size:14px;">public class HeiMaTest { public static void main(String[] args) throws Exception{ File original_file = new File("D:\\HeiMa.txt"); File target_file = new File("D:\\黑马.txt"); boolean isRenamwSuccess = original_file.renameTo(target_file); System.out.println(isRenamwSuccess); } }</span></span></span></span>(5)系统根目录和容量的获取
首先我们演示如何获取系统的根目录
<span style="font-size:14px;"><span style="font-size:14px;"><span style="font-size:14px;"><span style="font-size:14px;">public class HeiMaTest { public static void main(String[] args) throws Exception{ File[] files = File.listRoots(); for(File f:files){ System.out.println(f); } } }</span></span></span></span>
运行效果截图
接下来我们以d盘来通过代码来获取其容量
<span style="font-size:14px;"><span style="font-size:14px;"><span style="font-size:14px;"><span style="font-size:14px;">public class HeiMaTest { public static void main(String[] args) throws Exception{ File file = new File("d:\\"); System.out.println("d盘的剩余空间是 "+String.format("%.2f", file.getFreeSpace()/1024/1024/1024f)+"GB"); System.out.println("d盘的空间总计是 "+String.format("%.2f", file.getTotalSpace()/1024/1024/1024f)+"GB"); System.out.println("d盘用于虚拟机空间 "+String.format("%.2f", file.getUsableSpace()/1024/1024/1024f)+"GB"); } }</span></span></span></span>
运行效果截图
(6)获取目录内容
<span style="font-size:14px;"><span style="font-size:14px;"><span style="font-size:14px;">public class HeiMaTest { public static void main(String[] args) throws Exception{ File f = new File("C:\\"); String[] files = f.list(); for(String file:files){ System.out.println(file); } } }</span></span></span>上面的方法会将C盘下面的所有的文件名给打印出来包括隐藏文件,需要注意的就是调用list方法的文件对象必须是目录
,否则会发生空指针错误。
(7)文件中的过滤器
想象这样一个需求就是要获取c盘下面的隐藏文件,或者是获取C盘下面的java文件,txt文件,这时就需要使用File里面的文件过滤器了上面的list方法只是获取目录下面的文件名,还有这样的一个方法就是list(FileNameFileter filter)是上面list方法的重载形式可以指定一个过滤器,还有一个方法就是listFiles()这里获取目录下面的文件对象与之相呼应的方法就是listFiles(FileFileter filter)注意过滤器的名称已经发生了变化了是FileFilter了
下面我们来演示在C盘获取c盘下面的java文件
<span style="font-size:14px;"><span style="font-size:14px;"><span style="font-size:14px;">public class HeiMaTest { public static void main(String[] args) throws Exception{ File f = new File("C:\\"); String[] files = f.list(new MySuffixFilter(".java")); for(String name:files){ System.out.println(name); } } private static class MySuffixFilter implements FilenameFilter{ private String suffix; public MySuffixFilter(String suffix){ this.suffix = suffix; } @Override public boolean accept(File dir, String name) { return name.endsWith(suffix); } } }</span></span></span>运行效果图
下面我们来获取C盘下面的隐藏文件,这里就用到了listFiles(FileFileter filter)
<span style="font-size:14px;"><span style="font-size:14px;"><span style="font-size:14px;">public class HeiMaTest { public static void main(String[] args) throws Exception{ File f = new File("C:\\"); File[] files = f.listFiles(new MyHiddenFilter()); for(File name:files){ System.out.println(name); } } private static class MyHiddenFilter implements FileFilter{ @Override public boolean accept(File pathname) { return pathname.isHidden(); } } }</span></span></span>运行效果截图
(8)深度遍历文件夹
现在在D盘的HeiMa文件夹下面包含了许多子文件夹,子文件夹里面又包含了许多子文件我们现在来遍历这个文件夹,首先我们分析一下我们从最外面的文件夹进入的话就遍历的时候就会判断文件是文件还是目录如果是目录的话就应该继续遍历,这里就是哦用到递归,递归就是方法自己调用自己,不了解的可以百度学习下这里不做过多的介绍了。
<span style="font-size:14px;"><span style="font-size:14px;"><span style="font-size:14px;">public class HeiMaTest { public static void main(String[] args) throws Exception{ File f = new File("D:\\HeiMa"); listAllFile(f,0); } private static void listAllFile(File dir,int level) { System.out.println(getSpace(level)+dir.getName()); File[] files = dir.listFiles(); level++; for(File file:files){ if (file.isDirectory()) { listAllFile(file,level); }else{ System.out.println(getSpace(level)+file.getName()); } } } private static String getSpace(int level){ StringBuilder sb = new StringBuilder(); for(int i = 0;i<level;i++){ sb.append(" "); } return sb.toString(); } }</span></span></span>运行效果截图
(9) 深度遍历文件删除
在上面的目录部分已经提到过就是当一级目录中包含文件时不能直接删除该目录,在windows下面删除文件时从内往外删除的不能直接删除最外面的。
我们将上面遍历过的文件目录结构用遍历的方法进行删除
<span style="font-size:14px;"><span style="font-size:14px;"><span style="font-size:14px;">public class HeiMaTest { public static void main(String[] args) throws Exception{ File f = new File("D:\\HeiMa"); deleteFile(f); } private static void deleteFile(File dir){ File[] files = dir.listFiles(); for(File child:files){ //如果是目录的继续深入遍历 if (child.isDirectory()) { deleteFile(child); }else{ System.out.println("文件:"+child.getName()+"是否删除成功--》"+child.delete()); } } //将目录中的文件删除干净就该删除目录了 System.out.println("目录:"+dir.getName()+"是否删除成功--》"+dir.delete()); } }</span></span></span>运行效果截图
二、与IO流相关联的一个集合中的类Properties类
(1)Properties集合的特点1.该集合中的键和值都是字符串类型。
2.集合中的数据可以保存在流中也可以从流中进行加载
通常该集合用来操作以键值对形式存放的配置文件信息
(2)我们用Properties进行数据存和取
<span style="font-size:14px;"><span style="font-size:14px;"><span style="font-size:14px;">public class HeiMaTest { public static void main(String[] args) throws Exception{ Properties prop = new Properties(); prop.setProperty("IT", "黑马程序员训练营"); prop.setProperty("中关村", "软件培训"); prop.setProperty("传智播客", "IT"); Set<String> sets = prop.stringPropertyNames(); for (String name:sets) { String value = prop.getProperty(name); System.out.println("键="+name+"值="+value); } } }</span></span></span>
运行效果截图
如果要对其进行修改的可以使用setProperties(String key,String value)注意键相同值覆盖
(3)将Properties存入到本地的文件中我们还是以上面构建的properties来进行操作演示
<span style="font-size:14px;"><span style="font-size:14px;"><span style="font-size:14px;">public class HeiMaTest { public static void main(String[] args) throws Exception{ Properties prop = new Properties(); prop.setProperty("IT", "黑马程序员训练营"); prop.setProperty("中关村", "软件培训"); prop.setProperty("传智播客", "IT"); FileWriter fos = new FileWriter("D:\\HeiMaInfo.txt"); //需要注意的是第二个参数会在生成的配置文件中注释中出现 prop.store(fos, "HeiMa");</span></span></span>
<span style="font-size:14px;"><span style="font-size:14px;"><span style="font-size:14px;"> fos.close(); } }</span></span></span></span>(4)如何对已经存在的配置文件进行修改,比如我们想将上面的info文件中键为中关村的对应的值修改为北京
<span style="font-size:14px;"><span style="font-size:14px;"><span style="font-size:14px;"><span style="font-size:14px;">public class HeiMaTest { public static void main(String[] args) throws Exception{ File file = new File("D:\\HeiMaInfo.txt"); FileReader fis = new FileReader(file); Properties prop = new Properties(); prop.load(fis); prop.setProperty("中关村", "北京"); FileWriter fw = new FileWriter(file); prop.store(fw, ""); fw.close(); fis.close(); } }</span></span></span></span>因为配置文件中使用了较多的中文所以的话就采用了字符流读取和字符流写入
(5)现在有这么一个需求就是获取指定目录下,指定扩展名的文件(包含子目录中的),并且将这些文件的绝对路径写入到一个文本文件中我们还是以上面深度遍历的文件夹为例
思路就是先对文件夹进行深度的遍历,使用过滤器对其遍历到的文件进行过滤,对过滤后符合条件的文件获取其路径名存入一个集合中,将集合数据写入到一个文件中
<span style="font-size:14px;"><span style="font-size:14px;"><span style="font-size:14px;">public class HeiMaTest { public static void main(String[] args) throws Exception{ File file = new File("D:\\HeiMa"); MySuffixFilter filter = new MySuffixFilter(".txt"); ArrayList<File> lists = new ArrayList<File>(); getAllFile(file,filter,lists); writerToFile(lists); } private static void writerToFile(ArrayList<File> lists) throws IOException { BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\count.txt")); for(File f:lists){ bw.write(f.getAbsolutePath()); bw.newLine(); } bw.close(); } private static void getAllFile(File dir,FilenameFilter filter,ArrayList<File> lists) { File[] files = dir.listFiles(); for(File file:files){ if (file.isDirectory()) { getAllFile(file, filter,lists); }else{ if (filter.accept(dir, file.getName())) { lists.add(file); } } } } private static class MySuffixFilter implements FilenameFilter{ private String suffix; public MySuffixFilter(String suffix) { this.suffix = suffix; } @Override public boolean accept(File dir, String name) { // TODO Auto-generated method stub return name.endsWith(suffix); } } }</span></span></span>运行截图
相关文章推荐
- Java语言描述:回溯法之01背包问题(最易理解的回溯法讲解)
- Java 内存区域和GC机制
- Android项目中使用Eclipse导出jar文件
- Java的GC机制及算法
- Ubuntu下eclipse部署mysql jdbc驱动
- Java实验三
- 严重Exception starting filter struts2的解决
- java第三次试验报告
- myeclipse连接mysql遇到的小问题
- Java数据库之JDBC
- Eclipse安装SVN插件(转载)
- JAVA RMI 原理与实现
- struts2文件异步上传带进度条(二)
- Java文本编辑器中遇到的问题详解
- 我的第一次Spring学习
- java中serializable解析及用途
- 20135208 JAVA第三次实验
- [转载]JAVA中this用法小结
- spring_aop
- Java中继承thread类与实现Runnable接口的区别