zoukankan      html  css  js  c++  java
  • 创建文件和文件夹

    public class CreatFileUtil
    {
     
        //创建文件
        public static boolean createFile(String fileName)
        {
            File file=new File(fileName);
            if(file.exists())
            {
                System.out.println("文件已存在"+fileName+"创建失败");
                return false;
            }
            if(fileName.endsWith(File.separator))  //File.separator=""
            {
                System.out.println("文件不能为目录");
                return false;
            }
            if(!file.getParentFile().exists())
            {
                System.out.println("问价所在的目录不存在,准备创建它");
                if(!file.getParentFile().mkdirs())
                {
                    System.out.println("创建文件所在的目录失败!");
                    return false;
                }
            }
            try
            {
                if(file.createNewFile())
                {
                    System.out.println("创建文件"+file.getAbsolutePath()+"成功");
                    return true;
                }
                else 
                {
                    System.out.println("创建文件"+file.getAbsolutePath()+"失败");
                    return false;
                }
            }
            catch(IOException e)
            {
                System.out.println("创建文件"+file.getAbsolutePath()+"失败");
                e.printStackTrace();
                return false;
            }
     
        }
        //创建目录
        public static boolean creatDir(String dirName)
        {
            File file=new File(dirName);
            if(file.exists())
            {
                System.out.println("创建目录失败"+file.getAbsolutePath()+"目录已经存在");
                return false;
     
            }
     
            if(!dirName.endsWith(File.separator))
                dirName+=File.separator;
            if(file.mkdirs())
            {
     
                System.out.println(file.getAbsolutePath()+"创建成功");
                return true;
            }
            else 
                {
                System.out.println(file.getAbsolutePath()+"创建失败");
                return false; 
                }
     
        }
        public static void main(String []args)
        {
            String string="C:/temp/temp.txt";
            String string2="C:/hhaha";
            createFile(string);
            creatDir(string2);
     
        }
     
     
    }
    梦里不知身是客,一晌贪欢。
  • 相关阅读:
    Python爬虫-05:Ajax加载的动态页面内容
    Python爬虫-04:贴吧爬虫以及GET和POST的区别
    Python-爬虫03:urllib.request模块的使用
    Python Numpy-基础教程
    8皇后算法
    迷宫算法
    归并排序
    查找算法
    排序算法
    设计模式
  • 原文地址:https://www.cnblogs.com/dccmmtop/p/5709969.html
Copyright © 2011-2022 走看看