Eclipse will add a 3-byte BOM header to the Utf-8 file, most of the text tools will work, but Ant and other tools will not compile the code properly, and the following code replaces the Utf-8 BOM header:
Package org.sl.util; Import Java.io.File; /** * Replace Java file with space Utf-8 BOM Header * @author SHANL */public class Replaceutf8bom {public static void main (string[] args) {if ( Args.length = = 0) {help (); return;} if ("-D". Equalsignorecase (Args[0)) {System.out.println ("replaced the specified directory: '" +args[1]+ ') Utf-8 BOM headers for all. java files); Removedirectoryutf8bom (Args[1]); Return } if ("F". Equalsignorecase (Args[0])) {System.out.println ("replaced the specified file: ' + args[1] + ' Utf-8 BOM Header"); Removefileutf8bom (Args[1]); Return } System.out.println ("Wrong parameter: + args[0]"); Help (); static void Help () {System.out.println ("replaceutf8bom-d < directory name >|-f < filename >"); System.out.println ("D < directory name: Traversing the Utf-8 BOM header of the. java file that deletes the entire directory"); System.out.println ("F < filename: Delete utf-8 BOM header for specified. java file"); } static void Removefileutf8bom (String filestr) {file file = null; file = new file (FILESTR); Removefileutf8bom (file);} St atic void Removefileutf8bom (file file) {Utf8bomhelper.replaceutf8bom (file);} static void Removedirectoryutf8bom ( String dirstr) {Filedir = null; file[] files = null; dir = new File (DIRSTR); Files = dir.listfiles (new Javafilefilter ()); if (null!= files && 0!= files.length) {for (File f:files) {if (F.isdirectory ()) {Removedirectoryutf8bom ( F.getabsolutepath ()); }else{Removefileutf8bom (f);}} } } }
Package org.sl.util; Import Java.io.File; Import Java.io.RandomAccessFile; Class Utf8bomhelper {/** * replaces the UTF-8 BOM header of the specified file with a space (first 3 bytes) * @param file */public static void Replaceutf8bom (file file) {Ra Ndomaccessfile accessfile = null; byte[] Utf8bom = new Byte[3]; int readsize = 0; if (null = = file) {return;} try{accessfile = new Randomaccessfile (file, rw); readsize = Accessfile.read (Utf8bom); if (0 >= readsize) {return;} if ( -17 = utf8bom[0] && -69 = = utf8bom[1] && -65 ==utf8bom[2]) {Accessfile.see K (0); Accessfile.write (New byte[]{', ', '}); }}catch (Exception ex) {ex.printstacktrace ();} finally{try{if (null!= accessfile) accessfile.close ();} catch (Exception ex) {}}}}
Package Org.sl.util Import java.io.File import java.io.FileFilter/** * used to filter non. java files * @author KFBSL */class Java FileFilter implements filefilter{//If the file name extension is:. Java public boolean accept (file dir, String name) {if (Null = name | | "". Equals (name)) {return false;} Return Name.touppercase (). EndsWith (". JAVA "); Public Boolean accept (File pathname) {if (Pathname.isdirectory ()) {return true;} String fileName = Pathname.getname (); return accept (null, fileName); } }