根据日期创建文件夹:
先创建文件夹:
//1.使用递归方法-一层一层创建文件夹
public static void createFiles(File parentPath,String[] module,int currIndex) throws IOException{
if(parentPath.exists()){
String path=parentPath.getPath()+"/"+module[currIndex];
parentPath=new File(path);
if(!parentPath.exists()){parentPath.mkdir();}
if(currentIndex<module.length-1){
createFiles(parentPath,module,++currIndex);
}
}
}
//2.使用递归-文件目录创建文件夹
public static void createFolder(String path){
File parentFile=new File(path);
if(!parentFile.getparentFile().exists()){
createFolder(parentFile.getparentFile().getPath());
}
}
//main
public static void main(Stirng [] args)throws IOException(){
//1.
//File parentFile=new File(PropertiesUtil.getProperty("project.webroot.path"));
//String[] floders=new String[]{"1"."2","3","4"};
//createFiles(parentFile,floders,0);
//2.
String file="d:/1/2/3/4";
}
//service测试获取图片路径,Hex2byte类,使用2.
String jdljImage=PropertiesUtil.getProperty("project.webroot.path"));
Calender cal=Calender.getinstance();
int year=cal.get(Calender.YEAR);
int month=cal.get(calender.MONTH)+1;
int day=cal.get(calender.DAY_OF_MONTH);
String image=mo.getValue("image").toString();
String datePath="/upload/image/bbsInfo/"+year+"/"+month+"/"+day+"/";
if(!"".equals(image) && !"\"\"".equals(image)){
String parentpath=jdljImage+datePath;
StringUtil.createFolder(parentpath);//调用文件
String fileName=datePath+new Date().getTime()+".jpg";
String filePath=jdljImage+fileName;
//测试用 image=Hex2byte.getImgeHexString(image,"jpg");
Hex2byte.saveImage(image,filePath."jpg");
params.put("image",fileName);将fileName保存在数据库中
}
jdk中提供了创建目录的两种方法实现 mkdir() 和 mkdirs()
前者是在给定目录结构path参数下创建指定的目录,
如果path中少了一层目录没有创建则会抛出异常(FileNotFoundException)
而第二个方法,则是相对安全的实现,因为他会自动创建不存在的父级目录。
String filePath = "c:/1";
File fp = new File(filePath);
// 目录已存在创建文件夹
if (!fp.exists()) {
fp.mkdir();// 目录不存在的情况下,会抛出异常
}
}
2.目录不存在,创建整个目录
public static void main(String [] args){
String filePath = "d:/1/2/3/4/5/6";
File fp = new File(filePath);
// 创建目录
if (!fp.exists()) {
fp.mkdirs();// 目录不存在的情况下,创建目录。
}
}
(1)
- File file=new File("C:\\Users\\QPING\\Desktop\\JavaScript\\2.htm");
- if(!file.exists())
- {
- try {
- file.createNewFile();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
(2)
- File file =new File("C:\\Users\\QPING\\Desktop\\JavaScript");
- //如果文件夹不存在则创建
- if (!file .exists() && !file .isDirectory())
- {
- System.out.println("//不存在");
- file .mkdir();
- } else
- {
- System.out.println("//目录存在");
- }
package com.xhkj.util;
import java.io.File;
import java.io.IOException;
public class CreateFileUtil {
public static boolean CreateFile(String destFileName) {
File file = new File(destFileName);
if (file.exists()) {
System.out.println("创建单个文件" + destFileName + "失败,目标文件已存在!");
return false;
}
if (destFileName.endsWith(File.separator)) {
System.out.println("创建单个文件" + destFileName + "失败,目标不能是目录!");
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("创建单个文件" + destFileName + "成功!");
return true;
} else {
System.out.println("创建单个文件" + destFileName + "失败!");
return false;
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("创建单个文件" + destFileName + "失败!");
return false;
}
}
public static boolean createDir(String destDirName) {
File dir = new File(destDirName);
if(dir.exists()) {
System.out.println("创建目录" + destDirName + "失败,目标目录已存在!");
return false;
}
if(!destDirName.endsWith(File.separator))
destDirName = destDirName + File.separator;
// 创建单个目录
if(dir.mkdirs()) {
System.out.println("创建目录" + destDirName + "成功!");
return true;
} else {
System.out.println("创建目录" + destDirName + "成功!");
return false;
}
}
public static String createTempFile(String prefix, String suffix, String dirName) {
File tempFile = null;
try{
if(dirName == null) {
// 在默认文件夹下创建临时文件
tempFile = File.createTempFile(prefix, suffix);
return tempFile.getCanonicalPath();
}
else {
File dir = new File(dirName);
// 如果临时文件所在目录不存在,首先创建
if(!dir.exists()) {
if(!CreateFileUtil.createDir(dirName)){
System.out.println("创建临时文件失败,不能创建临时文件所在目录!");
return null;
}
}
tempFile = File.createTempFile(prefix, suffix, dir);
return tempFile.getCanonicalPath();
}
} catch(IOException e) {
e.printStackTrace();
System.out.println("创建临时文件失败" + e.getMessage());
return null;
}
}
public static void main(String[] args) {
// 创建目录
String dirName = "c:/test/test0/test1";
CreateFileUtil.createDir(dirName);
// 创建文件
String fileName = dirName + "/test2/testFile.txt";
CreateFileUtil.CreateFile(fileName);
// 创建临时文件
String prefix = "temp";
String suffix = ".txt";
for(int i = 0; i < 10; i++) {
System.out.println("创建了临时文件:" + CreateFileUtil.createTempFile(prefix, suffix, dirName));
}
}
//删除制定文件夹的所有文件及根文件夹
public void deleteFile(String path) {
// TODO Auto-generated method stub
File f = new File(path);
if(f.isDirectory())
{
File[] file = f.listFiles();
for (File file2 : file) {
this.deleteFile(file2.toString());
file2.delete();
}
}else
{
f.delete();
}
f.delete();
相关推荐
在Unity引擎中,创建文件夹是一项基础但至关重要的任务,特别是在构建游戏或应用程序时需要组织资源和数据。本文将深入探讨如何在Unity中实现“一键创建”常见的文件夹,如StreamingAssets、Resources等,并讨论这些...
在Teamcenter的二次开发中,我们经常会遇到需要根据业务需求定制功能的情况,例如“批量创建文件夹”。这个例子是关于如何利用Java编程语言来实现这样的功能。 首先,我们要了解Teamcenter的API,它是提供给开发者...
批量创建文件夹(除了包含exe程序,还包含了C、C#、python程序源码),不但可以实现单层的文件夹创建,还能实现多层嵌套的文件夹创建。提供了使用说明书,可按照说明操作,如果想自定义实现批量生产文件夹,可以自己...
批量创建文件夹2.0一款高效的根据文件名字快速生成文件夹工具+辅助分类 本工具可以根据文件名快速创建多个文件夹,可以根据文件的名字批量生成目录一行一个。本工地还可以根据文件的名字创建文件夹,并把这个文件放...
在IT领域,批量创建文件夹是一项常见的操作,尤其在数据管理、项目组织或者自动化脚本编写时显得尤为重要。批量创建文件夹的目的是提高效率,避免手动一个接一个地创建大量文件夹的繁琐工作。以下是对这个主题的详细...
在C#中,我们可以使用`System.IO`命名空间下的`Directory`类来创建文件夹。以下是如何创建一个新文件夹的示例: ```csharp using System.IO; // 创建文件夹 Directory.CreateDirectory("C:\\NewFolder"); ``` 这...
本示例将围绕"TC创建文件夹,item,form的例子"展开,详细介绍如何在TeamCenter环境中进行文件夹创建、item的管理以及form的设计,同时也会提及菜单功能和BOM(Bill of Materials,物料清单)的运用。 1. 创建...
本篇将详细介绍如何使用C#进行FTP操作,包括上传文件、下载文件、在服务器上创建文件夹以及删除服务器上的文件。 首先,进行FTP操作需要使用到System.Net命名空间中的FtpWebRequest和FtpWebResponse类。以下是一些...
### 创建文件夹知识点详解 #### 一、概述 在计算机编程中,经常需要对文件系统进行操作,例如创建文件夹。本篇文章将详细介绍如何在已知路径下创建一个名为`name`的新文件夹。该过程涉及到Java编程语言中的基本...
按日期创建文件夹;txt文件”项目中,我们可以深入探讨以下几个核心知识点: 1. **LabVIEW程序设计**: LabVIEW的编程基于视觉化的“虚拟仪器”概念,用户通过拖拽图标和连接线来构建程序。这个小程序可能包含了一...
在这个“LabVIEW按日期创建文件夹按时间写入txt数据.rar”压缩包中,我们很显然看到它涉及到LabVIEW的文件操作功能,特别是与日期和时间相关的文件管理以及文本数据的写入。 1. **文件夹创建**: 在LabVIEW中,...
在这里,`drive:`代表你想要创建文件夹的驱动器字母,`path`是你想在哪个路径下创建文件夹,而`foldername`则是你要创建的文件夹的名称。 **在当前盘符下创建文件夹** 如果要在当前所在盘符的当前目录下创建一个...
在ASP中,我们可以通过VBScript或JScript等脚本语言实现文件操作,包括创建文件夹和复制文件。这些功能在构建网站时非常有用,比如在用户上传文件、备份数据或者进行自动化处理时。 首先,让我们探讨如何在ASP中...
有时需要按日期批量创建文件夹,如果量大,逐个创建是比较麻烦的,此程序可帮助读者按照一定的规律创建文件夹。如每逢周一、周三、周四创建一个相应日期为名称的文件夹,程序中的示例可以实现。同时也附带了每天都...
python根据excel列批量创建文件夹
本文将详细介绍如何利用MFC来实现打开文件夹和创建文件夹的功能。 首先,打开文件夹的操作通常涉及到用户界面交互,这可以通过调用`SHBrowseForFolder`函数来实现。`SHBrowseForFolder`函数用于显示一个标准的文件...
在C#编程中,批量创建文件夹和设置访问权限是一项常见的任务,特别是在系统管理、软件开发和自动化脚本中。这个实例将向我们展示如何利用C#的强大功能来实现这一目标。下面,我们将深入探讨涉及的知识点,并提供相关...
3. **创建文件夹**:使用`mkdir`命令,批处理脚本会尝试在当前目录下创建一个以`%foldername%`命名的新文件夹。`mkdir`是创建目录的命令,它可以创建单个或者多个层级的目录结构。 4. **错误处理**:为了确保批处理...
以系统日期创建文件夹 并把要备份的文件拷贝到该文件夹下 要修改里面的路径 c:\atc2000f\datebase 改为你要备份文件所在路径
Folder Creator 1.0 是一个简单易用的开源工,大小仅为30KB,主要功能是批量创建文件夹。用户只需输入标文件夹路径、文件夹名称前缀、起始和文件夹数量,即可预览并批量生成文件夹。 这个工具的设计初衷是为了帮助...