0

我目前正在开发一个 java 1.4.2 GUI 文件读取/保存/加密程序。由于它超过 500 行,我不会在这里发布整个内容......我将发布我遇到问题的部分。我遇到的问题是加密功能,弹出框输入您要加密的文件的名称,但是无论您输入什么,在您有机会输入之前都会收到 FileNotFound 异常文件名。打开和保存功能工作正常,它只是加密部分。

这里是:

public void encrypt() throws IOException
{

        openWin = new JFrame("Encrypt File");

        Container openFile = openWin.getContentPane();


        JLabel L1;
        JPanel panel = new JPanel();
        JButton encryptButton;

        L1 = new JLabel ("Choose File to Encrypt: ");

        panel.add(L1);
        output = new JTextField(20);
        panel.add(output);

        encryptButton = new JButton("Encrypt File");
        encryptButton.addActionListener(this);
        panel.add(encryptButton);

        openFile.add(panel);

        openWin.setBounds(50,100,400,150);
        openWin.setVisible(true);


    //Get the current content pane
    contentPane = this.getContentPane();

    //refresh the content pane
    if(mainPanel !=null)
        contentPane.remove(mainPanel);

    //Create a new mainPanel
    mainPanel = new JPanel();
        //We need a buffered reader to read the file
    BufferedReader in = new BufferedReader(new FileReader(fileName));

    //Temp will hold heach line read in, text will be the final string
    String temp="";
    String text="";


    //read the first line
    temp = in.readLine();

    int length = temp.length();

    String encrypted = in.readLine();

    int index = temp.length() - 1;

    //loop until the file has ended
    while(index >= 0)
    {

        encrypted = encrypted + temp.charAt(index);

        index--;

        //read another line
        temp = in.readLine();
    }

    //create the text area.
    //send it (String data, height, width)

    page = new JTextArea(text,30,50);
    //Set line wrap to true, other wise it would just be one looooong line
    page.setLineWrap(true);

    //Here is where we create our scroll pane.
    scrollpane = new JScrollPane(page);


    //the page is connected to the scrollpane, the scrollpane gets connected to the mainPanel
    mainPanel.add(scrollpane);

    //The mainPanel is connected to the contentPane
    contentPane.add(mainPanel);

    //refresh the JFrame!
    validate();

}

任何想法我做错了什么?这是我第一次使用 GUI 编程的经验。

4

2 回答 2

1

在给 java 文件路径时,你使用双斜杠吗?例如,而不是

D:\Java\EncryptFile.txt

它应该是

D:\\Java\\EncryptFile.txt

因为反斜杠是Java中的转义字符。

于 2013-05-29T19:26:29.723 回答
1

哪个值具有“文件名”变量?也许您应该使用 JFileChooser。

http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html

于 2013-05-29T19:24:17.840 回答