在Scr根目录下创建文件夹的文本文档,存储信息
package com.soft.test;import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import
import
import
import
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class FileSave extends JFrame implements ActionListener{
private CardLayout card;
private JPanel jPanelBtn,jPanelSave;
private JButton saveBtn,inBtn;
private JTextField nameField;
public FileSave(){
init();
setTitle(" 图书管理");
setSize(600,500);
setLocationRelativeTo(null);
setVisible(true );
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public void init(){
jPanelBtn=new JPanel(new FlowLayout(FlowLayout.CENTER));
saveBtn=new JButton("保存");
saveBtn.addActionListener(this);
inBtn=new JButton("查询");
jPanelBtn.add(saveBtn);
jPanelBtn.add(inBtn);
jPanelSave=new JPanel(new GridLayout(5,2));
jPanelSave.setBackground(Color.LIGHT_GRAY);
//this.setLayout(new GridBagLayout());
JLabel nameLab=new JLabel("图书名称:");
JTextField nameField=new JTextField();
JLabel kindLab=new JLabel("图书类别:");
JTextField kindField=new JTextField();
JLabel priceLab=new JLabel("图书位置:");
JTextField priceField=new JTextField();
jPanelSave.add(nameLab);
jPanelSave.add(nameField);
jPanelSave.add(kindLab);
jPanelSave.add(kindField);
jPanelSave.add(priceLab);
jPanelSave.add(priceField);
add(jPanelSave,BorderLayout.CENTER);
add(jPanelBtn, BorderLayout.SOUTH);
}
public static void main(String[] args) {
new FileSave();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub saveBtn.addActionListener(new ActionListener() {
if(e.getSource()==saveBtn){
File file=new File("Dora/MyFile");
String a=nameField.getText();//***********************wrong
try { OutputStream out=new FileOutputStream(file);
byte []b=a.getBytes();
out.write(b);
out.close();
} catch (IOException e2) {
System.err.println("error");
}
}
else if(e.getSource()==inBtn){
new FileOut();
}
}
}
在scr根目录下创建了一个Dora文件夹,在文件夹里创建了一个文本文档MyFile,按保存的时候执行错误,不知怎么改