将文件内容全部倒置,并写入文件
2017-08-12 10:25
316 查看
/** * 用途: 将一个文件全部倒置,并写入文件,如果文件的路径和源文件一样的话,就会覆盖源文件的内容 * @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(); } } } }
相关文章推荐
- MSSQL分享:sp_writeall 将文本文件内容全部写入某一文件
- java读写文件-经过多方搜索整合成我需要的功能(读写指定文件夹下面的多个指定名称的txt中的内容全部写入到一个指定的txt中)
- 将目标文件的一部分或者全部内容拷贝到另外一个目标文件中 objcopy
- python---对python的help命令内容写入特定文件中
- 定时获取url内容并写入文件中
- 如何将LINUX本地的文件内容写入到远程服务器的文件中
- 字符流,写入内容到根目录下的hello.txt文件
- struts文件下载---自主写入下载内容
- C++如何将vector<string> str里的内容全部写入TXT
- Python每日练习 14 15 16 将某txt的内容写入到xls文件中
- 字符串写入文件 C++ 读文件 将文件内容读入到字符串string中
- java使用CsvReader和CsvWriter对csv文件内容进行读取和写入操作
- 批量生成文件并写入大量内容
- C#读取HTML文件内容写入记事本
- qt写入文件内容
- 将文件内容以16进制形式输出,并写入文件
- 拖动N个文件,改变文件的最后修改时间,并将文件内容全部设为0
- 在文件指定位置读取和写入内容RandomAccessFile
- asp,向文件中写入内容
- RandomAccessFile 替换文件内容 并写入