練功房推薦書單

  • 黑心建商的告白:買屋前不看會哭的17堂課
  • 黑心房仲的告白:買屋簽約前最後救命的17堂課
  • 黑心投資客炒房告白:搞懂中古屋坑錢陷阱的17堂課
  • 猛虎出閘制霸版:最新OCP Java SE 6 Programmer專業認證(附原始程式碼及範例檔)
Messages posted by: crc2121
Forum Index » Profile for crc2121 » Messages posted by crc2121
Message
我有一個問題就是將讀取到且是在螢幕所顯示出來的另存一個.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>
我弄個詢問功能且會顯示出來,如下所示:
C:\JAVA_WORK>javac ReadXMLFile.java

C:\JAVA_WORK>java ReadXMLFile

輸入分類號:0299

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

年度號 : 0085
分類號 : 0299
案次號 : 002

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

C:\JAVA_WORK>


目前的錯誤訊息如下:
C:\JAVA_WORK>java ReadXMLFile
輸入分類號:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""

        at java.lang.NumberFormatException.forInputString(Unknown Source)
        at java.lang.Integer.parseInt(Unknown Source)
        at java.lang.Integer.parseInt(Unknown Source)
        at ReadXMLFile.main(ReadXMLFile.java:20)

C:\JAVA_WORK>


我想設一個[try-catch]機制"找不到的情況",如下所示?
當我輸入一個不存在或沒有該屬於分類號就會印出"查無此分類號!請重新輸入..."。
C:\JAVA_WORK>javac ReadXMLFile.java

C:\JAVA_WORK>java ReadXMLFile

輸入分類號:0419

查無此分類號!請重新輸入...

C:\JAVA_WORK>

懇請指點一下<(_ _)>

程式碼如下:
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 SearchFile {
    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(); 
    int num = Integer.parseInt(s); 

    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();
                   }              
                   
		}
		
             
	  } catch (IOException io) {
		System.out.println(io.getMessage());
	  } catch (JDOMException jdomex) {
		System.out.println(jdomex.getMessage());
	  }
	}
}

