juniorbansal
juniorbansal

Reputation: 1271

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.

Upvotes: 0

Views: 811

Answers (3)

Peter Lawrey
Peter Lawrey

Reputation: 533462

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.

Upvotes: 2

Kelly S. French
Kelly S. French

Reputation: 12334

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

Upvotes: 0

pgras
pgras

Reputation: 12770

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

Upvotes: 0

Related Questions