Properties檔裡有中文怎麼辦?

當Properties檔有中文時,如果直接讀取一定會發現亂碼,此時怎麼辦?
Java提供了一個編碼的小程式, 可以幫我們將檔案裡的文字轉成unicode,這樣就沒問題了! 這個小程式叫作 native2ascii,範例如下:
1. 編一個檔案C:/abc.properties.big5如下:
Item1=Value1
Item2=Value2
Item3=中文值
2. 執行編碼轉換:
native2ascii -encoding Big5 abc.properties.big5 abc.properties
3. 寫一個測試程式:
import java.io.*;
import java.util.*;

public class Encode {

public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream(“C:/abc.properties”);
Properties p = new Properties();
p.load(fis);
System.out.println(p.getProperty(“Item1”));
System.out.println(p.getProperty(“Item2”));
System.out.println(p.getProperty(“Item3”));
}
catch (IOException ie) {
ie.printStackTrace();
}
}
}
4. 執行結果
Value1
Value2
中文值

[轉貼] “struts中文問題”,”struts國際化問題”——終極解決方案

----------------------------------------
引文
----------------------------------------
說實話,你們的方法都做的複雜了,Java本身就支援多國語言編碼,不需要寫任何程式,可以很簡單的
實現。
秘訣就是兩點:

1、所有HTML/JSP頁面全部採用UTF-8編碼
2、用戶端流覽器完全支援UTF-8編碼

步驟:

1、首先把所有的HTML/JSP的ContentType都設為UTF-8
2、然後對於JSP程式中的非ASCII碼提示資訊都不應該寫在程式裏面,都應該放在
application.properties裏面統一管理。
3、對HTML用native2ascii工具統一做一次處理,把HTML中的非ASCII碼都轉換為Unicode編碼。
4、針對不同的語言,寫不同的application.properties,比如說簡體中文是
application_zh_CN.properties,繁體中文是application_zh_TW.properties這樣,然後對這些配置信
息檔同樣用native2ascii工具處理一次,把非ASCII碼統統轉為Unicode編碼。
5、在Servlet的request.getCharacterEncoding()獲得用戶端的作業系統默認編碼,然後set到Struts
的HTTPSession的Locale中。

OK!現在不同的客戶訪問,就會顯示不同的語言版本了。你可以看看此時你的流覽器的字元集,就是
UTF-8。現在你的網站和Google一樣了,嘿嘿,其實你有心的話,看看你的流覽器訪問Google的時候是
什麼字元集吧
切記:所有的HTML/JSP都要設為UTF-8編碼,所有的檔中的非ASCII碼字元都要用native2ascii工具轉
為用ASCII表示的Unicode編碼。

----------------------------------------
原創
----------------------------------------
上 面所述是我從網上下的一篇于中文問題的解決方案,確切的說應該是關於Struts的國際化問題,
下面我結合我的實踐談談具體如何實現Struts的國際化 問題,我對理論不是非常精通,
我只能完全憑自己的理解和實踐來講述,所以下面講的內容可能不是非常正確,
還請大家原諒。但有一點可以肯定,我通過自己的努 力解決了Struts的中文問題,
並實現Struts的國際化,其實一切並不複雜,下面是具體步驟:
0.遇到的問題(這些問題也許不會同時出現)

a.中文資料從資料庫中到jsp中後就變成了”????”
b.做好的中文properties檔,其中的中文value在頁面顯示亂碼
c.jsp檔中的中文到流覽器後顯示時也是亂碼(建議不要在jsp檔中輸入中文,儘量放在properties檔中)
d.由jsp傳給bean的中文值,再由bean傳回頁面又是亂碼
e.當更換本地流覽器的語言選項時,Web應用程式不能自動根據你的locale選擇合適的*.properties文件。導致Web應用程式不能國際化。

1.環境:

Web伺服器: Tomcat 5.0.19
作業系統: Win2000 Server
JVM : jdk 1.4
數 據 庫: Oracle 8.1.7
開發工具: struts studio 5.2 pro for eclipse

