写了一个java源文件,不能写入和读出,求大神指点!!

package wfwfwfw;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class FWindow extends JFrame implements ActionListener//要实现监听端口
{
JTextArea text;
JButton buttonRead;
JButton buttonWrite;
FileReader in;//输入流
FileWriter out;//输出流
BufferedReader bufferin;//缓冲流
BufferedWriter bufferout;
FWindow()
{
super("文件读取");
JTextArea text=new JTextArea(10,10);//多行文本框
text.setBackground(Color.cyan);//设置背景颜色
JButton buttonRead=new JButton("读取");
buttonRead.addActionListener(this);
JButton buttonWrite=new JButton("写入");//两个按钮
buttonWrite.addActionListener(this);//以当前窗体作监视器
BorderLayout bl=new BorderLayout();
setLayout(bl);//布局
setSize(600,450);
setVisible(true);
add(text,BorderLayout.CENTER);//把文本框放在中间区域
Panel pNorth=new Panel();//空容器,可以装组件,用来嵌套
pNorth.add(buttonRead);
pNorth.add(buttonWrite);//把两个按钮装进容器
pNorth.validate();//刷新,实时更新,确保布局成功
add(pNorth,BorderLayout.NORTH);//把容器放文本框上边,整体北边
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);//退出系统
}
});//10.14.2 256页 适配器
}
public void ActionPerformed(ActionEvent e)
{
if(e.getSource()==buttonRead)
{
try
{
String s;
text.setText(null);
File f=new File("E:/sfile","123.txt");//读文件
in=new FileReader(f);//实例化对象
bufferin=new BufferedReader(in);//bufferin操作对象in,建立缓冲区
while((s=bufferin.readLine())!=null)
{
text.append(s+"\n");
}
bufferin.close();//关闭流
in.close();
}//捕获异常
catch(IOException exe)
{
System.out.println(exe);//打印异常信息
}
}
if(e.getSource()==buttonWrite)
{
try
{
File f=new File("E:/sfile","123.txt");//读文件
out=new FileWriter(f);
bufferout=new BufferedWriter(out);
bufferout.write(text.getText(),0,text.getText().length());//获取文本框,从第0位置开始写,长度为读取文本框的长度。
bufferout.flush();//强制将缓冲区数据写入文件
bufferout.close();
out.close();//关闭
}
catch(IOException exe)
{
System.out.println(exe);//打印异常信息
}
}
}
}
public class Lala
{
public static void main(String[] args)
{
FWindow win=new FWindow();
win.validate();//刷新,重新布局
}
}

第1个回答  推荐于2016-08-11
花了一小时帮你调好了(读取):
package wfwfwfw;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class FWindow extends JFrame implements ActionListener// 要实现监听端口
{
/**
*
*/
private static final long serialVersionUID = -8471515905942004558L;
JTextArea text=new JTextArea(10,10);
JButton buttonRead=new JButton("读取");
JButton buttonWrite=new JButton("写入");
FileReader in;// 输入流
FileWriter out;// 输出流
BufferedReader bufferin;// 缓冲流
BufferedWriter bufferout;

FWindow() {
super("文件读取");
//JTextArea text = new JTextArea(10, 10);// 多行文本框
text.setBackground(Color.cyan);// 设置背景颜色
//JButton buttonRead = new JButton("读取");
this.buttonRead.addActionListener(this);
//JButton buttonWrite = new JButton("写入");// 两个按钮
this.buttonWrite.addActionListener(this);// 以当前窗体作监视器
BorderLayout bl = new BorderLayout();
setLayout(bl);// 布局
setSize(600, 450);
setVisible(true);
add(text, BorderLayout.CENTER);// 把文本框放在中间区域
Panel pNorth = new Panel();// 空容器,可以装组件,用来嵌套
pNorth.add(buttonRead);
pNorth.add(buttonWrite);// 把两个按钮装进容器
pNorth.validate();// 刷新,实时更新,确保布局成功
add(pNorth, BorderLayout.NORTH);// 把容器放文本框上边,整体北边
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);// 退出系统
}
});// 10.14.2 256页 适配器
}
public void actionPerformed(ActionEvent e) {
System.out.println(":"+e.getSource().getClass());
if (e.getSource() == buttonRead) {
try {
String s="";
text.setText(null);
File f = new File("E:/sfile", "123.txt");// 读文件
in = new FileReader(f);// 实例化对象
bufferin = new BufferedReader(in);// bufferin操作对象in,建立缓冲区
while ((s = bufferin.readLine()) != null) {
System.out.println(s);
text.append(s + "\n");
}
bufferin.close();// 关闭流
in.close();
}// 捕获异常
catch (IOException exe) {
System.out.println(exe);// 打印异常信息
}
}
if (e.getSource() ==buttonWrite) {
try {
File f = new File("E:/sfile", "123.txt");// 读文件
out = new FileWriter(f);
bufferout = new BufferedWriter(out);
bufferout.write(text.getText(), 0, text.getText().length());// 获取文本框,从第0位置开始写,长度为读取文本框的长度。
bufferout.flush();// 强制将缓冲区数据写入文件
bufferout.close();
out.close();// 关闭
} catch (IOException exe) {
System.out.println(exe);// 打印异常信息
}
}
}

}
public class Lala {
public static void main(String[] args) {
FWindow win = new FWindow();
win.validate();// 刷新,重新布局
}
}本回答被提问者采纳
第2个回答  2012-11-23
.........
相似回答