RandomAccessFile 在特定行修改字符串

RandomAccessFile to modify a string at a specific line

简介

我用这个项目挑战了自己。

我需要修改 .txt 文件中特定行的数据而不做接下来的事情:

一个简单的 .txt 文件将包含一切!

文本文件

1;asd1324;2019-05-22 18:28:56;0;0;
2;asd1324;2019-05-22 18:28:56;0;0;
3;asd1324;2019-05-22 18:28:56;0;0;
4;asd1324;2019-05-22 18:28:56;0;0;
5;asd1324;2019-05-22 18:28:56;0;0;
6;asd1324;2019-05-22 18:28:56;0;0;

遵循这种格式,即带“;”的字符串作为分隔符。

我的代码尝试适用于从 0 到 9 的数字

(0 到 9 表示第一个数字 -> 1;asd1324;2019-05-22 18:28:56;0;0;)

public static void test(int lineNumber, String data)
    {
        String line;


          try{

              System.out.println("----DEBUG TEST----\n");

              RandomAccessFile file = new RandomAccessFile("parking.txt", "rw");

              System.out.println("File pointer is: " + file.getFilePointer());

              System.out.println("Line size is: " + file.readLine().length());

              System.out.println("Read line is: " + (line = file.readLine()) + " with size: " + line.length());     

              file.seek(line.length());

              System.out.println("File pointer is: " + file.getFilePointer());

              file.writeChars("\n");
              file.writeBytes("7;asd1324;2019-05-22 18:28:56;0;0;");
              file.writeChars("\n");


              System.out.println("File pointer is: " + file.getFilePointer());

              System.out.println("Line size is: " + file.readLine().length());

              System.out.println("Read line is: " + (line = file.readLine()) + " with size: " + line.length());    


              file.seek(lineNumber);

              file.close();

          }catch(IOException e){System.out.println(e);}

          System.out.println("\n----DEBUG TEST----\n");        



    }

理解问题

字符串 -> 1;asd1324;2019-05-22 18:28:56;0;0;

示例: 1;asd1324;2019-05-22 18:28:56;2019-06-01 10:11:16;100;

我想通过替换其位置上的字符串来添加它们。

备注

我们将随时知道我们要修改的字符串的大小(方法)并采取相应的行动。

我对此有何期待

能够从 Java 中的文件修改行。这也可能是此操作的通用方法。

没有存储介质(即 HDD、RAM、闪存等)的文本文件。计算机总是以二进制(即字节)存储所有内容。 文本文件是人类的概念。

以上例为例:

1;asd1324;2019-05-22 18:28:56;0;0;
2;asd1324;2019-05-22 18:28:56;0;0;
3;asd1324;2019-05-22 18:28:56;0;0;
4;asd1324;2019-05-22 18:28:56;0;0;
5;asd1324;2019-05-22 18:28:56;0;0;
6;asd1324;2019-05-22 18:28:56;0;0;

这是前两行在计算机上的样子(十六进制):

313B6173 64313332 343B3230 31392D30 352D3232 2031383A 32383A35 363B303B 303B0A
323B6173 64313332 343B3230 31392D30 352D3232 2031383A 32383A35 363B303B 303B0A

注意该行是如何终止的 (0A),这是大多数 *nix 系统的行尾字符。在 Windows 上它将是 0D0A(或者它是 0A0D?)并且在 Mac 上它将是 0D。

现在要换行,绝对不能比原来的长,否则会覆盖到下一行的开头。假设您将第一行从

1;asd1324;2019-05-22 18:28:56;0;0;

1;asd1324;2019-05-22 18:28:56;0;01;

你最终会得到(前两行):

313B6173 64313332 343B3230 31392D30 352D3232 2031383A 32383A35 363B303B 30313B
323B6173 64313332 343B3230 31392D30 352D3232 2031383A 32383A35 363B303B 303B0A

因此您的文件现在将读取为:

1;asd1324;2019-05-22 18:28:56;0;01;2;asd1324;2019-05-22 18:28:56;0;0;
3;asd1324;2019-05-22 18:28:56;0;0;
4;asd1324;2019-05-22 18:28:56;0;0;
5;asd1324;2019-05-22 18:28:56;0;0;
6;asd1324;2019-05-22 18:28:56;0;0;

你现在有 5 行而不是 6 行,因为你已经覆盖了原始第一行的行终止字符。

综上所述,修改文本文件而不重写它可能是一项非常乏味和艰巨的任务。用您的修改重写整个过程会更快更容易。否则,相比之下,军事新兵训练营就像在公园里散步一样。