/**
*
* @author ALbert
* @category 文件的打开和保存
*
*/
public class FileSaveOpen extends JFrame{
JTextArea area=null;
public FileSaveOpen(){
setBounds(300, 200, 600, 400);
JMenuBar bar=new JMenuBar();
JMenu jm=new JMenu();
jm.setText("文件");
JMenuItem j1=new JMenuItem();
j1.setText("保存");
JMenuItem j2=new JMenuItem();
j2.setText("打开");
jm.add(j1);
jm.add(j2);
bar.add(jm);
area=new JTextArea();
getContentPane().add(bar,BorderLayout.NORTH);
getContentPane().add(area);
//getContentPane().setLayout(null);
j1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fileSave();
}
});
j2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fileOpen();
}
});
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void fileSave(){
JFileChooser jfc=new JFileChooser();
int result=jfc.showOpenDialog(null);
if(result!=JFileChooser.CANCEL_OPTION){
File f=jfc.getSelectedFile();
System.out.println("您选择的文件是:"+f.getAbsolutePath());
FileWriter fw=null;
try {
f.createNewFile();
fw=new FileWriter(f);
fw.write(area.getText());
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fw.flush();
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}else{
System.out.println("您取消了选择的文件");
}
}
public void fileOpen(){
JFileChooser jfc=new JFileChooser();
int result=jfc.showOpenDialog(null);
if(result!=JFileChooser.CANCEL_OPTION){
File f=jfc.getSelectedFile();
System.out.println("您选择的文件是:"+f.getAbsolutePath());
FileReader fr=null;
try {
fr=new FileReader(f.getPath());
int i=0;
while((i=fr.read())!=-1){
area.append(String.valueOf((char)i));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}else{
System.out.println("您取消了选择的文件");
}
}
public static void main(String[] args) {
new FileSaveOpen();
}
}
*
* @author ALbert
* @category 文件的打开和保存
*
*/
public class FileSaveOpen extends JFrame{
JTextArea area=null;
public FileSaveOpen(){
setBounds(300, 200, 600, 400);
JMenuBar bar=new JMenuBar();
JMenu jm=new JMenu();
jm.setText("文件");
JMenuItem j1=new JMenuItem();
j1.setText("保存");
JMenuItem j2=new JMenuItem();
j2.setText("打开");
jm.add(j1);
jm.add(j2);
bar.add(jm);
area=new JTextArea();
getContentPane().add(bar,BorderLayout.NORTH);
getContentPane().add(area);
//getContentPane().setLayout(null);
j1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fileSave();
}
});
j2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fileOpen();
}
});
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void fileSave(){
JFileChooser jfc=new JFileChooser();
int result=jfc.showOpenDialog(null);
if(result!=JFileChooser.CANCEL_OPTION){
File f=jfc.getSelectedFile();
System.out.println("您选择的文件是:"+f.getAbsolutePath());
FileWriter fw=null;
try {
f.createNewFile();
fw=new FileWriter(f);
fw.write(area.getText());
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fw.flush();
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}else{
System.out.println("您取消了选择的文件");
}
}
public void fileOpen(){
JFileChooser jfc=new JFileChooser();
int result=jfc.showOpenDialog(null);
if(result!=JFileChooser.CANCEL_OPTION){
File f=jfc.getSelectedFile();
System.out.println("您选择的文件是:"+f.getAbsolutePath());
FileReader fr=null;
try {
fr=new FileReader(f.getPath());
int i=0;
while((i=fr.read())!=-1){
area.append(String.valueOf((char)i));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}else{
System.out.println("您取消了选择的文件");
}
}
public static void main(String[] args) {
new FileSaveOpen();
}
}