I have created a java application which basically scraps data from a website and create a .csv file. It is running fine when I execute it in NetBeans and creates file of 34kb. But, when I build it and run the .JAR executable file it creates the same file but size of the file is 4kb and not bringing all the data. I build the project on java 1.8. I don't understand this issue actually what the reason behind it. Is it the problem with the java version? Thanks in advance for your help. sorry for not adding the code here earlier. here's the code of my project.
我创建了一个java应用程序,它基本上从网站上删除数据并创建一个.csv文件。当我在NetBeans中执行它并创建34kb的文件时,它运行正常。但是,当我构建它并运行.JAR可执行文件时,它会创建相同的文件,但文件大小为4kb而不会带来所有数据。我在java 1.8上构建项目。我实际上不明白这个问题背后的原因是什么。这是java版本的问题吗?在此先感谢您的帮助。很抱歉没有在此处添加代码。这是我项目的代码。
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();
}
}
here is the log cat file after building the project.
这是构建项目后的日志cat文件。
ant -f C:\Users\talha\Documents\NetBeansProjects\com.open_end_smry -Dnb.internal.action.name=rebuild clean jar init: deps-clean: Updating property file: C:\Users\talha\Documents\NetBeansProjects\com.open_end_smry\build\built-clean.properties Deleting directory 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 Updating property file: C:\Users\talha\Documents\NetBeansProjects\com.open_end_smry\build\built-jar.properties Created dir: C:\Users\talha\Documents\NetBeansProjects\com.open_end_smry\build\classes Created dir: C:\Users\talha\Documents\NetBeansProjects\com.open_end_smry\build\empty Created dir: C:\Users\talha\Documents\NetBeansProjects\com.open_end_smry\build\generated-sources\ap-source-output Compiling 1 source file to C:\Users\talha\Documents\NetBeansProjects\com.open_end_smry\build\classes warning: [options] bootstrap class path not set in conjunction with -source 1.7 1 warning compile: Created dir: C:\Users\talha\Documents\NetBeansProjects\com.open_end_smry\dist Copying 1 file to C:\Users\talha\Documents\NetBeansProjects\com.open_end_smry\build Copy libraries to C:\Users\talha\Documents\NetBeansProjects\com.open_end_smry\dist\lib. Building jar: C:\Users\talha\Documents\NetBeansProjects\com.open_end_smry\dist\com.open_end_smry.jar To run this application from the command line without Ant, try: java -jar "C:\Users\talha\Documents\NetBeansProjects\com.open_end_smry\dist\com.open_end_smry.jar" jar: BUILD SUCCESSFUL (total time: 0 seconds)
ant -f C:\ Users \ talha \ Documents \ NetBeansProjects \ com.open_end_smry -Dnb.internal.action.name = rebuild clean jar init:deps-clean:更新属性文件: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:创建的目录:C:\ Users \ talha \ Documents \ NetBeansProjects \ com.open_end_smry \ build更新属性文件:C:\ Users \ talha \ Documents \ NetBeansProjects \ com.open_end_smry \ build \ built-jar.properties创建的目录:C:\ Users \ talha \ Documents \ NetBeansProjects \ com.open_end_smry \ build \ classes创建的目录:C:\ Users \ talha \ Documents \ NetBeansProjects \ com.open_end_smry \ build \ empty创建的目录: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] bootstrap类路径未与-sour一起设置ce 1.7 1 warning compile:创建的目录:C:\ Users \ talha \ Documents \ NetBeansProjects \ com.open_end_smry \ dist将1个文件复制到C:\ Users \ talha \ Documents \ NetBeansProjects \ com.open_end_smry \ build将库复制到C: \用户\塔拉\文档\的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 \ talha \ Documents \ NetBeansProjects \ com.open_end_smry \ dist \ com.open_end_smry.jar“jar:BUILD SUCCESSFUL(总时间:0秒)
1 个解决方案
#1
1
You have the delete function of the file inside your loop, so the file will deleted in every Loop. Put These part outside of your Loop:
你在循环中有文件的删除功能,因此每个循环中都会删除该文件。把这些部分放在你的循环之外:
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));
}
}
}
#1
1
You have the delete function of the file inside your loop, so the file will deleted in every Loop. Put These part outside of your Loop:
你在循环中有文件的删除功能,因此每个循环中都会删除该文件。把这些部分放在你的循环之外:
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));
}
}
}