首頁 考試吧論壇 Exam8視線 考試商城 網(wǎng)絡(luò)課程 模擬考試 考友錄 實用文檔 求職招聘 論文下載
2011中考 | 2011高考 | 2012考研 | 考研培訓(xùn) | 在職研 | 自學(xué)考試 | 成人高考 | 法律碩士 | MBA考試
MPA考試 | 中科院
四六級 | 職稱英語 | 商務(wù)英語 | 公共英語 | 托福 | 雅思 | 專四專八 | 口譯筆譯 | 博思 | GRE GMAT
新概念英語 | 成人英語三級 | 申碩英語 | 攻碩英語 | 職稱日語 | 日語學(xué)習(xí) | 法語 | 德語 | 韓語
計算機等級考試 | 軟件水平考試 | 職稱計算機 | 微軟認(rèn)證 | 思科認(rèn)證 | Oracle認(rèn)證 | Linux認(rèn)證
華為認(rèn)證 | Java認(rèn)證
公務(wù)員 | 報關(guān)員 | 銀行從業(yè)資格 | 證券從業(yè)資格 | 期貨從業(yè)資格 | 司法考試 | 法律顧問 | 導(dǎo)游資格
報檢員 | 教師資格 | 社會工作者 | 外銷員 | 國際商務(wù)師 | 跟單員 | 單證員 | 物流師 | 價格鑒證師
人力資源 | 管理咨詢師考試 | 秘書資格 | 心理咨詢師考試 | 出版專業(yè)資格 | 廣告師職業(yè)水平
駕駛員 | 網(wǎng)絡(luò)編輯
衛(wèi)生資格 | 執(zhí)業(yè)醫(yī)師 | 執(zhí)業(yè)藥師 | 執(zhí)業(yè)護士
會計從業(yè)資格考試會計證) | 經(jīng)濟師 | 會計職稱 | 注冊會計師 | 審計師 | 注冊稅務(wù)師
注冊資產(chǎn)評估師 | 高級會計師 | ACCA | 統(tǒng)計師 | 精算師 | 理財規(guī)劃師 | 國際內(nèi)審師
一級建造師 | 二級建造師 | 造價工程師 | 造價員 | 咨詢工程師 | 監(jiān)理工程師 | 安全工程師
質(zhì)量工程師 | 物業(yè)管理師 | 招標(biāo)師 | 結(jié)構(gòu)工程師 | 建筑師 | 房地產(chǎn)估價師 | 土地估價師 | 巖土師
設(shè)備監(jiān)理師 | 房地產(chǎn)經(jīng)紀(jì)人 | 投資項目管理師 | 土地登記代理人 | 環(huán)境影響評價師 | 環(huán)保工程師
城市規(guī)劃師 | 公路監(jiān)理師 | 公路造價師 | 安全評價師 | 電氣工程師 | 注冊測繪師 | 注冊計量師
繽紛校園 | 實用文檔 | 英語學(xué)習(xí) | 作文大全 | 求職招聘 | 論文下載 | 訪談 | 游戲
您現(xiàn)在的位置: 考試吧(Exam8.com) > 計算機等級考試 > 計算機二級 > JAVA > 復(fù)習(xí)資料 > 正文

2011年計算機等考二級JAVA學(xué)習(xí)精華整理(68)

