English 中文(简体)
替换卷宗中的一条线
原标题:Replace a line in File
  • 时间:2012-05-06 01:51:38
  •  标签:
  • java
  • file

我试图取代本档案中文本的第3行,但无法更新适当的行文:

 ENTITY test_labelOperation IS
generic (
    NO_OF_COLS  : integer := 640;// This is 3rd line and replace should be look like NO_OF_RECTGlE=100;
    NO_OF_ROWS  : integer := 480; 
    NO_BITS_CC  : integer := 10;
    DATA_WORD : integer :=8; 
    CODE_WIDTH :integer :=9
    );
END test_labelOperation;

在这方面,我正在尝试:

  RandomAccessFile raf=new RandomAccessFile(absoluteFile,"rw");
  int lineCount=0;
  while(raf.read()!=-1) {
      String s=raf.readLine();
      //System.out.println(s);
      String stAry[];
      lineCount++;
      if (lineCount==3) {
         System.out.println(s);
         stAry=s.split("=",2);
         stAry[0]="NO_OF_RECTGlE";
         stary[1]=100+";";                        
         String str=Arrays.toString(stAry);
         byte []byt=str.getBytes();
         raf.write(byt);

         }
      }
最佳回答

我的猜测是,问题在于,你读了第3行,把档案点移到该行的后面。 因此,当你填写raf.write(byt)时,你将这一行文写到底,而不是该行的顶部。

您在开始阅读第3行之前,需要记住这一立场,使用raf.position(,然后在撰写之前填写raf.seek(oldPosition)

但我确实不建议你这样做。 这并不等于在编辑中替换案文。 替代体的长度必须与所取代的体格完全相同,否则,你将最后在你档案中用垃圾处理。 如果更换方位更长,那么你就别无选择,只能把整个档案从零页上改写。

我强烈建议你考虑简单地读到所有线,取代第三行,然后把整个档案放在旧文件上。 执行工作更加容易,更有力。

http://commons.apache.org/io/“rel=“nofollow” 简明:

List<String> lines = FileUtils.readLines(file);
lines.set(2, "NO_OF_RECTGlE=100;");
FileUtils.writeLines(file, lines);
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签