Basically, I am looking to add to a .txt file depending on whether a certain line exist already or not.

基本上,我希望添加到.txt文件,具体取决于某条线是否已经存在。

I.e. add the line to the .txt file if it doesn't exist already. Don't add the line to the .txt file if it exists.

即如果该行已不存在,请将该行添加到.txt文件中。如果存在,则不要将该行添加到.txt文件中。

Every line stands alone

每条线都是独立的

E.g.

One

Two

Three

and not One, Two, Three etc.

而不是一,二,三等

I was thinking of using readLine() but it seems that it defines the end of a line when there's a break, like there my lines have. The method doesn't take any arguments (e.g. line numbers) so I'm pretty short-handen :/.

我正在考虑使用readLine(),但似乎它在有休息时定义了行的结尾,就像我的行一样。该方法不接受任何参数(例如行号),所以我很简短:/。

Any he

3 个解决方案

#1


0

You can use, as you suggest, the readLine() method of RandomAccessFile to read each line of the file, looking for the expected text.

您可以按照建议使用RandomAccessFile的readLine()方法来读取文件的每一行,查找预期的文本。

From the javadoc for readLine():

从readLine()的javadoc:

A line of text is terminated by a carriage-return character ('\r'), a newline character ('\n'), a carriage-return character immediately followed by a newline character, or the end of the file.

一行文本由回车字符('\ r'),换行符('\ n'),回车字符后面紧跟换行字符或文件末尾终止。

If the expected text is not found, then you can use one of the write methods of RandomAccessFile, e.g. writeUTF() or writeBytes() to add the line of text to the file.

如果找不到预期的文本,那么您可以使用RandomAccessFile的一种写入方法,例如: writeUTF()或writeBytes()将文本行添加到文件中。

更多相关文章

  1. java写入文件的几种方法小结
  2. 使用SAX Filter将新元素插入XML文件
  3. 在文件中添加新数据后,JComboBox不会刷新
  4. 关于LSA的相关学习---短文本聚类涉及到的知识,一种主题模型(原载于
  5. 小聊天程序,访问文件之间的变量
  6. 请问用Java如何逐行的读取一个文本文件呀?我现在只能完整读取.
  7. FilenameFilter文件名过滤器使用实例
  8. java操作ftp实现文件的上传下载(适用于图片文档服务器)
  9. 怎么在html,Javascript,vBscript中实现从网页上接收数据存入文本

随机推荐

  1. Android静默安装和静默卸载
  2. Android实时抓包分析 : 善用adb调试桥
  3. Android事件分发机制详解:史上最全面、最
  4. 【android】使用android-resource-remove
  5. Android(安卓)Map Api 使用和开发(1) 添
  6. Android(安卓)OpenGLES2.0(三)——等腰直角
  7. Android应用程序窗口(Activity)的运行上下
  8. 7种形式的Android Dialog使用举例
  9. 三星揭幕之际,苹果高管再次炮轰Android和
  10. eBook 功能模块二之设置模块Android