java创建多级目录或文件夹

本文介绍了一种使用Java创建多级目录的方法,并提供了一个复制文件夹内容的实用代码示例,适用于需要批量处理文件的情况。
java写入文件前常常会遇到。需要创建多级目录,以下代码便可以轻松做到。
String   path="D:/xxx/yyy/zzz/";
StringTokenizer st=new StringTokenizer(path,"/");
String path1=st.nextToken()+"/";
String path2 =path1;
while(st.hasMoreTokens())
{
path1=st.nextToken()+"/";
path2+=path1;
File inbox = new File(path2);
if(!inbox.exists())
inbox.mkdir();
}


复制目录或者文件夹到新的目录下

/**     
* 复制整个文件夹内容
* @param oldPath String 原文件路径 如:d:/aaaa/css
* @param newPath String 复制后路径 如:f:/xxx/yyy/zzz/css
*/
public static void copyFolder(String oldPath, String newPath) {

try {
(new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
File a = new File(oldPath);
String[] file = a.list();
File temp = null;
for (int i = 0; i < file.length; i++) {
if (oldPath.endsWith(File.separator)) {
temp = new File(oldPath + file[i]);
} else {
temp = new File(oldPath + File.separator + file[i]);
}

if (temp.isFile()) {
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newPath
+ "/" + (temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if (temp.isDirectory()) {//如果是子文件夹
copyFolder(oldPath + "/ " + file[i], newPath + "/ "
+ file[i]);
}
}
} catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错 ");
e.printStackTrace();

}

}
Java创建多级文件夹压缩包,可借助`java.util.zip`包来实现。以下是实现步骤代码示例: 1. 查出目录文件的集合,也可单独查询目录文件后进行拼装。 2. 创建`ZipOutputStream`对象,用于将文件写入压缩包。 3. 遍历文件目录,将其添加到压缩包中。 4. 注意不能将`zipOutPutStream.finish()``zipOutPutStream.close()`同时使用,以免导致压缩包出现不可预料的压缩文件末端问题 [^2]。 以下是示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipMultiLevelFolders { public static void zipFolder(String sourceFolderPath, String zipFilePath) throws IOException { FileOutputStream fos = new FileOutputStream(zipFilePath); ZipOutputStream zipOut = new ZipOutputStream(fos); File sourceFolder = new File(sourceFolderPath); zipFiles(sourceFolder, sourceFolder.getName(), zipOut); zipOut.close(); fos.close(); } private static void zipFiles(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException { if (fileToZip.isHidden()) { return; } if (fileToZip.isDirectory()) { if (fileName.endsWith("/")) { zipOut.putNextEntry(new ZipEntry(fileName)); zipOut.closeEntry(); } else { zipOut.putNextEntry(new ZipEntry(fileName + "/")); zipOut.closeEntry(); } File[] children = fileToZip.listFiles(); for (File childFile : children) { zipFiles(childFile, fileName + "/" + childFile.getName(), zipOut); } return; } FileInputStream fis = new FileInputStream(fileToZip); ZipEntry zipEntry = new ZipEntry(fileName); zipOut.putNextEntry(zipEntry); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { zipOut.write(bytes, 0, length); } fis.close(); } public static void main(String[] args) { try { zipFolder("path/to/source/folder", "path/to/output.zip"); System.out.println("压缩完成"); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上述代码中,`zipFolder`方法用于将指定的源文件夹压缩到指定的压缩包文件中。`zipFiles`方法是一个递归方法,用于遍历源文件夹及其子文件夹中的所有文件,并将它们添加到压缩包中。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值