To even get to etc, the phone has to be rooted.
Then write a shell script and run it in a terminal app. (Or develop an app that's both an editor and shell script runner.)
(A shell script to do much more than add a line to hosts is going to be kind of involved.)
To just rename the file (I don't know why you'd want to - a hosts file with just localhost won't do much, and if you want to add lines to it, just add them in a text editor), root the phone and use a root file manager. Select the file, tap "rename" and rename it. You don't need a script to do that.