用Java删除文件的最后一行

发布于 2025-01-03 09:52:27 字数 116 浏览 0 评论 0原文

我有一个 .txt 文件,我想用 Java 处理该文件。我想删除它的最后一行。

我需要有关如何实现此目的的想法,而不必将整个内容复制到另一个文件中并忽略最后一行。有什么建议吗?

I have a .txt file, which I want to process in Java. I want to delete its last line.

I need ideas on how to achieve this without having to copy the entire content into another file and ignoring the last line. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忱杏 2025-01-10 09:52:27

您可以通过扫描文件找到最后一行的开头,然后使用 FileChannel.truncateRandomAccessFile.setLength

You could find the beginning of the last line by scanning the file and then truncate it using FileChannel.truncate or RandomAccessFile.setLength.

面如桃花 2025-01-10 09:52:27

通过使用RandomAccessFile,您可以:

  • 找到最后一个换行符的位置。要读取位置:getFilePointer()。要截断文件:setLength(long)
  • 优化:要只读文件末尾,请使用方法 seek(long) 向前跳转。挑战在于定义跳跃。

否则读取整个文件并仅存储“\n”的最后位置。 // Unix 换行约定

import java.io.*;

public class TruncateFileExample {
    public static void main(String[] args) {
        String filename = "path/to/your/file.txt";

        try (RandomAccessFile raf = new RandomAccessFile(filename, "rw")) {
            long fileLength = raf.length();
            if (fileLength == 0) {
                // File is empty, nothing to delete
                return;
            }

            // Start searching for the last newline character from the end of the file
            long position = fileLength - 1;
            raf.seek(position);

            int lastByte;
            while ((lastByte = raf.read()) != -1) {
                if (lastByte == '\n') {
                    // Found the last newline character
                    break;
                }
                position--;
                raf.seek(position);
            }

            // Truncate the file at the position of the last newline character
            raf.setLength(position);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

By using RandomAccessFile you can:

  • find the position of the last new line character. To read the position: getFilePointer(). To truncate the file: setLength(long).
  • optimization: to read only end of the file use method seek(long) to jump ahead. Challenge is to define the jump.

Otherwise read whole file and store only the last position of the "\n". // Unix new line convention

import java.io.*;

public class TruncateFileExample {
    public static void main(String[] args) {
        String filename = "path/to/your/file.txt";

        try (RandomAccessFile raf = new RandomAccessFile(filename, "rw")) {
            long fileLength = raf.length();
            if (fileLength == 0) {
                // File is empty, nothing to delete
                return;
            }

            // Start searching for the last newline character from the end of the file
            long position = fileLength - 1;
            raf.seek(position);

            int lastByte;
            while ((lastByte = raf.read()) != -1) {
                if (lastByte == '\n') {
                    // Found the last newline character
                    break;
                }
                position--;
                raf.seek(position);
            }

            // Truncate the file at the position of the last newline character
            raf.setLength(position);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文