- 浏览: 496479 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (563)
- 工作经验 (12)
- 数据库 (13)
- Servlet (10)
- Struts2 (1)
- Spring (25)
- Eclipse (5)
- Hibernate (5)
- Eclips (8)
- HTTP (7)
- J2EE (21)
- EHcache (1)
- HTML (11)
- 工具插件使用 (20)
- JPA (2)
- 杂谈 (17)
- 数据结构与算法 (3)
- Cloud Foundry (1)
- 安全 (10)
- J2SE (57)
- SQL (9)
- DB2 (6)
- 操作系统 (2)
- 设计模式 (1)
- 版本代码管理工具 (13)
- 面试 (10)
- 代码规范 (3)
- Tomcat (12)
- Ajax (5)
- 异常总结 (11)
- REST (2)
- 云 (2)
- RMI (3)
- SOA (1)
- Oracle (12)
- Javascript (20)
- jquery (7)
- JSP自定义标签 (2)
- 电脑知识 (5)
- 浏览器 (3)
- 正则表达式 (3)
- 建站解决问题 (38)
- 数据库设计 (3)
- git (16)
- log4j (1)
- 每天100行代码 (1)
- socket (0)
- java设计模式 耿祥义著 (0)
- Maven (14)
- ibatis (7)
- bug整理 (2)
- 邮件服务器 (8)
- Linux (32)
- TCP/IP协议 (5)
- java多线程并发 (7)
- IO (1)
- 网页小工具 (2)
- Flash (2)
- 爬虫 (1)
- CSS (6)
- JSON (1)
- 触发器 (1)
- java并发 (12)
- ajaxfileupload (1)
- js验证 (1)
- discuz (2)
- Mysql (14)
- jvm (2)
- MyBatis (10)
- POI (1)
- 金融 (1)
- VMWare (0)
- Redis (4)
- 性能测试 (2)
- PostgreSQL (1)
- 分布式 (2)
- Easy UI (1)
- C (1)
- 加密 (6)
- Node.js (1)
- 事务 (2)
- zookeeper (3)
- Spring MVC (2)
- 动态代理 (3)
- 日志 (2)
- 微信公众号 (2)
- IDEA (1)
- 保存他人遇到的问题 (1)
- webservice (11)
- memcached (3)
- nginx (6)
- 抓包 (1)
- java规范 (1)
- dubbo (3)
- xwiki (1)
- quartz (2)
- 数字证书 (1)
- spi (1)
- 学习编程 (6)
- dom4j (1)
- 计算机系统知识 (2)
- JAVA系统知识 (1)
- rpcf (1)
- 单元测试 (2)
- php (1)
- 内存泄漏cpu100%outofmemery (5)
- zero_copy (2)
- mac (3)
- hive (3)
- 分享资料整理 (0)
- 计算机网络 (1)
- 编写操作系统 (1)
- springboot (1)
最新评论
-
masuweng:
亦论一次OutOfMemoryError的定位与解错 -
变脸小伙:
引用[color=red][/color]百度推广中运用的技术 ...
Spring 3 mvc中返回pdf,json,xml等不同的view -
Vanillva:
不同之处是什么??
Mybatis中的like查询 -
thrillerzw:
转了。做个有理想的程序员
有理想的程序员必须知道的15件事 -
liujunhui1988:
觉得很有概括力
15 个必须知道的 Java 面试问题(2年工作经验)
源:http://kingsleylong.iteye.com/blog/1447556
评:
我们都知道,Java File类能够创建文件或者文件夹,但是不能两个一起创建,假设我需要在一个(或多层)尚未创建的文件夹下新建一个文件,那将会很麻烦。所以写了一个简单的工具来完成这件事情。
package kingsleylong;
import java.io.File;
import java.io.IOException;
/**
* This class provides methods to create new file/directory
* @author kingsleylong
*
*/
public class DirMaker {
/**
* Enhancement of java.io.File#createNewFile()
* Create the given file. If the parent directory don't exists, we will create them all.
* @param file the file to be created
* @return true if the named file does not exist and was successfully created; false if the named file already exists
* @see java.io.File#createNewFile
* @throws IOException
*/
public static boolean createFile(File file) throws IOException {
if(! file.exists()) {
makeDir(file.getParentFile());
}
return file.createNewFile();
}
/**
* Enhancement of java.io.File#mkdir()
* Create the given directory . If the parent folders don't exists, we will create them all.
* @see java.io.File#mkdir()
* @param dir the directory to be created
*/
public static void makeDir(File dir) {
if(! dir.getParentFile().exists()) {
makeDir(dir.getParentFile());
}
dir.mkdir();
}
public static void main(String args[]) {
String filePath = "C:/temp/a/b/c/d/e/f/g.txt";
File file = new File(filePath);
try {
System.out.println("file.exists()? " + file.exists());
boolean created = createFile(file);
System.out.println(created?"File created":"File exists, not created.");
System.out.println("file.exists()? " + file.exists());
} catch (IOException e) {
e.printStackTrace();
}
}
}
评:
我们都知道,Java File类能够创建文件或者文件夹,但是不能两个一起创建,假设我需要在一个(或多层)尚未创建的文件夹下新建一个文件,那将会很麻烦。所以写了一个简单的工具来完成这件事情。
package kingsleylong;
import java.io.File;
import java.io.IOException;
/**
* This class provides methods to create new file/directory
* @author kingsleylong
*
*/
public class DirMaker {
/**
* Enhancement of java.io.File#createNewFile()
* Create the given file. If the parent directory don't exists, we will create them all.
* @param file the file to be created
* @return true if the named file does not exist and was successfully created; false if the named file already exists
* @see java.io.File#createNewFile
* @throws IOException
*/
public static boolean createFile(File file) throws IOException {
if(! file.exists()) {
makeDir(file.getParentFile());
}
return file.createNewFile();
}
/**
* Enhancement of java.io.File#mkdir()
* Create the given directory . If the parent folders don't exists, we will create them all.
* @see java.io.File#mkdir()
* @param dir the directory to be created
*/
public static void makeDir(File dir) {
if(! dir.getParentFile().exists()) {
makeDir(dir.getParentFile());
}
dir.mkdir();
}
public static void main(String args[]) {
String filePath = "C:/temp/a/b/c/d/e/f/g.txt";
File file = new File(filePath);
try {
System.out.println("file.exists()? " + file.exists());
boolean created = createFile(file);
System.out.println(created?"File created":"File exists, not created.");
System.out.println("file.exists()? " + file.exists());
} catch (IOException e) {
e.printStackTrace();
}
}
}
发表评论
-
BeanCopier系列之一:特性测试
2018-06-12 12:15 405源:http://czj4451.iteye.com/ ... -
Lombok 之 ToString
2018-03-20 19:35 595源:http://himichaelchu.iteye.com ... -
extends和super的区别
2018-01-14 19:05 540源:http://bbs.csdn.net/topics/38 ... -
double转BigDecimal,数变大
2017-09-09 14:02 571源:http://blog.csdn.net/linbrain ... -
循环依赖检测方法 spring源码方法
2016-07-06 18:58 1117场景:checkForAliasCircle(name, al ... -
Oracle模糊查询之(4.采用全文索引解决模糊查询,给出具体步骤)采用全文索引解决模糊查询速度慢的问题[主文]
2016-03-24 16:16 821源:http://blog.csdn.net/haiross/ ... -
理解和正确使用Java中的断言(assert)
2016-03-24 15:55 1030源:http://blog.csdn.net/leic ... -
string stringbuffer stringbuilder区别
2016-03-07 15:39 425string 是不可变类,内部数据结构为final char数 ... -
接口和抽象类有什么区别
2015-11-27 15:58 412源:http://m.blog.csdn.net/bl ... -
Java中Enum类型的序列化
2015-11-25 14:57 1210源:http://mysun.iteye.com/blog/1 ... -
serialVersionUID的作用
2015-11-08 15:27 538源:http://www.cnblogs.com/gu ... -
dom4j写xml时&会被转义成&的解决方法?
2015-09-08 20:52 912源:http://www.iteye.com/problems ... -
java ImageIO处理图像的封装
2015-09-06 10:17 344源:http://blog.csdn.net/hu_sheng ... -
XML 和 java对象相互转换
2015-08-17 16:45 0源:http://hbiao68.iteye.com/blog ... -
java 版本更迭
2015-08-03 10:19 564源:https://en.wikipedia.org/wiki ... -
谈抽象类与接口的区别之一
2015-04-23 11:46 512源:http://wenku.baidu.com/li ... -
Java基本数据类型
2015-04-17 14:57 654源:http://blog.csdn.net/zuolongs ... -
JAVA深复制(深克隆)与浅复制(浅克隆)
2015-04-17 14:23 408源:http://www.cnblogs.com/yxnchi ... -
java中的四种访问权限是?
2015-04-16 17:58 524源:http://zhidao.baidu.com/link? ... -
DMA Direct Memory Access(存储器直接访问)
2015-04-02 15:55 530源:http://baike.baidu.com/li ...
相关推荐
内建方法,java创建和删除文件夹、文件
java创建和删除文件夹、文件.doc
这是一个用JAVA编写的,在本地磁盘上创建一个文件目录,里面有文件夹和文件,然后再删除创建的这个目录,非常简单,希望能给初学JAVA的朋友一点帮助。如果这个程序有什么 问题,联系我felixlee@yeah.net
Java生成、修改文件夹和文件名,Java创建目录或文件夹,并修改、删除、重命名文件夹或文件名称,使用进行的文件操作实例。 super("目录和文件的创建、删除和更名"); //调用父类构造函数 jtfPath=new ...
java 通过文件流,动态创建文件夹或文件
根据文件日期自动创建层级目录文件夹(文件夹根据日期命名),然后将文件移动到文件夹里。通过启动文件可以在windows或Linux下直接运行工作
java创建文件夹及文件代码
用JAVA创建文件夹,文件,删除文件时,我们可以用这些方法来创建一个指定目录的文件夹或文件。
Java创建文件夹
功能:运行start.bat启动脚本,将自动读取默认(d:/needFolder)下的所有文件夹,并根据每个文件的创建时间创建相应日期的文件夹,然后将相应文件移动到相应文件夹中。本程序能在WINDOWS环境或者LINUX下直接运行。应用...
java代码,将文件夹下文件(包括子文件夹文件),全部合并到一个目标文件中
此文件支持对文件及文件夹结构的多种操作,其中包括: 读取文本文件内容 遍历指定路径下指定后缀的所有文件 新建目录 多级目录创建 新建文件 有编码方式的文件创建 删除文件 删除文件夹 删除指定文件夹下所有文件 ...
用java代码 获取文件创建时间,有一点小问题就是,当文件名有空格时,将会出错、有心人可解决、
java使用swing编写sfp文件与文件夹上传功能,后续给大家升级同时多个目标地址上传
JAVA 文件操作类和文件夹的操作代码实例,包括读取文本文件内容, 新建目录,多级目录创建,新建文件,有编码方式的文件创建, 删除文件,删除文件夹,删除指定文件夹下所有文件, 复制单个文件,复制整个文件夹的...
java 文件或文件夹 创建 删除 复制 移动 指定目录等
java FTP服务器文件上传下载,查找文件目录,不存在则添加,创建多级文件夹,java ,demo 直接使用,内有jar包
java文件夹排序,按时间、大小等排序。。。。。。
使用纯java代码遍历文件夹,生成一个文件夹目录结构的目录树。 及返回一个包含有 id(文件夹id) fid(当前文件夹父目录文件夹id) name (文件/文件夹路径)的集合对象。可根据这个结果传到前台,通过jquery.ztree 或是...
文件上传工具类,上传文件,上传导入文件到path文件夹下,判断图片大小是否超过要求,上传多张图片到服务器,上传图片到服务器,上传文件(在指定目录下创建年月日目录),上传导入文件到temp文件夹下,删除指定...