简体   繁体   中英

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 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. ie 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.

你必须计算开口{ s和关闭}一旦你有相同的数字你找到了结束的...

熟悉编写Eclipse插件的人可能能够扩展现有的代码格式化程序插件来执行此操作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM