2016-04-27 75 views
-1

我已經創建了一個java應用程序,基本上從網站上剪下數據並創建一個.csv文件。我在NetBeans中執行它時運行良好,並創建了34kb的文件。但是,當我構建它並運行.JAR可執行文件時,它會創建相同的文件,但文件大小爲4kb,並且不會引入所有數據。 我在java 1.8上構建項目。我不明白這個問題究竟是什麼背後的原因。這是Java版本的問題嗎?在此先感謝您的幫助。 很抱歉沒有在這裏添加代碼。這是我的項目的代碼。.JAR可執行文件運行不正確

import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.Iterator; 
import java.util.Locale; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.select.Elements; 

//this program is for open-end-summary 
/** 
* 
* @author talha 
*/ 
public class ComMufapScraping { 

    boolean writeCSVToConsole = true; 
    boolean writeCSVToFile = true; 
    boolean sortTheList = true; 
    boolean writeToConsole; 
    boolean writeToFile; 
    public static Document doc = null; 
    public static Elements tbodyElements = null; 
    public static Elements elements = null; 
    public static Elements tdElements = null; 
    public static Elements trElement2 = null; 
    public static String Dcomma = ","; 
    public static String line = ""; 
    public static ArrayList<Elements> sampleList = new ArrayList<Elements>(); 

    public static void createConnection() throws IOException { 
     System.setProperty("http.proxyHost", "191.1.1.202"); 
     System.setProperty("http.proxyPort", "8080"); 
     String tempUrl = "http://www.mufap.com.pk/nav_returns_performance.php?tab=01"; 
     doc = Jsoup.connect(tempUrl).get(); 
    } 

    public static void parsingHTML() throws Exception { 
     for (Element table : doc.getElementsByTag("table")) { 

      for (Element trElement : table.getElementsByTag("tr")) { 
       trElement2 = trElement.getElementsByTag("tr"); 
       tdElements = trElement.getElementsByTag("td"); 
       File fold = new File("C:\\open-end-smry.csv"); 
       fold.delete(); 
       File fnew = new File("C:\\open-end-smry.csv"); 
       FileWriter sb = new FileWriter(fnew, true); 
       if (trElement.hasClass("tab-data1")) { 
        for (Iterator<Element> it = tdElements.iterator(); it.hasNext();) { 
         if (it.hasNext()) { 
          sb.append("\r\n"); 

         } 

         for (Iterator<Element> it2 = trElement2.iterator(); it.hasNext();) { 
          Element tdElement2 = it.next(); 
          final String content = tdElement2.text(); 
          if (it2.hasNext()) { 

           sb.append(formatData(content)); 
           sb.append(" , "); 

          } 
          if (!it.hasNext()) { 
           String content1 = content.replaceAll(",$", " "); 
           sb.append(formatData(content1)); 
           //it2.next(); 

          } 
         } 

         System.out.println(sb.toString()); 
         sb.flush(); 
         sb.close(); 
        } 
       } 
       System.out.println(sampleList.add(tdElements)); 

      } 
     } 
    } 
    private static final SimpleDateFormat FORMATTER_MMM_d_yyyy = new SimpleDateFormat("MMM d, yyyy", Locale.US); 
    private static final SimpleDateFormat FORMATTER_dd_MMM_yyyy = new SimpleDateFormat("dd-MMM-YYYY", Locale.US); 

    public static String formatData(String text) { 
     String tmp = null; 

     try { 
      Date d = FORMATTER_MMM_d_yyyy.parse(text); 
      tmp = FORMATTER_dd_MMM_yyyy.format(d); 
     } catch (ParseException pe) { 
      tmp = text; 
     } 

     return tmp; 
    } 

    public static void main(String[] args) throws IOException, Exception { 
     createConnection(); 
     parsingHTML(); 

    } 

} 

這是構建項目後的日誌貓文件。

