Java implements file conversion between different encodings. Using the InputStreamReader or FileReader class, they can automatically convert a specific character encoding to a local character code. Otherwise, use the writeUTF () method in the DataOutputStream class to write a string in Unicode text. Of course, you must use DataInputStream to open it and read these strings using the readUTF () method.
Why do we need to convert the encoding? As we all know, Java is based on Unicode, but the operating systems all have their own internal encoding methods that may be incompatible with Unicode, therefore, the input received by the user may belong to different code systems. The strings displayed by the program must be decoded using a recognizable method by the local operating system.
To convert different codes, follow these steps:
1. Compile the basic framework of the ConvertEncoding class, which includes the main () method, usage () method, and convert () method:
The code is as follows: |
Copy code |
Public class ConvertEncoding { Public static void main (String [] args ); Public static void usage (); Public static void convert (String infile, String outfile, String from, String) Throws IOException, UnsupportedEncodingException; } |
2. The Main () method implements the conversion of an encoding format file into another encoding format:
The code is as follows: |
Copy code |
Public static void main (String [] args) { String from = null, to = null; String infile = null, outfile = null; For (int I = 0; I <args. length; I ++) {// process command line parameters If (I = args. length-1) usage ();. If (args [I]. equals ("-from") from = args [++ I]; Else if (args [I]. equals ("-to") to = args [++ I]; Else if (args [I]. equals ("-in") infile = args [++ I]; Else if (args [I]. equals ("-out") outfile = args [++ I]; Else usage (); } Try {convert (infile, outfile, from, to);} // start conversion Catch (Exception e) {// Handling exceptions System. exit (1 ); } } |
3. The usage () method prompts you to enter the command line correctly. The code is as follows:
The code is as follows: |
Copy code |
Public static void usage () { System. err. println ("Usage: java ConvertEncoding <options> \ n" + "Options: \ n \ t-from <encoding> \ n \ t" + "-To <encoding> \ n \ t" + "-In <file> \ n \ t-out <file> "); System. exit (1 ); } |
4. The Convert () method converts the encoding method. The code is as follows:
The code is as follows: |
Copy code |
Public static void convert (String infile, String outfile, String from, String) Throws IOException, UnsupportedEncodingException { // Set up byte streams. InputStream in; If (infile! = Null) in = new FileInputStream (infile ); Else in = System. in; OutputStream out; If (outfile! = Null) out = new FileOutputStream (outfile ); Else out = System. out; // Set the default encoding method If (from = null) from = System. getProperty ("file. encoding "); If (to = null) to = System. getProperty ("file. encoding "); // Create a producer stream for read/write Reader r = new BufferedReader (new InputStreamReader (in, from )); Writer w = new BufferedWriter (new OutputStreamWriter (out, )); /** Copy characters from input to output. InputStreamReader * Convert the character from the original encoding method to Unicode * OutputStreamWriter converts the character from Unicode encoding to the specified output encoding method. * Cannot output characters encoded in the specified output encoding method '? ' **/ Char [] buffer = new char [4096]; Int len; While (len = r. read (buffer ))! =-1 ). W. write (buffer, 0, len );. R. close ();. W. close (); }
|
Note: The ConvertEncoding class needs to introduce import java. io .*;