xml格式如下:
<?xml version = '1.0' encoding = 'Big5'?>
<!DOCTYPE ROWSET [
<!ELEMENT ROWSET (ROW)*>
<!ELEMENT ROW (年度號, 案名?, 分類號, 案次號 )>
<!ATTLIST ROW          num CDATA #REQUIRED>
<!ATTLIST 電子檔案名稱 num CDATA #REQUIRED>
<!ATTLIST 附件名稱     num CDATA #REQUIRED>
<!ATTLIST 附件媒體型式 num CDATA #REQUIRED>
<!ATTLIST 附件數量     num CDATA #REQUIRED>
<!ATTLIST 附件單位     num CDATA #REQUIRED>
<!ATTLIST 主題         num CDATA #REQUIRED>
<!ATTLIST 附註         num CDATA #REQUIRED>
<!ELEMENT 案名 (#PCDATA)>
<!ELEMENT 年度號 (#PCDATA)>
<!ELEMENT 分類號 (#PCDATA)>
<!ELEMENT 案次號 (#PCDATA)>
]>

<ROWSET>

 <ROW num="1">
  年度號 : 0072
  分類號 : 200
  案次號 : 001
 </ROW>
 <ROW num="2">
  年度號 : 0070
  分類號 : 200
  案次號 : 002
 </ROW>
 <ROW num="3">
  年度號 : 0076
  分類號 : 011
  案次號 : 004
 </ROW>
 <ROW num="4">
  年度號 : 0091
  分類號 : 011
  案次號 : 001
 </ROW>
 <ROW num="5">
  年度號 : 0086
  分類號 : 0102
  案次號 : 053
 </ROW>
 <ROW num="6">
  年度號 : 0081
  分類號 : 012
  案次號 : 023
 </ROW>
 <ROW num="7">
  年度號 : 0071
  分類號 : 112
  案次號 : 013
 </ROW>

</ROWSET>
原本錯了25個,現在錯了12個(應該是有修正),現在是哪邊有問題?


C:\JAVA_WORK>javac ReadXMLFile.java
ReadXMLFile.java:4: package org.jdom does not exist
import org.jdom.Document;
               ^
ReadXMLFile.java:5: package org.jdom does not exist
import org.jdom.Element;
               ^
ReadXMLFile.java:6: package org.jdom does not exist
import org.jdom.JDOMException;
               ^
ReadXMLFile.java:7: package org.jdom.input does not exist
import org.jdom.input.SAXBuilder;
                     ^
ReadXMLFile.java:12: cannot find symbol
symbol  : class SAXBuilder
location: class ReadXMLFile
          SAXBuilder builder = new SAXBuilder();
          ^
ReadXMLFile.java:12: cannot find symbol
symbol  : class SAXBuilder
location: class ReadXMLFile
          SAXBuilder builder = new SAXBuilder();
                                   ^
ReadXMLFile.java:17: cannot find symbol
symbol  : class Document
location: class ReadXMLFile
                Document document = (Document) builder.build(xmlFile);
                ^
ReadXMLFile.java:17: cannot find symbol
symbol  : class Document
location: class ReadXMLFile
                Document document = (Document) builder.build(xmlFile);
                                     ^
ReadXMLFile.java:18: cannot find symbol
symbol  : class Element
location: class ReadXMLFile
                Element rootNode = document.getRootElement();
                ^
ReadXMLFile.java:23: cannot find symbol
symbol  : class Element
location: class ReadXMLFile
                   Element node = (Element) list.get(i);
                   ^
ReadXMLFile.java:23: cannot find symbol
symbol  : class Element
location: class ReadXMLFile
                   Element node = (Element) list.get(i);
                                   ^
ReadXMLFile.java:34: cannot find symbol
symbol  : class JDOMException
location: class ReadXMLFile
          } catch (JDOMException jdomex) {
                   ^
12 errors

C:\JAVA_WORK>


懇請指點一下,謝謝<(_ _)>
現在我只要讀取xml檔案中的標籤(YYY、SSS、NO三個字串),xml檔案格式如下:

<?xml version = '1.0' encoding = 'Big5'?>
<!DOCTYPE ROWSET [
<!ELEMENT ROWSET (ROW)*>
<!ELEMENT ROW (YYY, SSS, NO)> 
<!ATTLIST ROW          num CDATA #REQUIRED>
<!ATTLIST 電子檔案名稱 num CDATA #REQUIRED>
<!ATTLIST 附件名稱     num CDATA #REQUIRED>
<!ATTLIST 附件媒體型式 num CDATA #REQUIRED>
<!ATTLIST 附件數量     num CDATA #REQUIRED>
<!ATTLIST 附件單位     num CDATA #REQUIRED>
<!ATTLIST 主題         num CDATA #REQUIRED>
<!ATTLIST 附註         num CDATA #REQUIRED>
<!ELEMENT YYY (#PCDATA)>
<!ELEMENT SSS (#PCDATA)>
<!ELEMENT NO (#PCDATA)>
]>

<ROWSET>

<ROW num="1">
  <YYY>0080</YYY>
  <SSS>010302</SSS>
  <NO>002</NO>
</ROW>

<ROW num="2">
  <YYY>0078</YYY>
  <SSS>011602</SSS>
  <NO>001</NO>
</ROW>

<ROW num="3">
  <YYY>0070</YYY>
  <SSS>019302</SSS>
  <NO>011</NO>
</ROW>
     ‧
       ‧
     ‧
       ‧
<ROW num="1225">
  <YYY>0099</YYY>
  <SSS>018719</SSS>
  <NO>111</NO>
</ROW>

</ROWSET>


然後我顯示出來我要的資訊及如下程式碼:

-- Output --
年度號: 0080
分類號: 010302
案次號: 002
---------------------------------------
年度號:0078
分類號:011602
案次號:001
---------------------------------------
     ‧
       ‧
       ‧
       ‧
年度號: 0099
分類號: 018719
案次號: 111


import java.io.File;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
 
public class ReadXMLFile {
	public static void main(String[] args) {
 
	  SAXBuilder builder = new SAXBuilder();
	  File xmlFile = new File("C:\\JAVA_WORK\\test3.xml");  //已修正

 
	  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);
 
		   System.out.println("年度號 : " + node.getChildText("YYY"));
		   System.out.println("分類號 : " + node.getChildText("SSS"));
		   System.out.println("案次號 : " + node.getChildText("NO"));
		   System.out.println("----------------------------------------"); 
		}
 
	  } catch (IOException io) {
		System.out.println(io.getMessage());
	  } catch (JDOMException jdomex) {
		System.out.println(jdomex.getMessage());
	  }
	}
}


仍是有錯?有在想是不是沒安裝JDOM與設定路徑?

目前將http://jdom.org/dist/binary/

的jdom-1.1.2.zip擋下載了。

可以請教一下怎麼安裝與設定路徑嗎?

感激不盡<(_ _)>
我用圖來解釋一下好了。

圖中的ACCESS2007建立的資料庫中的test資料表(如紅線上方),透過SQL或是VBA在或者是JAVA都可,能否把圖中的綠藍紫框個別製作成資料表且每個資料表由001一直至做到XXX為止。
http://img200.imageshack.us/f/002wui.jpg/
其中ID欄位重複是和段欄位做分段用。

p.s所有資料型態皆為備忘,如圖:http://img195.imageshack.us/f/001anpk.jpg/
原本是依賴拉物件方式搭SQL去做,後來伺服器掛點,最新一次的備份出來用CSV的;所以想問若是用Office 2007裡面ACCESS,用ACCESS去做每一行(即每一行筆資料)查詢的SQL語法應該怎麼下?或是怎麼寫支程式進去下達?如下:


原始資料表 ALL_TABLE

索引_欄位1_欄位2_欄位3_欄位4

01_XXXX1_YYYY1_ZZZZ1_WWWW1

02_XXXX2_YYYY2_ZZZZ2_WWWW2
.  . .  . .
.  . .  . .
.  . .  . .
.  . .  . .
.  . .  . .
NN_XXXXN_YYYYN_ZZZZN_WWWWN

每一行各別轉如下:

01_TABLE

索引_欄位1_欄位2_欄位3_欄位4

01_XXXX1_YYYY1_ZZZZ1_WWWW1

02_TABLE

索引_欄位1_欄位2_欄位3_欄位4

02_XXXX2_YYYY2_ZZZZ2_WWWW2

........一直到

NN_TABLE

索引_欄位1_欄位2_欄位3_欄位4

NN_XXXXN_YYYYN_ZZZZN_WWWWN


~以上~

有這樣子SQL語法嗎?或是VBA應該怎麼寫?還是java可以做到?

懇請賜教,感激不盡~
我知道我錯哪邊了囧rz...
編碼的屬性是UTF-8應更改為ANSI才對囧rz...
我無法搜尋我要的詞彙(鼻涕),我這是有少打甚麼?為何只會搜尋到0筆資料。
可以給個起手式嗎?

懇請賜教~

Lucene 3.0.0 API
http://www.jarvana.com/jarvana/view/org/apache/lucene/lucene-core/3.0.0/lucene-core-3.0.0-javadoc.jar!/index.html?org/apache/lucene/util/Version.html


[Index code]

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;

public class Indexer
{
   public static void main(String[] args) throws IOException
   {
  String Idx = "C:\\test\\Idx";
  String dateDir = "C:\\test\\Data";
  IndexWriter indexWriter = null;

  Directory dir = new SimpleFSDirectory(new File(Idx));
  indexWriter = new IndexWriter(dir,new StandardAnalyzer(Version.LUCENE_30),true,IndexWriter.MaxFieldLength.UNLIMITED);

  File[] files = new File(dateDir).listFiles();

  for (int i = 0; i < files.length; i++)
  {
  Document doc = new Document();
  doc.add(new Field("contents", new FileReader(files[i])));
  doc.add(new Field("filename", files[i].getName(), Field.Store.YES, Field.Index.NOT_ANALYZED));
  doc.add(new Field("indexDate",DateTools.dateToString(new Date(), DateTools.Resolution.DAY),Field.Store.YES,Field.Index.NOT_ANALYZED));
  indexWriter.addDocument(doc);
  }
  System.out.println("numDocs"+indexWriter.numDocs());
  indexWriter.close();

    }

}
[END]

[Searcher code]

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;

public class Searcher
{
    public static void main(String[] args) throws IOException, ParseException
    {
   String Idx = "C:\\test\\Idx";
   Directory dir = new SimpleFSDirectory(new File(Idx));
   IndexSearcher indexSearch = new IndexSearcher(dir);

   QueryParser queryParser = new QueryParser(Version.LUCENE_30, "contents", new StandardAnalyzer(Version.LUCENE_30));
   Query query = queryParser.parse("鼻涕"); //key Query term
   TopDocs hits = indexSearch.search(query, 500);
   System.out.println("找到"+hits.totalHits+"個");
   for (int i = 0; i < hits.scoreDocs.length; i++)
   {
   ScoreDoc sdoc = hits.scoreDocs[i];
   Document doc = indexSearch.doc(sdoc.doc);
   System.out.println(doc.get("filename"));
   }
   indexSearch.close();
    }
}
[END]


訊息回應如下圖:
http://img263.imageshack.us/f/bug03.jpg/

已修正完~
看書做一個 Lucene 提供的 index 可是... 出現5個bug可是卻不知道是哪邊打錯了?
可以請前輩指導一下嗎?

感激不盡<(_ _)>

圖片如下:
http://img130.imageshack.us/f/bug01.jpg/


程式碼如下:
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.io.FileFilter;
import java.util.Date;
import org.apache.lucene.analysis.*;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;

public class TxtFileIndex
{
       public static void main(String[] args) throws Exception
       {
          File dataDir = new File("C:\\IR\\IR_TEST");
          File indexDir = new File("C:\\IR\\IR_index");

          Analyzer luceneAnalyzer = new StandardAnalyzer();
          File[] dataFiles = dataDir.listFiles();

          IndexWriter indexWriter = new IndexWriter(indexDir,luceneAnalyzer,true);

          long startTime = new Date().getTime();
          for (int i=0;i<dataFiles.length;i++)
          {
              if(dataFiles[i].isFile() && dataFiles[i].getName().endsWith(".txt"))
              {
                    System.out.println("Idx"+dataFiles[i].getCanonicalPath());

                    Document document = Document();
                    Reader txtReader = new FileReader(dataFiles[i]);
                    document.add(new Field("path", dataFiles[i].getCanonicalPath(), Field.Store.YES, Field.Index.UN_TOKENIZED));
                    document.add(new Field("contents, txtReader"));
              }
          }
          indexWriter.optimize();
          indexWriter.close();
          long endTime = new Date().getTime();

          System.out.println("It take "+ (endTime - startTime) + "milliseconds to create index for the files in directory." + dataDir.getPath());

       }
}
 
Forum Index » Profile for crc2121 » Messages posted by crc2121
Go to: