zoukankan      html  css  js  c++  java
  • java下文件遍历,与删除

    package cn.stat.p1.file;
    
    import java.io.File;
    
    public class newfilelist {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            File f1=new File("E:\BaiduYunDownload");
            nlist(f1,0);
    
        }
        public static void nlist(File f1,int len)
        {
            File[] name=f1.listFiles();
            for(int x = 0;x<name.length;x++)
            {
                if(name[x].isDirectory())
                {
                    System.out.print("|-");
                    for(int y=0;y<len;y++)
                    {
                        System.out.print("-");
                    }
                    System.out.println(name[x].getAbsolutePath());
                    
                    nlist(name[x],len+1);
                }else
                {
                    System.out.print("|-");
                    for(int y=0;y<len;y++)
                    {
                        System.out.print("-");
                    }
                    System.out.println(name[x].getAbsolutePath());
                }
            }
        }
    
    }

    删除

    package cn.stat.p1.file;
    
    import java.io.File;
    
    public class filedel {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            File f1=new File("E:\s2");
            delfile(f1,0);
        }
    
        private static void delfile(File f1, int len) {
            // TODO Auto-generated method stub
            File[] filename=f1.listFiles();
            for(int x=0;x<filename.length;x++)
            {
                if(filename[x].isDirectory())
                {
                    delfile(filename[x], len+1);
                    System.out.println(filename[x]+"删除:"+filename[x].delete());
                }
                else
                {
                    System.out.println(filename[x]+"删除:"+filename[x].delete());
                }
            }
        }
    
    }
  • 相关阅读:
    反爬的几种手段总结
    算法基础篇一
    python总结九
    python总结八
    python总结七
    python总结六
    初识Machine学习
    python总结五
    python总结四
    python总结三
  • 原文地址:https://www.cnblogs.com/zywf/p/4776902.html
Copyright © 2011-2022 走看看