我目前正在开发一个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编程…
哪个值有'fileName'变量?也许你应该使用JFileChooser。
http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html在给java文件路径时使用双斜杠吗?例如,不用
D:JavaEncryptFile.txt
应该是
D:\Java\EncryptFile.txt
因为反斜杠是Java中的转义字符