練功房推薦書單

  • Google!Android 3手機應用程式設計入門(第四版)
  • 賈伯斯傳(軟皮精裝版)
  • 猛虎出閘制霸版:最新OCP Java SE 6 Programmer專業認證(附原始程式碼及範例檔)
  • SCWCD 5 猛虎出閘:Java Web 應用程式專業認證
如何用程式寫出一個文字檔案,編碼為Big-5  XML
Forum Index » 網頁程式設計 Web Development
Author Message
crc2121

九級學員

Joined: 2011-01-17 16:28:20
Messages: 10
Offline

我有一個問題就是將讀取到且是在螢幕所顯示出來的另存一個.txt檔案要如何做?
我參考範例文件:http://pastie.org/3262724
仍然兜不太出來,懇請指點<(_ _)>

我的程式碼:
import java.io.*;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class WriteFile {
    public static void main(String[] args) throws IOException {
    SAXBuilder builder = new SAXBuilder();
    File xmlFile = new File("C:\\JAVA_WORK\\test1.xml");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("請輸入分類號:"); 
    String s = br.readLine();
    System.out.print("------------------------------------------\n");
    boolean found = false;
    FileWriter fw;
    String  Signal="";
    String lineSep = System.getProperty("line.separator");

    if (s != null) {
             try {
                       Document document = (Document) builder.build(xmlFile);
                       Element rootNode = document.getRootElement();
                       List list = rootNode.getChildren("ROW");

                       for (int i = 0; i < list.size(); i++) {
                          Element node = (Element) list.get(i);
                           if (node.getChildText("分類號").equals(s)) {
                                 System.out.println("年度號:" + node.getChildText("年度號"));
                                 System.out.println("分類號:" + node.getChildText("分類號"));
                                 System.out.println("案次號:" + node.getChildText("案次號"));
                                 Scanner input = new Scanner(System.in);		      
                                 input.hasNextLine();
                                 found = true;
                            }
                        }
                        fw= new FileWriter("c:\\JAVA_WORK\\T02.txt");
                        fw.write(Signal);
                        fw.close();
                        Signal += "年度號:" + node.getChildText("年度號")+lineSep+ "分類號:" + node.getChildText("分類號")+lineSep+ "案次號:" + node.getChildText("案次號")+lineSep;
                        } catch(Exception e) {
                           System.out.println("發生" + e + "例外無法寫檔!");  	 
                        } catch (IOException io) {
                           System.out.println(io.getMessage());
                        } catch (JDOMException jdomex) {
                           System.out.println(jdomex.getMessage());
                        } 
                    }
                       if (!found) {
                            System.out.println("\n查無此分類號!請重新輸入...");
                       } else {System.out.println("\n搜尋結束...");}
               }  
}

而我只要寫入僅三項年度號、分類號和案次號;存檔目的地:C:\JAVA_WORK\Write01.txt
執行結果示意圖如下:
C:\JAVA_WORK\JAVA\HW> javac SearchXML.java

C:\JAVA_WORK\JAVA\HW> java SearchXMLFile
請輸入分類號:0299

年度號:70
分類號:0299
案次號:001

年度號:79
分類號:0299
案次號:011

年度號:78
分類號:0299
案次號:013

搜尋完畢...

C:\JAVA_WORK\JAVA\HW> 

然後存檔為.txt檔案,裡面的中文件如下:
第一筆...
年度號:70
分類號:0299
案次號:001

第二筆...
年度號:79
分類號:0299
案次號:011

第三筆...
年度號:78
分類號:0299
案次號:013

以上三筆資料。
錯誤訊息如下:
C:\JAVA_WORK\JAVA\HW\Jwork>javac WriteFile.java
WriteFile.java:52: cannot find symbol
symbol  : variable node
location: class WriteFile
Signal += "年度號:" + node.getChildText("年度號")+lineSep+ "分類號:" + node.getChildText("分類號")+lineSep+ "案次號:" + node.getChildText("案次號")+lineSep;
                                            ^
WriteFile.java:52: cannot find symbol
symbol  : variable node
location: class WriteFile
Signal += "年度號:" + node.getChildText("年度號")+lineSep+ "分類號:" + node.getChildText("分類號")+lineSep+ "案次號:" + node.getChildText("案次號")+lineSep;
       ^
WriteFile.java:52: cannot find symbol
symbol  : variable node
location: class WriteFile
Signal += "年度號:" + node.getChildText("年度號")+lineSep+ "分類號:" + node.getChildText("分類號")+lineSep+ "案次號:" + node.getChildText("案次號")+lineSep;
                                                ^
3 errors

C:\JAVA_WORK\JAVA\HW\Jwork>

This message was edited 9 times. Last update was at 2012-01-28 09:21:28

bious3927

十級學員

Joined: 2011-06-02 11:14:41
Messages: 2
Offline

Signal += "年度號:" + node.getChildText("年度號")+lineSep+ "分類號:" + node.getChildText("分類號")+lineSep+ "案次號:" + node.getChildText("案次號")+lineSep;


區域變數 node 已超出使用範圍拉, 只能用在for回圈中!!

This message was edited 1 time. Last update was at 2012-02-02 11:54:32

 
Forum Index » 網頁程式設計 Web Development
Go to: