我目前正在研究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编程的经历。
答案 0 :(得分:1)
哪个值有'fileName'变量?也许你应该使用JFileChooser。
http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html
答案 1 :(得分:1)
给java文件的路径时是否使用双斜杠? 例如,而不是
D:\Java\EncryptFile.txt
应该是
D:\\Java\\EncryptFile.txt
因为反斜杠是Java中的转义字符。