java中为什么使用write不能把字符写到文件,以下代码哪里有问题呢?
packagecom.bin;importjavax.swing.*;importjava.io.*;importjava.awt.*;importjava.awt.ev...
package com.bin;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class demo3 extends JFrame implements ActionListener {
JTextArea jta;
JMenu jm;
JMenuBar jmb;
JMenuItem jmt;
JMenuItem jmt2;
public static void main(String[] args) {
// TODO Auto-generated method stub
demo3 demo=new demo3();
}
public demo3(){
jta=new JTextArea();
jmb=new JMenuBar();
jm =new JMenu("菜单");
jmt=new JMenuItem("打开");
jmt.addActionListener(this);
jmt.setActionCommand("open");
jmt2=new JMenuItem("保存");
jmt2.addActionListener(this);
jmt2.setActionCommand("save");
this.add(jta);
jmb.add(jm);
jm.add(jmt);
jm.add(jmt2);
this.setJMenuBar(jmb);
this.setSize(500, 350);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setTitle("记事本");
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("open")){
JFileChooser jfc=new JFileChooser();
jfc.setDialogTitle("选择文件...");
jfc.showOpenDialog(null);
jfc.setVisible(true);
String firepath=jfc.getSelectedFile().getAbsolutePath();
FileReader fr=null;
BufferedReader br=null;
try {
fr=new FileReader(firepath);
br=new BufferedReader(fr);
String n="";
String s="";
while((n=br.readLine())!=null){
s+=n+"\r\n";
}
this.jta.setText(s);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{
try {
fr.close();
br.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
else if(e.getActionCommand().equals("save")){
JFileChooser jfc=new JFileChooser();
jfc.setDialogTitle("另存为...");
jfc.showSaveDialog(null);
jfc.setVisible(true);
String filepath=jfc.getSelectedFile().getAbsolutePath();
FileWriter fw=null;
BufferedWriter bw=null;
try {
fw=new FileWriter(filepath);
bw=new BufferedWriter(fw);
System.out.println(filepath);
bw.write(this.jta.getText());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{
try {
fw.close();
bw.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}
} 展开
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class demo3 extends JFrame implements ActionListener {
JTextArea jta;
JMenu jm;
JMenuBar jmb;
JMenuItem jmt;
JMenuItem jmt2;
public static void main(String[] args) {
// TODO Auto-generated method stub
demo3 demo=new demo3();
}
public demo3(){
jta=new JTextArea();
jmb=new JMenuBar();
jm =new JMenu("菜单");
jmt=new JMenuItem("打开");
jmt.addActionListener(this);
jmt.setActionCommand("open");
jmt2=new JMenuItem("保存");
jmt2.addActionListener(this);
jmt2.setActionCommand("save");
this.add(jta);
jmb.add(jm);
jm.add(jmt);
jm.add(jmt2);
this.setJMenuBar(jmb);
this.setSize(500, 350);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setTitle("记事本");
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("open")){
JFileChooser jfc=new JFileChooser();
jfc.setDialogTitle("选择文件...");
jfc.showOpenDialog(null);
jfc.setVisible(true);
String firepath=jfc.getSelectedFile().getAbsolutePath();
FileReader fr=null;
BufferedReader br=null;
try {
fr=new FileReader(firepath);
br=new BufferedReader(fr);
String n="";
String s="";
while((n=br.readLine())!=null){
s+=n+"\r\n";
}
this.jta.setText(s);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{
try {
fr.close();
br.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
else if(e.getActionCommand().equals("save")){
JFileChooser jfc=new JFileChooser();
jfc.setDialogTitle("另存为...");
jfc.showSaveDialog(null);
jfc.setVisible(true);
String filepath=jfc.getSelectedFile().getAbsolutePath();
FileWriter fw=null;
BufferedWriter bw=null;
try {
fw=new FileWriter(filepath);
bw=new BufferedWriter(fw);
System.out.println(filepath);
bw.write(this.jta.getText());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{
try {
fw.close();
bw.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询