Java向文本指定行插入值

需求:

使用Java在不覆写文本的情况下,指定将输入内容插入到指定行。

思路:

1、获取到写入内容的字节数,比如 Size = b.length()
2、使用Java中的RandomAccessFile类,获取写入的文本地址
3、通过randomAccessFile.seek()找到内容书写的定位
4、将定位以下的内容全部向下移动Size 字节
5、通过randomAccessFile.write(b),将内容添加到文本

代码如下:

import java.io.RandomAccessFile;
import java.util.regex.Pattern;

public class ESTest008 {
    public static void main(String[] args) throws Exception {
        insertNewWord(20, "\n魔兽世界", "F:/HotWords.php");
        insertNewWord(20, "\n地下城勇士", "F:/HotWords.php");
    }

    private static void insertNewWord(long skip, String str, String fileName) {
        try {
            RandomAccessFile randomAccessFile = new RandomAccessFile(fileName, "rw");
            if (skip < 0 || skip > randomAccessFile.length()) {
                return;
            }
            byte[] b = str.getBytes();
            System.out.println(b.length);
            System.out.println(randomAccessFile.length());
            randomAccessFile.setLength(randomAccessFile.length() + b.length);
            //把后面的内容往后面挪
            for (long i = randomAccessFile.length() - 1; i > b.length + skip - 1; i--) {
                randomAccessFile.seek(i - b.length);
                byte temp = randomAccessFile.readByte();
                randomAccessFile.seek(i);
                randomAccessFile.writeByte(temp);
            }
            randomAccessFile.seek(skip);
            randomAccessFile.write(b);
            randomAccessFile.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

结果如下:

<?php 
$s = <<<'EOF'
地下城勇士
魔兽世界
王者荣耀
骑乐无穷
阴阳师
EOF;
header("Content-type: text/html; charset=utf-8"); 
header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT', true, 200);
header('ETag: "5816f349-19"');
echo $s;
?>

       如果您喜欢我写的博文,读后觉得收获很大,不妨小额赞助我一下,让我有动力继续写出高质量的博文,感谢您的赞赏!!!

要在 Java 中向 Word 文档的指定位置插入图片,可以使用 Apache POI 库。以下是示例代码: ```java import org.apache.poi.util.IOUtils; import org.apache.poi.xwpf.usermodel.*; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; public class InsertImageInWord { public static void main(String[] args) throws IOException { // 打开 Word 文档 XWPFDocument document = new XWPFDocument(new FileInputStream("template.docx")); // 获取要插入图片的段落和位置 int paragraphIndex = 1; int positionInParagraph = 1; XWPFParagraph paragraph = document.getParagraphArray(paragraphIndex); XWPFRun run = paragraph.insertNewRun(positionInParagraph); // 插入图片 String imagePath = "image.png"; InputStream inputStream = new FileInputStream(imagePath); byte[] bytes = IOUtils.toByteArray(inputStream); run.addPicture(inputStream, XWPFDocument.PICTURE_TYPE_PNG, imagePath, Units.toEMU(100), Units.toEMU(100)); // 保存 Word 文档 FileOutputStream out = new FileOutputStream("output.docx"); document.write(out); out.close(); document.close(); } } ``` 在上述代码中,我们首先使用 `XWPFDocument` 类打开 Word 文档。接着,我们获取要插入图片的段落和位置,通过 `insertNewRun()` 方法创建一个新的运对象,并使用 `addPicture()` 方法将图片插入到文档中。最后,我们保存 Word 文档并关闭相关的流。 注意,代码中的 `Units.toEMU(100)` 表示将图片的宽度和高度设置为 100 磅(1 磅 = 1/72 英寸),可以根据需要进调整。另外,`insertNewRun()` 方法会将指定位置之后的文本、样式等往后移动,因此可能会影响原有的排版。如果需要更精确的控制,可以使用 `createRun()` 方法创建新的运对象,并设置相关的样式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北京小辉

你的鼓舞将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值