Eclipse Coding Issues

Source: Internet
Author: User
Tags tmp file

Typically, Mac and Windows encoding are not the same under Eclipse. If you have a Chinese Java sources, there is usually garbled characters.

Solve---small program!

Import Java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.nio.bytebuffer;import Java.nio.channels.filechannel;import Java.nio.charset.charset;public class Charencodingexchange {private final static string source_encoding = "EUC-CN";p rivate final static string target_encoding = "UTF-8";p rivate static string Sourc  E_dir = "/users/pandans/desktop/coastalbank/src";p rivate static String Target_dir = "/users/pandans/desktop/tmp";/** * * @param args */public static void main (string[] args) {//TODO auto-generated method stubtry {Exchange (SOURCE_DIR);} CATC H (Exception e) {//TODO auto-generated catch Blockxje.printstacktrace ();}} /** * * Exchange the character encoding from Srcdir to TARGETDIR * * * * @param srcdir * * @param targetDir */public s tatic void Exchange (String srcdir) {string abspath = ""; if (!srcdir.equals (Source_dir)) {Abspath = srcdir.substring (sourc E_dir.length ()); String TarGetdir = Target_dir + abspath; File TargetDirectory = new file (TargetDir), if (Targetdirectory.isdirectory () &&!targetdirectory.exists ()) { Targetdirectory.mkdirs ();}} File Sourcedirectory = new file (Srcdir), if (Sourcedirectory.exists ()) {if (Sourcedirectory.isfile ()) {String Targetfilepath = Target_dir + abspath;try {fileencodingexchange (sourcedirectory, Targetfilepath);} catch (IOException E {//TODO auto-generated catch Blocke.printstacktrace ();}} else {file[] childs = Sourcedirectory.listfiles (); for (File Child:childs) Exchange (Child.getpath ());}}} private static void Fileencodingexchange (File infile,string targetabsfilepath) throws IOException {FileInputStream fin = Null FileOutputStream fout = null; FileChannel fcin = null; FileChannel fcout = null; System.out.println (infile + "" + Targetabsfilepath); String Tmptargetpath = targetabsfilepath.substring (0, Targetabsfilepath.lastindexof (file.separator)); File Tmptargetdir = new file (Tmptargetpath); if (!tmptargetdir.exists ()) TmptarGetdir.mkdirs (); try {fin = new FileInputStream (infile); fout = new FileOutputStream (targetabsfilepath); fcin = Fin.getchannel (); fcout = Fout.getchannel (); Bytebuffer buffer = Bytebuffer.allocatedirect (1024x768), while (true) {buffer.clear (); int r = fcin.read (buffer); if (r = =-1) {b Reak;} Buffer.flip (); String encoding = System.getproperty ("file.encoding"); Fcout.write (Bytebuffer.wrap (charset.forname). Decode (buffer). ToString (). GetBytes (target_encoding)));}} catch (FileNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Au To-generated catch Blocke.printstacktrace ();} Finally {if (fin! = null) {Fin.close ();} if (fcin! = null) {Fcin.close ();} if (fout! = null) fout.close (); if (fcout! = null) Fcout.close ();}}

Modify the path yourself.

Run under command line

Compile.

Javac Charencodingexchange.java

Run:

Java Charencodingexchange

First, the engineering code becomes the desired encoding. This is UTF-8.

Overwrite the TMP file with the project SRC.

--------------eclipse Modifying the default encoding---------------

UTF-8

This modification of the whole project will automatically change.

UTF-8

Eclipse Coding Issues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.