开发者

Java - File Reading/Writing

I got some java files in my workspace. Now i want to write a java program which would read a text file coming from a different source, one at a time, line by line and insert those lines into respective java files in the workspace .

The text file would say me which file to insert into which ".java" class.

For example I have AddressType.Java in my workspace with getters, setters

I got a text file namely AddressType.txt which has some additional methods, comments, instance variables etc etc which should actually go and sit into the body of the AddressType.Java file.

For this i have a logic in my mind and its sort of working. I know how to read/write to files. Here is my problem. My approach is to replace the ending bracket } in AddressType.开发者_JAVA技巧Java with a space and then pull the contents of AddressType.txt into AddressType.java and then close the } again.

Can some one help me how to find EOF } in a .java file using a java program.

I cannot do this manually as I have 100's of java files and text files to move content to.

Please help. I hope some one understands this question.

Nothing is run time. Everything is compile time itself. I mean i need to merge the txt file contents into java file and then compile and ship the java files.


You can read the entire java text file into memory and remove the last } that way. Classes can't be very big. i.e less than a few MB. And you can do this with IOUtils.toString() in one line.

String textWithoutLast = IOUtils.toString(
      new FileInputStream(javaFilename)
      ).replaceAll("}\\s*$","");

This removes the last } with any amount of spaces, tabs or new lines after it.


You have to count the opening {s and the closing } once you have the same number you have found the closing one...


Someone familiar with writing Eclipse plugins might be able to extend the existing code formatter plugin to do this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