簡體   English   中英

JAR文件將創建txt。 文件,但不會寫入

[英]JAR file will create txt. file but won't write to it

我已經使用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);
            }

        }

    }

}

..建造罐子時,它不會打印任何警告或錯誤

好了,我通過制作通用JComboBox解決了它

而不是JComboBox seasons;

我在這里放了JComboBox<String> seasons;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM