java生成文件夹和文件的简单示例分享

 更新时间:2014年04月02日 09:00:33   作者:  
这篇文章主要介绍了java生成文件夹和文件的简单示例,需要的朋友可以参考下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

实现文件夹和文件生成

复制代码 代码如下:

package com.gotobus.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class JFile {

 public static boolean createFile(String destFileName) { 
        File file = new File(destFileName); 
        if(file.exists()) { 
         return false; 
        } 
        if (destFileName.endsWith(File.separator)) { 
         return false; 
        } 
        if(!file.getParentFile().exists()) { 
      if(!file.getParentFile().mkdirs()) { 
       return false; 
            } 
        } 
        try { 
            if (file.createNewFile()) { 
             return true; 
            } else { 
             return false; 
            } 
        } catch (IOException e) { 
            e.printStackTrace(); 
            return false; 
        } 
    } 

     public static boolean createDir(String destDirName) { 
        File dir = new File(destDirName); 
        if (dir.exists()) { 
         return false; 
        } 
        if (!destDirName.endsWith(File.separator)) { 
            destDirName = destDirName + File.separator; 
        } 
       if (dir.mkdirs()) { 
           return true; 
        } else { 
            return false; 
        } 
    } 

    public static String createTempFile(String prefix, String suffix, String dirName) { 
        File tempFile = null; 
        if (dirName == null) { 
            try{ 
                tempFile = File.createTempFile(prefix, suffix); 
                return tempFile.getCanonicalPath(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
               return null; 
            } 
        } else { 
            File dir = new File(dirName); 
            if (!dir.exists()) { 
                if (!JFile.createDir(dirName)) { 

                    return null; 
                } 
            } 
            try { 
                tempFile = File.createTempFile(prefix, suffix, dir); 
                return tempFile.getCanonicalPath(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
                return null; 
            } 
        } 
    }

    public static void copyFile(String oldPath, String newPath) {
        try {
            int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(oldPath);
            if (oldfile.exists()) {
             InputStream inStream = new FileInputStream(oldPath);
                FileOutputStream fs = new FileOutputStream(newPath);
                byte[] buffer = new byte[1444];
                int length;
                while ( (byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread;
                    fs.write(buffer, 0, byteread);
                }
                inStream.close();
            }
        }
        catch (Exception e) {
                e.printStackTrace();

        }
    }
}

相关文章

  • 举例讲解Java编程中this关键字与super关键字的用法

    举例讲解Java编程中this关键字与super关键字的用法

    这篇文章主要介绍了Java编程中this关键字与super关键字的用法示例,super是this的父辈,在继承过程中两个关键字经常被用到,需要的朋友可以参考下
    2016-03-03
  • springboot自动配置没有生效的问题定位(条件断点)

    springboot自动配置没有生效的问题定位(条件断点)

    这篇文章主要介绍了springboot自动配置未生效问题定位,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,下面我们来学习一下吧
    2019-06-06
  • Properties操作如何保存到属性文件

    Properties操作如何保存到属性文件

    这篇文章主要介绍了Properties操作保存到属性文件的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-06-06
  • elasticsearch索引创建create?index集群matedata更新

    elasticsearch索引创建create?index集群matedata更新

    这篇文章主要介绍了elasticsearch索引创建create?index及集群matedata更新,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-04-04
  • Java比较问题详细分析

    Java比较问题详细分析

    本篇文章主要给大家讲解了Java中比较问题的相关知识,一起参考学习下吧。
    2017-12-12
  • Spring使用注解进行引用类型的自动装配逐步分析

    Spring使用注解进行引用类型的自动装配逐步分析

    自动装配是springboot的核心,一般提到自动装配就会和springboot联系在一起。实际上Spring Framework早就实现了这个功能。Spring Boot只是在其基础上,通过SPI的方式,做了进一步优化
    2023-03-03
  • 使用JSONObject生成和解析json的方法

    使用JSONObject生成和解析json的方法

    下面小编就为大家带来一篇使用JSONObject生成和解析json的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • RabbitMQ消息队列中多路复用Channel信道详解

    RabbitMQ消息队列中多路复用Channel信道详解

    这篇文章主要介绍了RabbitMQ消息队列中多路复用Channel信道详解,消息Message是指在应用间传送的数据,消息可以非常简单,比如只包含文本字符串,也可以更复杂,可能包含嵌入对象,需要的朋友可以参考下
    2023-08-08
  • Struts2 的国际化实现方式示例

    Struts2 的国际化实现方式示例

    这篇文章主要介绍了Struts2 的国际化实现方式示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • Springboot中集成Swagger2框架的方法

    Springboot中集成Swagger2框架的方法

    这篇文章主要介绍了Springboot中集成Swagger2框架的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-12-12

最新评论


http://www.vxiaotou.com