将文件内容全部倒置,并写入文件
来源:互联网 发布:制作电子文档软件 编辑:程序博客网 时间:2024/06/07 05:56
/** * 用途: 将一个文件全部倒置,并写入文件,如果文件的路径和源文件一样的话,就会覆盖源文件的内容 * @param fromFilepath 参数:源文件的全路径 * @param toFilePath 参数:目标文件的父目录路径,不加文件名 * 注意事项:文件的父级路径不存在则会自动创建 */ public static void inversionFile(String fromFilepath, String toFilePath) { File from = new File(fromFilepath); if (!from.exists()) { System.out.println("源文件不存在,请重新审核输入的文件路径!"); } File to = new File(toFilePath); if (!to.exists()) { to.mkdirs(); } ByteArrayOutputStream ou = null; FileOutputStream fileOutputStream = null; try { RandomAccessFile ref = new RandomAccessFile(from, "rw"); ou = new ByteArrayOutputStream(); byte[] buffer = new byte[1024 * 8]; int len; while ((len = ref.read(buffer)) != -1) { ou.write(buffer, 0, len); } byte[] byteArray = ou.toByteArray(); //使用StringBuilder包装String,调用方法实现反转 StringBuilder sBuilder=new StringBuilder(new String(byteArray)); fileOutputStream = new FileOutputStream(new File(to, from.getName())); fileOutputStream.write( sBuilder.reverse().toString().getBytes()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (ou != null) { try { ou.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
阅读全文
0 0
- 将文件内容全部倒置,并写入文件
- 将JSON内容写入并读取文件
- MSSQL分享:sp_writeall 将文本文件内容全部写入某一文件
- OC 将字典写入数组 并将数组写入文件 对文件内容操作
- 创建文件并写入内容
- 将文件内容以16进制形式输出,并写入文件
- 如何读取一个文件并将内容写入一个新文件中
- 将文件内容逐行读取处理并写入对应文件中
- Oracle--将文件内容写入数据库
- C/C++将内容写入外部文件
- 将文件中的内容写入链表
- java操作将内容写入文件
- JAVA操作将内容追加写入文件
- 将Excel文件内容写入到数据库
- 将内容写入到文件中
- c++ 将文件内容写入到数组
- 将列表内容写入文件中
- redis-cli 将内容写入文件中
- MAven学习 — <dependencies> && <dependencyManagement>
- Yolo 9000的darknet实现
- 【剑指offer】面试题3:数组中重复的数字
- Maven学习 — <build> 中的 <plugins>元素的作用
- lianxi
- 将文件内容全部倒置,并写入文件
- 链表的头插法
- 01 目录结构、基础命令的讲解、管道和重定向(讲解邮件的发送)
- 对Java中classpath、path和Java_HOME的理解
- 线程执行完,死亡后,还能再次执行start吗?
- 【树的倍增】【二分 or LCA】USACO MAR11银组 聚会地点
- 部分概念对比
- 算法学习之旅,初级篇(7)--数字塔
- Glide 圆形加载图片