螞蟻-f C:\用戶\塔爾哈\文件\的NetBeansProjects \ com.open_end_smry -Dnb.internal.action.name =重建清潔罐INIT:DEPS清潔:更新屬性文件: C: \ Users \ talha \ Documents \ NetBeansProjects \ com.open_end_smry \ build \ built-clean.properties 刪除目錄 C:\ Users \ talha \ Documents \ NetBeansProjects \ com.open_end_smry \ build clean:init:deps-jar:Created dir: C:\ Users \ talha \ Documents \ NetBeansProjects \ com.open_end_smry \ build 更新屬性文件: C:\ Users \ talha \ Documents \ NetBeansProjects \ com.open_end_smry \ build \ built-jar.prop ERTIES 創建目錄: C:\用戶\塔拉\文檔\的NetBeansProjects \ com.open_end_smry \建立\類 創建目錄: C:\用戶\塔拉\文檔\的NetBeansProjects \ com.open_end_smry \建立\空 創建目錄: C:\ Users \ talha \ Documents \ NetBeansProjects \ com.open_end_smry \ build \ generated-sources \ ap-source-output 將1個源文件編譯爲 C:\ Users \ talha \ Documents \ NetBeansProjects \ com.open_end_smry \ build \ classes 警告:[options]引導類路徑未與 一起設置-source 1.7 1警告編譯:創建目錄:C:\ Users \ talha \ Documents \ NetBeansProjects \ com.open_end_smry \ dist 將1個文件複製到 C:\ Users \用戶塔拉\文檔\氖tBeansProjects \ com.open_end_smry \ build將 庫複製到 C:\ Users \ talha \ Documents \ NetBeansProjects \ com.open_end_smry \ dist \ lib。 構建jar: C:\ Users \ talha \ Documents \ NetBeansProjects \ com.open_end_smry \ dist \ com.open_end_smry.jar 要從沒有Ant的命令行運行此應用程序,請嘗試:java -jar「C:\ Users \塔拉\文檔\的NetBeansProjects \ com.open_end_smry \ DIST \ com.open_end_smry.jar」 罐子:BUILD SUCCESSFUL(總時間:0秒)

+0

你需要把你的項目拷貝到本地機器上。然後在你的控制檯上運行'javac file.java',然後'java File'。如果你已經正確安裝了Java,這應該可以工作。 –

+1

假設所有內容都被正確複製和編譯,我們無法知道您提到的內容出了什麼問題。 –

+0

如果您可以提供[mcve],我們可以嘗試重現該問題。沒有一些代碼,很難幫你 –

回答

1

你有你的循環內的文件刪除功能,所以該文件將在每個循環中刪除。把這些部分放在你的循環之外:

public static void parsingHTML() throws Exception { 
     File fold = new File("C:\\open-end-smry.csv"); 
     fold.delete(); 
     for (Element table : doc.getElementsByTag("table")) { 

      for (Element trElement : table.getElementsByTag("tr")) { 
       trElement2 = trElement.getElementsByTag("tr"); 
       tdElements = trElement.getElementsByTag("td"); 
       File fnew = new File("C:\\open-end-smry.csv"); 
       FileWriter sb = new FileWriter(fnew, true); 
       if (trElement.hasClass("tab-data1")) { 
        for (Iterator<Element> it = tdElements.iterator(); it.hasNext();) { 
         if (it.hasNext()) { 
          sb.append("\r\n"); 

         } 

         for (Iterator<Element> it2 = trElement2.iterator(); it.hasNext();) { 
          Element tdElement2 = it.next(); 
          final String content = tdElement2.text(); 
          if (it2.hasNext()) { 

           sb.append(formatData(content)); 
           sb.append(" , "); 

          } 
          if (!it.hasNext()) { 
           String content1 = content.replaceAll(",$", " "); 
           sb.append(formatData(content1)); 
           //it2.next(); 

          } 
         } 

         System.out.println(sb.toString()); 
         sb.flush(); 
         sb.close(); 
        } 
       } 
       System.out.println(sampleList.add(tdElements)); 

      } 
     } 
    }