| import java.io.*; |
| |
| public class test { |
| |
| public static void main(String[] args) throws IOException { |
| |
| String writeContext = "SET NAMES utf8mb4;\n" + |
| "SET FOREIGN_KEY_CHECKS = 0;\n" + |
| "\n"; |
| |
| String path = "C:\\work\\git\\test"; |
| |
| int deep = 0; |
| |
| File file = new File(path); |
| |
| File[] array = file.listFiles(); |
| for(int i=0;i<array.length;i++) |
| { |
| if(array[i].isFile()) |
| { |
| for (int j = 0; j < deep; j++) |
| System.out.print(" "); |
| |
| String txtFilePath = "C:\\work\\git\\test\\" + array[i].getName(); |
| System.out.println(txtFilePath); |
| |
| addContainsToFile(txtFilePath, 0, writeContext); |
| } |
| } |
| } |
| |
| public static void addContainsToFile(String filePath, int position, String contents) throws IOException { |
| |
| File file = new File(filePath); |
| System.out.println(file); |
| |
| if (!(file.exists() && file.isFile())) { |
| System.out.println("文件不存在 ~ "); |
| return; |
| } |
| |
| if ((position < 0) || (position > file.length())) { |
| System.out.println("position不合法 ~ "); |
| return; |
| } |
| |
| File tempFile = File.createTempFile("sss", ".temp", new File("C:/work")); |
| |
| FileOutputStream outputStream = new FileOutputStream(tempFile); |
| FileInputStream inputStream = new FileInputStream(tempFile); |
| |
| tempFile.deleteOnExit(); |
| |
| RandomAccessFile rw = new RandomAccessFile(file, "rw"); |
| |
| rw.seek(position); |
| int tmp; |
| |
| while ((tmp = rw.read()) != -1) { |
| outputStream.write(tmp); |
| } |
| |
| rw.seek(position); |
| rw.write(contents.getBytes()); |
| |
| while ((tmp = inputStream.read()) != -1) { |
| rw.write(tmp); |
| } |
| rw.close(); |
| outputStream.close(); |
| inputStream.close(); |
| } |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?