2.先將所有*.jsp 網頁中開頭處加入

<%@ page language=”java” contentType=”text/html; charset=utf-8″ %>
再設置<html:html locale = “true”>

3.然後編輯好兩個*.properties檔,放在classes檔夾下你指定的地方,這裏是放在/web-inf/classes/com/wiley 下,它們分別是:

ApplicationResources.properties (英文資源檔案)
ApplicationResources_zh.properties (中文資源檔案)
隨便用什麼工具編寫都行啊!

4.將ApplicationResources_zh.properties轉碼成gb2312。上面引文說要轉成UTF-8,結果我試了,不行。轉成gb2312就行了,操作是。

將ApplicationResources_zh.properties更名為ApplicationResources_xx.properties
在DOS命令行進入ApplicationResources_xx.properties所在的文件夾
使用命令:native2ascii -encoding gb2312 ApplicationResources_xx.properties ApplicationResources_zh.properties(至於你為什麼會出現“native2ascii不是內部命令”,,請查其他資 料,可能你要設置環境變數,因為他是jdk的檔夾bin下的一個應用程式)

5.接下來配置struts-config.xml,很簡單,我們加入:

<message-resources parameter=”com.wiley.ApplicationResources”/> 就行了;
到此已能解決大多數中文問題。如上面所說的a,b,e 現在打開流覽器,選擇功能表:工具》internet選項》語言,將“中文-中國[zh-cn]”刪掉,添加一個“英語-英國[zh-gb]”確定後,重啟 Tomcat,輸入網址你就會發現,你的頁面的文本資訊就會用的是ApplicationResources.properties (英文資源檔案)中的內容。如果換回“中文-中國[zh-cn]”,它就會顯示ApplicationResources_zh.properties (中文資源檔案)中的中文內容。

至於問題“c.jsp檔中的中文到流覽器後顯示時也是亂碼” 你就要用與第4步類似的方法來重新對*.jsp 檔編碼,這時-encoding的參數就要用UTF-8了,如果你用的也是struts studio 5.2 pro for eclipse工具,這一步就免了。它會自動用UTF-8的格式存儲。
至於問題“d.由jsp傳給bean的中文值,再由bean傳回頁面又是亂碼”的解決,我只是加了個篩檢程式。
你可以現在web.xml中加入:
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.wiley.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>

