鱼C论坛

 找回密码
 立即注册
查看: 2358|回复: 2

[已解决]Java写入文件时怎样把写入内容写到文件开头?

[复制链接]
发表于 2022-4-12 15:33:18 | 显示全部楼层 |阅读模式
50鱼币
如题;

比如说:
(随便举个例子)
Test.txt 文件存在内容:
Hello World!

写入内容为 name

预期效果为:
name
Hello World!
最佳答案
2022-4-12 15:33:19
现有的类好像都没有办法。
先读取数据,操作数据,然后写入。
网上提供了的方法。
  1. public static void insert(String filename, long offset, byte[] content) throws IOException {
  2.                 RandomAccessFile r = new RandomAccessFile(new File(filename), "rw");
  3.                 RandomAccessFile rtemp = new RandomAccessFile(new File(filename + "~"), "rw");
  4.                
  5.                 long fileSize = r.length();
  6.                 FileChannel sourceChannel = r.getChannel();
  7.                 FileChannel targetChannel = rtemp.getChannel();

  8.                 sourceChannel.transferTo(offset, (fileSize - offset), targetChannel);
  9.                 sourceChannel.truncate(offset);
  10.                 r.seek(offset);
  11.                 r.write(content);

  12.                 long newOffset = r.getFilePointer();
  13.                 targetChannel.position(0L);
  14.                 sourceChannel.transferFrom(targetChannel, newOffset, (fileSize - offset));

  15.                 sourceChannel.close();
  16.                 targetChannel.close();

  17.         }
复制代码


调用insert("test.txt", 0, "中国".getBytes());

另一个方法,使用的类不同。

  1.     public void test3() throws IOException {

  2.         RandomAccessFile raf1 = new RandomAccessFile("hello.txt","rw");

  3.         raf1.seek(3);//将指针调到角标为3的位置
  4.         //保存指针3后面的所有数据到StringBuilder中
  5.         StringBuilder builder = new StringBuilder((int) new File("hello.txt").length());
  6.         byte[] buffer = new byte[20];
  7.         int len;
  8.         while((len = raf1.read(buffer)) != -1){
  9.             builder.append(new String(buffer,0,len)) ;
  10.         }
  11.         //调回指针,写入“xyz”
  12.         raf1.seek(3);
  13.         raf1.write("xyz".getBytes());

  14.         //将StringBuilder中的数据写入到文件中
  15.         raf1.write(builder.toString().getBytes());

  16.         raf1.close();

  17.     }
复制代码

最佳答案

查看完整内容

现有的类好像都没有办法。 先读取数据,操作数据,然后写入。 网上提供了的方法。 调用insert("test.txt", 0, "中国".getBytes()); 另一个方法,使用的类不同。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-12 15:33:19 | 显示全部楼层    本楼为最佳答案   
现有的类好像都没有办法。
先读取数据,操作数据,然后写入。
网上提供了的方法。
  1. public static void insert(String filename, long offset, byte[] content) throws IOException {
  2.                 RandomAccessFile r = new RandomAccessFile(new File(filename), "rw");
  3.                 RandomAccessFile rtemp = new RandomAccessFile(new File(filename + "~"), "rw");
  4.                
  5.                 long fileSize = r.length();
  6.                 FileChannel sourceChannel = r.getChannel();
  7.                 FileChannel targetChannel = rtemp.getChannel();

  8.                 sourceChannel.transferTo(offset, (fileSize - offset), targetChannel);
  9.                 sourceChannel.truncate(offset);
  10.                 r.seek(offset);
  11.                 r.write(content);

  12.                 long newOffset = r.getFilePointer();
  13.                 targetChannel.position(0L);
  14.                 sourceChannel.transferFrom(targetChannel, newOffset, (fileSize - offset));

  15.                 sourceChannel.close();
  16.                 targetChannel.close();

  17.         }
复制代码


调用insert("test.txt", 0, "中国".getBytes());

另一个方法,使用的类不同。

  1.     public void test3() throws IOException {

  2.         RandomAccessFile raf1 = new RandomAccessFile("hello.txt","rw");

  3.         raf1.seek(3);//将指针调到角标为3的位置
  4.         //保存指针3后面的所有数据到StringBuilder中
  5.         StringBuilder builder = new StringBuilder((int) new File("hello.txt").length());
  6.         byte[] buffer = new byte[20];
  7.         int len;
  8.         while((len = raf1.read(buffer)) != -1){
  9.             builder.append(new String(buffer,0,len)) ;
  10.         }
  11.         //调回指针,写入“xyz”
  12.         raf1.seek(3);
  13.         raf1.write("xyz".getBytes());

  14.         //将StringBuilder中的数据写入到文件中
  15.         raf1.write(builder.toString().getBytes());

  16.         raf1.close();

  17.     }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-4-12 15:34:33 | 显示全部楼层
我只能写成:

Hello World!
name

这样。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-4 14:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表