考試吧整理了2011年計算機的等考二級JAVA學(xué)習(xí)精華,幫助考生備考。

  5. 格式化輸出和指定編碼

  默認(rèn)的輸出方式為緊湊方式,默認(rèn)編碼為UTF-8,但對于我們的應(yīng)用而言,一般都要用到中文,并且希望顯示時按自動縮進的方式的顯示,這就需用到OutputFormat類。

  /**

  * 格式化XML文檔,并解決中文問題

  * @param filename

  * @return

  */

  public int formatXMLFile(String filename){

  int returnValue = 0;

  try{

  SAXReader saxReader = new SAXReader();

  Document document = saxReader.read(new File(filename));

  XMLWriter writer = null;

  /** 格式化輸出,類型IE瀏覽一樣 */

  OutputFormat format = OutputFormat.createPrettyPrint();

  /** 指定XML編碼 */

  format.setEncoding("GBK");

  writer= new XMLWriter(new FileWriter(new File(filename)),format);

  writer.write(document);

  writer.close();

  /** 執(zhí)行成功,需返回1 */

  returnValue = 1;

  }catch(Exception ex){

  ex.printStackTrace();

  }

  return returnValue;

  }

  說明:

  OutputFormat format = OutputFormat.createPrettyPrint();

  這句指定了格式化的方式為縮進式,則非緊湊式。

  format.setEncoding("GBK");

  指定編碼為GBK。

  XMLWriter writer = new XMLWriter(new FileWriter(new File(filename)),format);

  這與前面兩個方法相比,多加了一個OutputFormat對象,用于指定顯示和編碼方式。

  6. 完整的類代碼

  前面提出的方法都是零散的,下面給出完整類代碼。

  Dom4jDemo.java

  package com.holen.dom4j;

  import java.io.File;

  import java.io.FileWriter;

  import java.util.Iterator;

  import java.util.List;

  import org.dom4j.Attribute;

  import org.dom4j.Document;

  import org.dom4j.DocumentHelper;

  import org.dom4j.Element;

  import org.dom4j.io.OutputFormat;

  import org.dom4j.io.SAXReader;

  import org.dom4j.io.XMLWriter;

  /**

  * @author Holen Chen

  */

  public class Dom4jDemo {

  public Dom4jDemo() {

  }

  public int createXMLFile(String filename){…}

  public int ModiXMLFile(String filename,String newfilename){…}

  public int formatXMLFile(String filename){…}

  public static void main(String[] args) {

  Dom4jDemo temp = new Dom4jDemo();

  System.out.println(temp.createXMLFile("d://holen.xml")); System.out.println(temp.ModiXMLFile("d://holen.xml","d://holen2.xml"));

  System.out.println(temp.formatXMLFile("d://holen2.xml"));

  }

  }

  說明:

  main()方法中依次調(diào)用三個方法,第一個方法用于生成holen.xml,第二個方法用于修改holen.xml,并且修改后的內(nèi)容另存為holen2.xml,第三個方法將holen2.xml格式化縮進式輸出,并指定編碼方式為GBK。

  一個應(yīng)用Dom4j的例子

  [ 2005-4-21 ]

  Created with Colorer-take5 Library. Type 'net.sf.colorer.FileType@777255'

  0: /*

  1: * Created on 2005-4-19

  2: *

  3: * Copyright (c) 2005 Julysea

  4: * Window - Preferences - Java - Code Style - Code Templates

  5: */

  6:

  7: /*應(yīng)用此log4j的log4j.properties配置文件

  8: *

  9: *#######################################################################

  10: *# Categories and levels

  11: *#######################################################################

  12: *

  13: *log4j.rootCategory=DEBUG, FileApp, ConApp

  14: *log4j.category.de.jayefem=DEBUG, FileApp, ConApp

  15: *

  16: *#######################################################################

  17: *# Appenders

  18: *#######################################################################

  19: *

  20: *# ConApp is set to be a ConsoleAppender.

  21: *log4j.appender.ConApp=org.apache.log4j.ConsoleAppender

  22: *log4j.appender.ConApp.Target=System.out

  23: *log4j.appender.ConApp.layout=org.apache.log4j.PatternLayout

  24: *log4j.appender.ConApp.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

  25: *

上一頁  1 2 3 4 5 6 7 8 9 10  ... 下一頁  >> 
  相關(guān)推薦:

  計算機等級考試二級Java經(jīng)典算法大全匯總

  2010年9月計算機等級考試成績查詢時間匯總

   2011計算機等級考試二級JAVA學(xué)習(xí)精華匯總

文章搜索
計算機等級考試欄目導(dǎo)航
版權(quán)聲明:如果計算機等級考試網(wǎng)所轉(zhuǎn)載內(nèi)容不慎侵犯了您的權(quán)益,請與我們聯(lián)系800@exam8.com,我們將會及時處理。如轉(zhuǎn)載本計算機等級考試網(wǎng)內(nèi)容,請注明出處。