JAR文件将创建txt。文件,但不会写入它

时间:2016-05-07 08:16:35

标签: java

我用Jsoup Html解析器创建了UEFA团队生成器。当它作为一个项目运行时,一切都很完美,但是当我构建jar时,它将创建文件,但不会写入它。我听说它有某种保障但我想避免它。有什么想法吗?

代码:

public final class Generator extends JPanel {

    JLabel label;
    File file;
    Font font;
    ArrayList<String> urllist;
    PrintWriter writer;
    Document doc;
    URL url;
    JComboBox<String> seasons; // edited
    String[] seas = {"1999-2000", "2000-2001", "2001-2002", "2002-2003", "2003-2004", "2004-2005", "2005-2006", "2006-2007", "2007-2008", "2008-2009", "2009-2010", "2014-2015", "2015-2016"};

    public Generator(int width, int height) {

        setLayout(null);
        setPreferredSize(new Dimension(width, height));
        seasons = new JComboBox<>(seas);
        font = new Font("Verdana", Font.BOLD, 12);

        label = new JLabel("Choose a season :");
        label.setFont(font);
        label.setBounds(20, 10, 120, 30);
        seasons.setBounds(150, 10, 100, 30);
        add(seasons);
        add(label);

        urllist = new ArrayList<>();
        urllist.add("https://en.wikipedia.org/wiki/1999%E2%80%932000_UEFA_Champions_League_group_stage");
        urllist.add("https://en.wikipedia.org/wiki/2000%E2%80%9301_UEFA_Champions_League_group_stage");
        urllist.add("https://en.wikipedia.org/wiki/2001%E2%80%9302_UEFA_Champions_League_group_stage");
        urllist.add("https://en.wikipedia.org/wiki/2002%E2%80%9303_UEFA_Champions_League_group_stage");
        urllist.add("https://en.wikipedia.org/wiki/2003%E2%80%9304_UEFA_Champions_League_group_stage");
        urllist.add("https://en.wikipedia.org/wiki/2004%E2%80%9305_UEFA_Champions_League_group_stage");
        urllist.add("https://en.wikipedia.org/wiki/2005%E2%80%9306_UEFA_Champions_League_group_stage");
        urllist.add("https://en.wikipedia.org/wiki/2006%E2%80%9307_UEFA_Champions_League_group_stage");
        urllist.add("https://en.wikipedia.org/wiki/2007%E2%80%9308_UEFA_Champions_League_group_stage");
        urllist.add("https://en.wikipedia.org/wiki/2008%E2%80%9309_UEFA_Champions_League_group_stage");
        urllist.add("https://en.wikipedia.org/wiki/2009%E2%80%9310_UEFA_Champions_League_group_stage");
        urllist.add("https://en.wikipedia.org/wiki/2014%E2%80%9315_UEFA_Champions_League_group_stage");
        urllist.add("https://en.wikipedia.org/wiki/2015%E2%80%9316_UEFA_Champions_League_group_stage");
        seasons.addActionListener(new GenerateHandler());

    }

    public void cycle(int start, int end) {

        for (int j = start; j < end; j++) {

            Element table = doc.select("table.wikitable").get(j);
            Elements rows = table.select("tr");

            for (int i = 1; i < 3; i++) {
                writer.println(rows.get(i).select("td").select("a").get(1).attr("Title").replace(" (football)", "") + "," + rows.get(i).select("td").select("a").attr("Title"));

            }

        }
        writer.close();

    }

    public class GenerateHandler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            int i = seasons.getSelectedIndex();
            String foldername = "C:/Users/neutron/Desktop/data/";
            file = new File(foldername, seas[i] + ".txt");
            file.canWrite();

            try {
                url = new URL(urllist.get(i));
            } catch (MalformedURLException ex) {
                Logger.getLogger(Generator.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                doc = Jsoup.parse(url, 3000);
            } catch (IOException ex) {
                Logger.getLogger(Generator.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                writer = new PrintWriter(file, "UTF-8");
            } catch (FileNotFoundException | UnsupportedEncodingException ex) {
                Logger.getLogger(Generator.class.getName()).log(Level.SEVERE, null, ex);
            }

            if (i >= 0 && i < 3) {
                cycle(1, 9);
            } else if ((i > 2 && i < 4)|| (i > 10 && i < 13)) {
                cycle(5, 13);
            } else if (i == 8) {
                cycle(2, 10);
            }
             else {
                cycle(6, 14);
            }

        }

    }

}

..在制作jar时,它不会打印任何警告或错误

1 个答案:

答案 0 :(得分:0)

好的,我通过制作通用的JComboBox来解决它

而不是JComboBox seasons;

我放入了JComboBox<String> seasons;