Java IO _字元編碼__Ajax

來源:互聯網
上載者:User

1.瞭解常用的幾種編碼

2.掌握亂碼的產生原因



如果沒有處理好編碼的事情,則肯定在程式中出現亂碼。


System 類可以取得系統的相關資訊,一直在接觸此類。

public class CharSetDemo01{public static void main(String args[]){System.out.println("系統預設編碼:" +System.getProperty("file.encoding")) ;// 擷取當前系統編碼}};
因為現在在本機環境,是中文環境,所以是使用GBK 進行編碼,那麼很明顯,如果現在在程式中使用了ISO8859-1編碼。如果要編碼,則需要使用String 類的支援。
public byte[] getBytes(String charsetName)                throws UnsupportedEncodingException
可以指定要的字元編碼。

import java.io.OutputStream ;import java.io.FileOutputStream ;import java.io.File ;public class CharSetDemo02{public static void main(String args[]) throws Exception {File f = new File("D:" + File.separator + "test.txt") ;// 執行個體化File類OutputStream out = new FileOutputStream(f) ;// 執行個體化輸出資料流byte b[] = "中國,你好。".getBytes("ISO8859-1") ;// 轉碼操作out.write(b) ;// 儲存out.close() ;// 關閉}};
亂碼產生的根本原因就是字元編碼不統一造成的。

總結:

1、瞭解幾種學用的編碼特點

2、理解亂碼是如何產生的

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.