然後在你指定的包內加個java文件 我放在了/web-inf/classes/com/wiley 裏,下面是源代碼:
/*
* XP Forum
*
* Copyright (c) 2002-2003 RedSoft Group. All rights reserved.
*
*/
package com.huahang.tj.struts.filters;
import javax.servlet.*;
import java.io.IOException;
/**
* <p>Filter that sets the character encoding to be used in parsing the
* incoming request, either unconditionally or only if the client did not
* specify a character encoding. Configuration of this filter is based on
* the following initialization parameters:</p>
* <ul>
* <li><strong>encoding</strong> – The character encoding to be configured
* for this request, either conditionally or unconditionally based on
* the <code>ignore</code> initialization parameter. This parameter
* is required, so there is no default.</li>
* <li><strong>ignore</strong> – If set to “true”, any character encoding
* specified by the client is ignored, and the value returned by the
* <code>selectEncoding()</code> method is set. If set to “false,
* <code>selectEncoding()</code> is called <strong>only</strong> if the
* client has not already specified an encoding. By default, this
* parameter is set to “true”.</li>
* </ul>
*
* <p>Although this filter can be used unchanged, it is also easy to
* subclass it and make the <code>selectEncoding()</code> method more
* intelligent about what encoding to choose, based on characteristics of
* the incoming request (such as the values of the <code>Accept-Language</code>
* and <code>User-Agent</code> headers, or a value stashed in the current
* user’s session.</p>
*
* @author <a href=”mailto:jwtronics@yahoo.com”>John Wong</a>
*
* @version $Id: SetCharacterEncodingFilter.java,v 1.1 2002/04/10 13:59:27 johnwong Exp $
*/
public class SetCharacterEncodingFilter implements Filter {
// —————————————————– Instance Variables

/**
* The default character encoding to set for requests that pass through
* this filter.
*/
protected String encoding = null;

/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;

/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;

// ——————————————————— Public Methods

/**
* Take this filter out of service.
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}

/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
// Pass control on to the next filter
chain.doFilter(request, response);
}

/**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter(“encoding”);
String value = filterConfig.getInitParameter(“ignore”);
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase(“true”))
this.ignore = true;
else if (value.equalsIgnoreCase(“yes”))
this.ignore = true;
else
this.ignore = false;
}

// —————————————————— Protected Methods

/**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}//EOC

到此我遇到的中文問題已全部得到解決,並從中理解到struts的國際化的深刻含義。
我個人覺得struts作為一個功能強大的應用框架,應該早就考慮到它的國際化問題,並在實際應用中不會很複雜,
只要我們遵循一些規則,就可以盡情享受struts給我們帶來的無窮樂趣。希望以上所述對大家有所幫助。

[轉貼] Visual Studio 2005 鍵盤快速鍵列表下載

作者 htchien

微軟的程式開發文件產品經理 Rob Caron 在 1 月 30 日發表了 Visual Studio 2005 的鍵盤快速鍵列表檔案並開放下載,我想應該會對使用 Visual Studio 開發軟體的人有一些幫助吧。

如果有興趣看看或是下載這些快速鍵列表檔案的話,可以到 Rob Caron 的部落格去看看:

http://blogs.msdn.com/robcaron/archive/2007/01/29/1552795.aspx

我,與我的同事

http://www.pcdvd.com.tw/showpost.php?p=1079592581&postcount=427
第一話 ~ 第二十話 快速連結

前言第一話 相遇第二話 不好的開始第三話 拒絕第四話 合作的開始第五話  女朋友

第六話 信任第七話 好朋友?第八話 停電第九話 週末第十話 句點,也是逗點

番外雜記第十一話 尾牙第十二話 目光第十三話 青鳥第十四話 新年快樂第十五話 開工

第十六話 壞人第十七話 自責第十八話 惡言第十九話 宵夜第二十話 眼淚的真相中場時間

什麼是OCR?

OCR的原文為「Optical Character Recognize」,翻成中文為「光學字型辨識技術」,
OCR技術的功能就是要將原本寫在紙上的文件透過數位掃描、圖像輸入、版面分析、字元切割、字元識別…等等流程,
將原本寫在紙上的文字,一個一個比對、辨識出來,然後轉換為電腦中所使用的字型與可編修的文字格式。
OCR的光學文件辨識技術可以幫我們將報紙、書本、雜誌或者各類公文中的中文、英文、數字、符號…等等,
都轉換成電腦專用的文字格式,方便我們直接將這些圖文轉到電腦、網路上編輯、流傳,達成快速數位化的目標。

OCR軟體有很多,一般常見的專門處理OCR文件辨識工作的軟體有「丹青文件辨識系統」、「丹青名片辨識系統」、
「蒙恬認識王」、「蒙恬名片通訊王」…等等,透過這類軟體與蒙恬名片通訊王文件掃瞄器搭配使用,
可以幫我們將報章中的文字轉成電子檔,甚至還可自動辨識/轉換字型、自動欄位分析、影像修正…等,
技術相當成熟功能也非常的方便。

傷心+不甘心

我到底算是什麼呀!!這種感覺、這種想法真的是很糟…
佔有慾,期待感,充斥其中….
不過發展的並不盡如我意….

騎車回家的路上,肚子是餓的,身體在顫抖,心裡卻很亢奮,
記得上次有這種感覺,已是近2年前的事了,
那時考MCSD時就差及格700分一點點而沒過,
那刻我就有著類似的感受,
回家後我也沒什麼吃,就拿著球出門,
我需要發洩的管道,我想忘記這一刻,
MSN上記錄著”傷心以及不甘心”