也說eclipse檔案編碼
使用eclipse構建web應用程式時,中文亂碼的問題始終難以有效解決,下面提供一些常見的問題的解決方案。
1.更改專案檔編碼方式
2.如何查看一個檔案的編碼方式?
3.如何修改一個檔案的編碼方式?
1.更改專案檔的編碼方式?
如果項目剛剛開始,很幸運,先把整個項目的編碼方式改掉:http://blog.csdn.net/xuqianghit/archive/2010/11/08/5995384.aspx
2.如何查看那一個檔案的編碼方式?
如果項目已經進行了一部分,但是突然發現兩個團隊使用的編碼方式不一樣,怎麼辦?首先統一兩邊的餓編碼方式,那麼第一個問題來了:如何查看那一個檔案的編碼方式?最簡單的方法就是使用IE瀏覽器來查看,首先使用ie開啟需要查看的檔案,通過選擇編碼方式來查看檔案的編碼。
3.如何修改一個檔案的編碼方式?
方法1:手動修改
方法2:程式自動實現,不過程式需要自己動手寫,或者去網上down一個。好吧,開始自己寫一個簡單的程式,來實現字元的轉換。
using
System;
using
System.Text;
namespace
ConvertExample
{
class
ConvertExampleClass
{
static
void
Main()
{
string
unicodeString = "This string contains the unicode
character Pi(\u03a0)"
;
// Create two different encodings.
Encoding ascii = Encoding.ASCII;
Encoding unicode =
Encoding.Unicode;
// Convert
the string into a byte[].
byte
[] unicodeBytes =
unicode.GetBytes(unicodeString);
// Perform the conversion from one encoding to the
other.
byte
[] asciiBytes =
Encoding.Convert(unicode, ascii, unicodeBytes);
// Convert the new byte[] into a char[] and then
into a string.
// This is a
slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char
[] asciiChars = new
char
[ascii.GetCharCount(asciiBytes, 0,
asciiBytes.Length)];
ascii.GetChars(asciiBytes, 0,
asciiBytes.Length, asciiChars, 0);
string
asciiString = new
string
(asciiChars);
// Display the strings created before and after the
conversion.
Console.WriteLine("Original string: {0}"
,
unicodeString);
Console.WriteLine("Ascii converted string: {0}"
,
asciiString);
}
}
}