黑马程序员技术交流社区

标题: 第20天java基础学习。一个关于IO的有意思的简单小程序 [打印本页]

作者: ye646682485    时间: 2015-4-24 15:34
标题: 第20天java基础学习。一个关于IO的有意思的简单小程序
:lol把自己电脑里的教学视频名称全部汇总了


  1. /*
  2. 将文件夹中的文件名称存入txt文件中
  3. */
  4. import java.util.*;
  5. import java.io.*;
  6. class MovFileTest
  7. {
  8.         public static void main(String[] args)
  9.         {
  10.                 File dir = new File("D:\\Program Files (x86)\\教学视屏");
  11.                 List<File> list = new ArrayList<File>();
  12.                 toArrayList(dir,list);
  13.                 File file = new File(dir,"教学视频汇总.txt");
  14.                 toText(list,file);
  15.         }
  16.         public static void toArrayList(File dir,List<File> list)
  17.         {
  18.                 File[] file = dir.listFiles();
  19.                 for(File f:file)
  20.                 {
  21.                         if(f.isDirectory())
  22.                                 toArrayList(f,list);
  23.                         else
  24.                         {
  25.                                 String name = f.getName();
  26.                                 if(name.endsWith(".avi")||name.endsWith(".mp4")||name.endsWith(".rmvb")||name.endsWith(".mkv")||name.endsWith(".flv")||name.endsWith(".wmv"))
  27.                                         list.add(f);
  28.                         }
  29.                 }
  30.         }
  31.         public static void toText(List<File> list,File dir)
  32.         {
  33.                 BufferedWriter bw =null;
  34.                 int count=list.size();
  35.                 try
  36.                 {
  37.                         bw = new BufferedWriter(new FileWriter(dir));
  38.                         bw.write("共计"+count+"部影片");
  39.                         bw.newLine();
  40.                         for(File f :list)
  41.                         {
  42.                                 String path=f.getAbsolutePath();
  43.                                 //String name=f.getName();
  44.                                 bw.write(path);
  45.                                 bw.newLine();
  46.                                 bw.flush();
  47.                         }
  48.                        
  49.                 }
  50.                 catch (IOException io)
  51.                 {
  52.                         throw new RuntimeException("出错");
  53.                 }
  54.                 finally
  55.                 {
  56.                         try
  57.                         {
  58.                                 if(bw!=null)
  59.                                         bw.close();
  60.                         }
  61.                         catch (IOException io)
  62.                         {
  63.                                 throw new RuntimeException("关闭资源出错!");
  64.                         }
  65.                 }
  66.         }
  67. }
复制代码




作者: wwfangfang    时间: 2015-4-24 15:36
楼主牛……
作者: ye646682485    时间: 2015-4-24 15:42
wwfangfang 发表于 2015-4-24 15:36
楼主牛……

跟毕老师学的
作者: 吕晓阳    时间: 2015-4-24 15:48
写的不错
作者: ye646682485    时间: 2015-4-24 15:49
吕晓阳 发表于 2015-4-24 15:48
写的不错

这是我学java 以来真正感觉到了他的用处了,谢谢了:lol
作者: sunyue    时间: 2015-4-24 16:53
现学现用啊 不错啊
作者: 海阔天空监管科    时间: 2015-4-24 18:58
不错,值得学习
作者: forplay    时间: 2015-4-28 10:47
现学现卖,不错不错
作者: 大西洋    时间: 2015-4-28 10:59
rmvb  mkv 格式的电影会不会也被放进去? :D






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2