`

java 创建多级目录和文件

阅读更多
/**
	 * 新建目录.
	 * 
	 * @param path 文件路径
	 * @throws Exception
	 */
	public static void createDirectory(String path) throws Exception {
		if (StringUtils.isEmpty(path)) {
			return;
		}
		try {
			// 获得文件对象
			File f = new File(path);
			if (!f.exists()) {
				// 如果路径不存在,则创建
				f.mkdirs();
			}
		} catch (Exception e) {
			log.error("创建目录错误.path=" + path, e);
			throw e;
		}
	}

	/**
	 * 新建文件.
	 * 
	 * @param path 文件路径
	 * @throws Exception
	 */
	public static void createFile(String path) throws Exception {
		if (StringUtils.isEmpty(path)) {
			return;
		}
		try {
			// 获得文件对象
			File f = new File(path);
			if (f.exists()) {
				return;
			}
			// 如果路径不存在,则创建
			if (!f.getParentFile().exists()) {
				f.getParentFile().mkdirs();
			}
			f.createNewFile();
		} catch (Exception e) {
			log.error("创建文件错误.path=" + path, e);
			throw e;
		}
	}

	/**
	 * 保存文件(文件不存在会自动创建).
	 * 
	 * @param path 文件路径
	 * @param content 文件内容
	 * @throws Exception
	 */
	public static void saveFile(String path, String content) throws Exception {
		saveFile(path, content, DEFAULT_ENCODE);
	}

	/**
	 * 保存文件(文件不存在会自动创建).
	 * 
	 * @param path 文件路径
	 * @param content 文件内容
	 * @param encoding 编码(UTF-8/gb2312/...)
	 * @throws Exception
	 */
	public static void saveFile(String path, String content, String encoding) throws Exception {
		FileOutputStream fileOutputStream = null;
		BufferedOutputStream bw = null;
		try {
			// 获得文件对象
			File f = new File(path);
			// 默认编码UTF-8
			encoding = (StringUtils.isEmpty(encoding)) ? DEFAULT_ENCODE : encoding;
			// 如果路径不存在,则创建
			if (!f.getParentFile().exists()) {
				f.getParentFile().mkdirs();
			}
			// 开始保存文件
			fileOutputStream = new FileOutputStream(path);
			bw = new BufferedOutputStream(fileOutputStream);
			bw.write(content.getBytes(encoding));
		} catch (Exception e) {
			log.error("保存文件错误.path=" + path + ",content=" + content + ",encoding=" + encoding, e);
			throw e;
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e1) {
				}
			}
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e1) {
				}
			}
		}
	}

 

分享到:
评论
3 楼 zoel175185 2016-07-27  
if (!f.getParentFile().exists()) { 
                f.getParentFile().mkdirs(); 
            } 

这个可能报空指针异常 f.getParentFile() 可能返回NULL
2 楼 u014251458 2016-05-06  
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
1 楼 u014251458 2016-05-06  
         

相关推荐

    java创建多级目录文件的实例讲解

    `File`类提供了多种方法来操作文件和目录,包括创建、删除、重命名等。 创建多级目录的关键在于使用`mkdirs()`方法。这个方法会递归地创建目录,直到指定的完整路径存在。如果路径中的某个中间目录已经存在,`...

    java FTP服务器文件上传下载,创建多级文件夹.zip

    可以使用`FTPClient`的`listNames()`或`listFiles()`方法获取FTP服务器上的文件和目录列表,然后根据需要进行查找。 在提供的压缩包"java FTP服务器文件上传下载,创建多级文件夹.zip"中,包含了一个可以直接运行的...

    多用户多级目录文件系统的实现

    总结来说,利用Java实现多用户多级目录文件系统,需要关注以下几个关键点:文件和目录的抽象表示、权限控制与身份验证、读写操作的实现、并发访问控制以及持久化存储策略。通过合理的设计和编程,可以构建出安全、...

    java 创建文件夹或文件

    根据提供的文件信息,本文将详细解释Java中如何利用文件流动态创建文件夹或文件的关键知识点。主要内容包括: ...以上介绍的方法提供了创建文件和目录的基础,开发者可以根据具体需求进一步扩展和优化。

    java创建和删除文件夹、文件

    `mkdir()`方法用于创建单级目录,而`mkdirs()`则可以创建多级目录。 ```java public void newFolder(String folderPath) { try { String filePath = folderPath; File myFilePath = new File(filePath); if (!...

    在Linux下用 mkdir 命令来创建目录和子目录的用法

    如果需要创建多级目录,例如`parent/child`,不使用`-p`选项时,Linux会提示找不到父目录。但使用`-p`(--parents)选项,可以一次性创建所有必要的父目录,如`mkdir -p parent/child`,这会在当前目录下创建`...

    Java在Excel中创建多级分组、折叠或展开分组的实现

    Java在Excel中创建多级分组、折叠或展开分组的实现 Java在Excel中创建多级分组、折叠或展开分组的实现是指通过Java程序在Excel中创建多级分组、折叠或展开分组的方法。在Excel中,分组可以对行或列进行分组,并设置...

    Java实现将多目录多层级文件打成ZIP包,以及解压ZIP包

    在Java编程中,处理文件和压缩包操作是常见的任务,特别是在软件开发和数据传输中。本文将详细讲解如何使用Java实现将多目录多层级的文件打成ZIP包,以及如何解压这些ZIP包。我们将主要关注`FileUtils.java`这个类,...

    JAVA解压ZIP多层目录文件(需ant.jar

    ### JAVA解压ZIP多层目录文件(需ant.jar) #### 概述 本文将详细介绍一个Java方法,该方法用于解压包含多层目录结构的ZIP文件,并能够支持中文文件名。这种方法利用了Apache Ant库中的`org.apache.tools.zip....

    模拟文件系统(Java版)

    一个用Java实现的一个虚拟文件系统,支持多用户使用,模拟了数据分块,实现了一些基本的文件目录操作,如创建目录,目录跳转,目录的重命名,复制拷贝,删除,以及文件的创建,编辑,重命名和删除,查看目录和文件的...

    Java目录,文件操作

    Java是一种广泛使用的编程语言,尤其在企业级应用中,它提供了强大的文件和目录操作功能。以下是一些关于Java中操作文件和目录的关键知识点: 1. **创建文件夹**: Java中的`File`类提供了创建目录的方法。通过...

    Java IO创建目录和文件实例代码

    "Java IO创建目录和文件实例代码" 本篇文章主要分享了Java IO创建目录和文件的实例代码,提供了详细的代码示例和解释,帮助开发者快速掌握Java IO的文件操作技能。 Java IO创建目录 在 Java 中,创建目录可以使用...

    java实现创建指定路径下的文本文件的写功能

    - `java.io.File`: 用于表示文件和目录的抽象类。 - `java.io.FileWriter`: 提供向文件写入字符流的功能。 - `java.io.BufferedWriter`: 提供缓冲区来提高写入效率。 2. **创建目录和文件** - 使用`File`类创建...

    Java导出数据到Excel文件中(支持多表头)

    3. 设计和创建多级表头,一级表头在前,二级表头在其下。 4. 从数据库查询数据,根据数据结构创建行和单元格。 5. 设置单元格样式和内容。 6. 写入数据到Excel文件并关闭工作簿。 通过以上步骤,我们可以构建一个...

    根据文件日期创建日期层级文件夹并迁移文件

    例如,使用Python就是一个好选择,因为Python有良好的跨平台支持,其标准库中的文件和目录操作函数在不同操作系统上行为一致。 5. **文件组织结构**:压缩包子文件的文件名列表包含了`log4j.properties`、`config....

    JAVA创建文件夹文件.doc

    总结一下,使用Java创建文件夹主要涉及`File`类的`mkdirs()`方法,创建文件则需要`FileOutputStream`和`DataOutputStream`来读写数据。删除文件时,使用`File`类的`delete()`方法。在进行这些操作时,确保正确处理...

    java中用FileUtil实现文件读写

    #### 删除文件和目录:`deleteFile` 和 `deleteDir` 为了实现文件及目录的删除操作,`FileUtil` 提供了两个方法:`deleteFile` 用于删除单个文件,而`deleteDir` 则负责删除整个目录及其下的所有子目录和文件。 - ...

    编写一个java应用程序,将一个包含多个子目录和文件的目录复制到另外一个指定的目录下

    综上所述,编写一个能够复制包含多级子目录和文件的Java应用程序需要理解文件和目录操作、递归、异常处理、日期格式化以及可能的用户交互设计。通过合理组合这些知识点,可以构建出一个功能完整的目录复制工具。

    java代码笔记2010-06-11:java_x_文件_字节流,创建一个一个文件目录,在该目录下创建一个文件 txt, 然后写入内容进去, 再读取文件.

    这个类提供了许多方法来操作文件和目录。例如,我们可以调用`mkdir()`或`mkdirs()`方法来创建一个新目录。`mkdir()`用于创建单级目录,而`mkdirs()`则可以创建多级目录。假设我们有一个路径`/path/to/new/directory`...

    java操作Hadoop源码之HDFS Java API操作-创建目录

    `mkdirs()`方法会递归地创建多级目录,如果路径中的父目录不存在,它会自动创建。 最后,别忘了关闭`FileSystem`实例,释放资源: ```java fs.close(); ``` 以上就是使用Java API操作Hadoop HDFS创建目录的完整...

Global site tag (gtag.js) - Google Analytics