getAbsolutePath、getCanonicalPath、FileInputStream、FileOutputStream

本文提供了一个使用Java进行文件读写的示例程序。通过该示例,读者可以了解到如何创建文件、写入数据到文件以及如何从文件中读取数据。示例中包括了获取当前项目的根目录路径,并在此基础上指定文件位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package fileStream;


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


public class Test01 {


/**
* @param args
* @throws IOException 
*/
public static void main(String[] args) throws IOException {
String projectRootPath = new File("").getAbsolutePath();//获取项目路径

// String projectRootParentPath = new File("..").getCanonicalPath();
// File fileFolder = new File(projectRootParentPath+File.separator+"files");//规范路径名(将.以及..进行转换)
// if(!fileFolder.exists()){
// fileFolder.mkdir();
// }
// File file = new File(fileFolder.getCanonicalFile()+File.separator+"hello.txt");

File file = new File(projectRootPath+File.separator+"files"+File.separator+"hello.txt");
if(!file.exists()){
file.createNewFile();
}

//将字符串写入文件
String str = "hello world,my name is bing!";
byte[] bytes = str.getBytes();
FileOutputStream fos = new FileOutputStream(file);
fos.write(bytes);
fos.flush();
fos.close();

//从文本文件中读取内容
FileInputStream fis = new FileInputStream(file);
byte[] bytes2 = new byte[(int)file.length()];
fis.read(bytes2);
fis.close();
System.err.println(file.getCanonicalPath()+"\n"+new String(bytes2));
}


}
public boolean unzip(String zipFilePath, String outputDir) { boolean success = true; LogUtils.i("开始解压ZIP文件: " + zipFilePath + " 到目录: " + outputDir); // 创建输出目录(如果不存在) File dir = new File(outputDir); if (!dir.exists()) { LogUtils.d("尝试创建输出目录: " + outputDir); if (!dir.mkdirs()) { LogUtils.e("无法创建输出目录: " + outputDir); return false; } LogUtils.i("成功创建输出目录: " + outputDir); } // 获取输出目录的规范路径用于安全检查 String canonicalOutputDir; try { canonicalOutputDir = dir.getCanonicalPath(); LogUtils.d("输出目录规范路径: " + canonicalOutputDir); } catch (IOException e) { LogUtils.e("获取输出目录规范路径失败: " + e.getMessage()); return false; } try (ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFilePath)))) { LogUtils.d("打开ZIP输入流成功"); ZipEntry entry; int entryCount = 0; while ((entry = zis.getNextEntry()) != null) { entryCount++; String originalEntryName = entry.getName(); LogUtils.d("处理条目 #" + entryCount + ": " + originalEntryName + " | 目录: " + entry.isDirectory() + " | 大小: " + entry.getSize() + " bytes"); // 规范化路径处理 String entryName = normalizePath(originalEntryName); if (entryName.isEmpty()) { LogUtils.e("跳过无效条目: " + originalEntryName); continue; } LogUtils.d("规范化后路径: " + entryName); File outputFile = new File(outputDir, entryName); // 详细路径安全日志 try { String canonicalPath = outputFile.getCanonicalPath(); LogUtils.d("目标文件规范路径: " + canonicalPath); // 路径遍历安全检查 if (!canonicalPath.startsWith(canonicalOutputDir + File.separator)) { LogUtils.e("安全违规: 条目 " + originalEntryName + " 试图逃逸到 " + canonicalPath); success = false; continue; } } catch (IOException e) { LogUtils.e("路径解析错误: " + outputFile.getAbsolutePath() + " | 错误: " + e.getMessage()); success = false; continue; } // 创建父目录(如果需要) File parentDir = outputFile.getParentFile(); if (parentDir != null && !parentDir.exists()) { LogUtils.d("尝试创建父目录: " + parentDir.getAbsolutePath()); if (!parentDir.mkdirs()) { LogUtils.e("无法创建父目录: " + parentDir.getAbsolutePath()); success = false; continue; } LogUtils.i("成功创建父目录: " + parentDir.getAbsolutePath()); } if (!entry.isDirectory()) { LogUtils.d("解压文件到: " + outputFile.getAbsolutePath()); try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outputFile))) { byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead; long totalBytes = 0; while ((bytesRead = zis.read(buffer)) != -1) { bos.write(buffer, 0, bytesRead); totalBytes += bytesRead; // 安全检查逻辑(每秒最多1次) long currentTime = System.currentTimeMillis(); if (currentTime - lastCheckTime > 1000) { synchronized (this) { if (currentTime - lastCheckTime > 1000) { LogUtils.d("执行安全检测..."); if (!HmiJNIImpl.getInstance().mapControl()) { LogUtils.e("安全检测失败,终止解压"); return false; } lastCheckTime = currentTime; } } } } LogUtils.i("成功解压文件: " + outputFile.getName() + " | 大小: " + totalBytes + " bytes"); } catch (IOException e) { LogUtils.e("写入文件失败: " + outputFile.getAbsolutePath() + " | 错误: " + e.getMessage()); success = false; // 删除部分写入的文件 if (outputFile.exists() && !outputFile.delete()) { LogUtils.e("无法删除部分写入的文件: " + outputFile.getAbsolutePath()); } } } else { LogUtils.d("创建目录: " + outputFile.getAbsolutePath()); if (!outputFile.exists()) { if (!outputFile.mkdirs()) { LogUtils.e("无法创建目录: " + outputFile.getAbsolutePath()); success = false; } else { LogUtils.i("成功创建目录: " + outputFile.getAbsolutePath()); } } else { LogUtils.d("目录已存在: " + outputFile.getAbsolutePath()); } } zis.closeEntry(); LogUtils.d("完成处理条目: " + originalEntryName); } LogUtils.i("处理完成所有条目,共 " + entryCount + " 个"); } catch (IOException e) { LogUtils.e("解压ZIP文件时发生错误: " + e.getMessage()); success = false; } // 结果处理 if (success) { LogUtils.i("解压成功,尝试删除原始ZIP文件: " + zipFilePath); File zipFile = new File(zipFilePath); if (zipFile.delete()) { LogUtils.i("成功删除原始ZIP文件: " + zipFilePath); } else { LogUtils.e("无法删除原始ZIP文件: " + zipFilePath); success = false; } } else { LogUtils.e("解压过程中遇到错误,保留原始ZIP文件"); } LogUtils.i("解压结果: " + (success ? "成功" : "失败")); return success; } 2025-07-28 09:58:56.945 20477-20509 ISA_NAVI_ com.isa.navi I [ZipUtils.java:unzip:1] 开始解压ZIP文件: /data/data/com.isa.navi/KVM_EU_202502_T1.0_S31N.zip 到目录: /data/data/com.isa.navi/sign/ 2025-07-28 09:58:56.961 20477-20509 ISA_NAVI_ com.isa.navi I [ZipUtils.java:unzip:7] 成功创建输出目录: /data/data/com.isa.navi/sign/ 2025-07-28 09:58:56.962 20477-20509 ISA_NAVI_ com.isa.navi E [ZipUtils.java:unzip:68] 解压ZIP文件时发生错误: /data/data/com.isa.navi/KVM_EU_202502_T1.0_S31N.zip: open failed: ENOENT (No such file or directory) 2025-07-28 09:58:56.962 20477-20509 ISA_NAVI_ com.isa.navi E [ZipUtils.java:unzip:74] 解压过程中遇到错误,保留原始ZIP文件 2025-07-28 09:58:56.962 20477-20509 ISA_NAVI_ com.isa.navi I [ZipUtils.java:unzip:75] 解压结果: 失败
最新发布
07-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值