【问题标题】:Java - File Reading after exporting to executable jar file in eclipseJava - 在 Eclipse 中导出到可执行 jar 文件后的文件读取
【发布时间】:2015-09-16 16:15:45
【问题描述】:

在 Eclipse 中,当我将代码导出到可执行 jar 文件时,jar 找不到任何文件。我使用的代码是
FileReader fr = new FileReader("src/pkg/Password.txt"); BufferedReader br = new BufferedReader(fr); 那是生成一个FileNotFoundException
注意:此代码在 Eclipse 中运行良好。 有没有办法在不使用 .getResource().getResourseAsStream() 的情况下正确执行此操作,因为 FileReader 没有带有 URL 或 InputStream 的构造函数

这是我的一类代码:

package pkg;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class MainP extends JPanel {

/**
 * 
 */
private JButton btnAddCoupon, btnChangeStarCount;
private static final long serialVersionUID = 2669362135801409216L;
private JPasswordField passwordField;
static String s;

/**
 * Create the panel.
 */
public MainP() {
    GridBagLayout gridBagLayout = new GridBagLayout();
    gridBagLayout.columnWidths = new int[] { 245, 94, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    gridBagLayout.rowHeights = new int[] { 49, 0, 0, 0, 0, 0 };
    gridBagLayout.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
    gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
            Double.MIN_VALUE };
    setLayout(gridBagLayout);

            JButton btnCashIn = new JButton("Cash In");
            btnCashIn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    MainClass.f2.setVisible(true);
                    MainClass.f.setVisible(false);

                }
            });
            GridBagConstraints gbc_btnCashIn = new GridBagConstraints();
            gbc_btnCashIn.fill = GridBagConstraints.BOTH;
            gbc_btnCashIn.insets = new Insets(0, 0, 5, 5);
            gbc_btnCashIn.gridx = 0;
            gbc_btnCashIn.gridy = 0;
            add(btnCashIn, gbc_btnCashIn);

    JLabel lblAdminrequiresPassword = new JLabel(
            "Admin (Requires Password)");
    GridBagConstraints gbc_lblAdminrequiresPassword = new GridBagConstraints();
    gbc_lblAdminrequiresPassword.insets = new Insets(0, 0, 5, 0);
    gbc_lblAdminrequiresPassword.gridx = 9;
    gbc_lblAdminrequiresPassword.gridy = 0;
    add(lblAdminrequiresPassword, gbc_lblAdminrequiresPassword);

    btnAddCoupon = new JButton("Add Coupon");
    btnAddCoupon.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            MainClass.f3.setVisible(true);
            MainClass.f.setVisible(false);
        }
    });
    GridBagConstraints gbc_btnAddCoupon = new GridBagConstraints();
    gbc_btnAddCoupon.insets = new Insets(0, 0, 5, 0);
    gbc_btnAddCoupon.gridx = 9;
    gbc_btnAddCoupon.gridy = 1;
    add(btnAddCoupon, gbc_btnAddCoupon);

    btnChangeStarCount = new JButton("Change Star Count");
    btnChangeStarCount.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            MainClass.f4.setVisible(true);
            MainClass.f.setVisible(false);
        }
    });
    GridBagConstraints gbc_btnChangeStarCount = new GridBagConstraints();
    gbc_btnChangeStarCount.insets = new Insets(0, 0, 5, 0);
    gbc_btnChangeStarCount.gridx = 9;
    gbc_btnChangeStarCount.gridy = 2;
    add(btnChangeStarCount, gbc_btnChangeStarCount);
    enab(false);
    passwordField = new JPasswordField();
    passwordField.setForeground(Color.red);
    passwordField.getDocument().addDocumentListener(new DocumentListener() {

        @Override
        public void removeUpdate(DocumentEvent e) {
            try {
                BufferedReader br = new BufferedReader(new FileReader("/pkg/Password.txt"));
                s = br.readLine();
                if (passwordField.getText().equals(s)) {
                    passwordField.setForeground(Color.green);
                    enab(true);
                } else {
                    passwordField.setForeground(Color.red);
                    enab(false);
                }
                br.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            try {
                BufferedReader br = new BufferedReader(new FileReader("/pkg/Password.txt"));
                s = br.readLine();
                if (passwordField.getText().equals(s)) {
                    passwordField.setForeground(Color.green);
                    enab(true);
                } else {
                    passwordField.setForeground(Color.red);
                    enab(false);
                }
                br.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            // XXX Do not change XXX
        }
    });
    GridBagConstraints gbc_passwordField = new GridBagConstraints();
    gbc_passwordField.insets = new Insets(0, 0, 5, 0);
    gbc_passwordField.fill = GridBagConstraints.HORIZONTAL;
    gbc_passwordField.gridx = 9;
    gbc_passwordField.gridy = 3;
    add(passwordField, gbc_passwordField);

    JLabel lblPasswordField = new JLabel("Password Field");

    GridBagConstraints gbc_lblPasswordField = new GridBagConstraints();
    gbc_lblPasswordField.gridx = 9;
    gbc_lblPasswordField.gridy = 4;
    add(lblPasswordField, gbc_lblPasswordField);

}

private void enab(boolean b) {
    btnAddCoupon.setEnabled(b);
    btnChangeStarCount.setEnabled(b);
}

}

注意:我尝试同时使用FileReaderInputStreamReader

目录树:

MyJar.jar (C://Users/Orion31/Desktop/MyJar.jar | ---- pkg | --- Password.txt | --- StarCount.txt | --- CS.txt

【问题讨论】:

    标签: java eclipse java-io


    【解决方案1】:

    永远不要在任何路径中引用src,一旦程序导出,它就不会存在。

    所有资源都存储为 zip 文件(Jar 文件)中的条目,这意味着您不能再像现在这样使用任何文件访问方法,而是需要使用 Class#getResourceClass#getResourcesStream 代替

    有没有办法在不使用 .getResource() 或 .getResourseAsStream() 的情况下正确执行此操作

    没有(或者至少没有我愿意讨论的)

    因为 FileReader 没有带有 URL 或 InputStream 的构造函数

    其实有,或者更重要的是,有一种方法,你可以用InputStreamReader代替FileReader,例如...

    try (BufferedReader reader = new InputStreamReader(getClass().getResourceAsStream("/pkg/Password.txt"))) {
        // Read as normal
    } catch (IOException exp) {
        exp.printStackTrace();
    }
    

    【讨论】:

    • 好的,谢谢,我会试试的。写文件是InputStreamWriter吗?
    • 不,应该是OutputStreamWriter,但您不能写入资源
    • 按资源,你的意思是不能写入文本文件吗?
    • 不,我的意思是那些嵌入在 Jar 文件中的 ;)
    • 嵌入式是什么意思?而且,当使用OutputStreamWriter 时,我使用什么构造函数,因为#getResource#getResourceAsStream 都不起作用
    猜你喜欢
    • 2014-08-19
    • 1970-01-01
    • 2016-08-15
    • 2010-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    相关资源
    最近更新 更多