【问题标题】:writing to a txt file in random location java写入随机位置java中的txt文件
【发布时间】:2012-06-17 17:50:49
【问题描述】:

我正在尝试将字符串写入随机位置的 txt 文件,即我想编辑特定的 txt 文件。我不想追加,但我想做的就是在第 3 行写一些字符串。 我尝试使用 RandomAccessFile 进行相同操作,但是当我写入特定行时,它会替换该行的数据。

例如 a.txt-

1
2
3
4
5

我期望的输出..

1
2

3
4
5

我使用 RandomAccessFiles 所取得的成就

1
2

4
5


我尝试在替换之前保存第 3 行的内容,为此我使用了 readLine() 函数,但它不起作用,它给出了意想不到的结果。

【问题讨论】:

  • 我已将其附在问题中.. :)
  • 只是想告诉你,自从上次见到你以来,我已经大大更新了我的答案。我想你会觉得它很有趣。

标签: java file-handling random-access


【解决方案1】:

对于内存不太大的文件:

你可以read the file into a String 然后做这样的事情:

String s = "1234567890"; //This is where you'd read the file into a String, but we'll use this for an example.
String randomString = "hi";
char[] chrArry = s.toCharArray(); //get the characters to append to the new string
Random rand = new Random();
int insertSpot = rand.nextInt(chrArry.length + 1); //Get a random spot in the string to insert the random String
//The +1 is to make it so you can append to the end of the string
StringBuilder sb = new StringBuilder();
if (insertSpot == chrArry.length) { //Check whether this should just go to the end of the string.
  sb.append(s).append(randomString);
} else {
  for (int j = 0; j < chrArry.length; j++) {
    if (j == insertSpot) {
      sb.append(randomString);
    }
    sb.append(chrArry[j]);
  }
}
System.out.println(sb.toString());//You could then output the string to the file (override it) here.

对于内存太大的文件:

您可以对copying the file 进行某种变体,您可以事先确定一个随机点,然后在该块的其余部分之前将其写入输出流中。下面是一些代码:

public static void saveInputStream(InputStream inputStream, File outputFile) throws FileNotFoundException, IOException {
  int size = 4096;
  try (OutputStream out = new FileOutputStream(outputFile)) {
    byte[] buffer = new byte[size];
    int length;
    while ((length = inputStream.read(buffer)) > 0) {
      out.write(buffer, 0, length);
      //Have length = the length of the random string and buffer = new byte[size of string]
      //and call out.write(buffer, 0, length) here once in a random spot.
      //Don't forget to reset the buffer = new byte[size] again before the next iteration.
    }
    inputStream.close();
  }
}

像这样调用上面的代码:

InputStream inputStream = new FileInputStream(new File("Your source file.whatever"));
saveInputStream(inputStream, new File("Your output file.whatever"));

【讨论】:

  • 我实际上正在处理一个 .java 文件,它非常大,大约 200 多行,仍然会试一试,thnkx 很多
  • 200+ 行绝对不会太大,内存无法处理。如果文件是一两个千兆字节,您可能会开始担心内存(不确定究竟是什么时候成为问题,取决于您的系统),所以您应该没问题 :)
  • “200+ 行肯定不会太大,内存无法处理。” AFAIR Java 源文件的硬限制为 64Kb。轻松读取/操作/写入该数量。
  • @AndrewThompson 64KB 限制不是字节码限制吗?您可以在源文件中包含更多内容,例如厘米。
  • @weston 你可能就在那儿。听起来更合乎逻辑的是班级规模。尽管如此,我认为任何明智的评论水平都不会超过 3 倍(没有 cmets)。
【解决方案2】:

没有 API 可以做到这一点。 你可以做的是:

  1. 随机选择一个位置
  2. 到行尾
  3. 写 '\n + '你的文字'

【讨论】:

  • @1144031,现在就试试。非常感谢
【解决方案3】:

您只有两个选项,追加和覆盖。文件没有插入方法,因为它们不支持此操作。要插入数据,您必须重写文件的其余部分以将其向下移动。

【讨论】:

  • 我知道你在说什么,但是我在这里使用 .java 文件,我想在其中一个 java 文件中添加一条指令,这就是为什么我不能覆盖数据。跨度>
  • 这有什么改变?如果要插入,请向下移动要保留的文本。如果要删除,则必须向上移动文本,以免留下空白。无论您是编写文本还是二进制文件,.javaXML 文件都只能以一种方式工作。
  • alrgiht,它的工作.. thnks 很多,存储数据从文件到临时字符串,编辑它,n 写回文件.:) :)
猜你喜欢
  • 2018-03-26
  • 1970-01-01
  • 1970-01-01
  • 2013-07-05
  • 1970-01-01
  • 2016-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多