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);
     
        }
     
     
    }
    梦里不知身是客,一晌贪欢。
  • 相关阅读:
    【C#】Send data between applications
    【C#】Switch datatype between object and byte[]
    【C#】Get the html code of a webpage
    MSIL Hello World
    MonoGame 3.2 下,截屏与 Texture2D 的保存
    mciSendString 的两个小坑
    virtual 修饰符与继承对析构函数的影响(C++)
    让 OpenAL 也支持 S16 Planar(辅以 FFmpeg)
    博客园第一篇——SDL2+FFmpeg 制作简单播放器&同步
    第五次UML作业——结对作业二:班级成绩表
  • 原文地址:https://www.cnblogs.com/dccmmtop/p/5709969.html
Copyright © 2011-2022 走看看