java文件一些操作
1、文件操作
public static void main(String args[]) throws IOException { String dirPath = "d:/aaa/bbb/"; File fileDirPath = new File(dirPath); System.out.println(fileDirPath.exists()); //判断文件目录是否存在 if (!fileDirPath.exists()) { if (!fileDirPath.isDirectory()) { fileDirPath.mkdir(); } } String filePath = dirPath + "/4244.txt"; System.out.println(filePath); File file = new File(filePath); //判断文件是否存在 if (!file.exists()) { file.createNewFile(); } //写数据到文件 BufferedWriter out = new BufferedWriter(new FileWriter(file)); out.write("55555fsfddasf55"); out.write("aaaaaaaa"); out.close(); System.out.println("文件创建成功!"); // System.out.println(delAllFile(dirPath)); //删除文件 // file.delete(); //读取文件 FileInputStream fileInputStream = new FileInputStream(filePath); int i ; byte[] bytes = new byte[1024]; while ((i = fileInputStream.read(bytes))!= -1) { String s = new String(bytes); System.out.println(s); } fileInputStream.close(); }
2、文件打包成tar
先导入pom:
<dependency> <groupId>org.xeustechnologies</groupId> <artifactId>jtar</artifactId> <version>1.1</version> </dependency>
/** * 文件打成tar */ //原来的文件地址 FileOutputStream dest; //打包后的tar地址 TarOutputStream out; try { dest = new FileOutputStream(tarPath); out = new TarOutputStream(new BufferedOutputStream(dest)); File f = new File(txtPath); out.putNextEntry(new TarEntry(f, f.getName())); BufferedInputStream origin = new BufferedInputStream(new FileInputStream(f)); int count; byte data[] = new byte[2048]; while ((count = origin.read(data)) != -1) { out.write(data, 0, count); } out.flush(); origin.close(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法