怎么不写入和读出啊JAVA

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class n extends SelectionAdapter{
final Display display;
final Shell shell;
private Button bx;
private Button bc;
int n;
private Text tm;
private Text tx;
private RandomAccessFile rf;

n(){
display=new Display();
shell=new Shell(display);
shell.setText("file");
shell.setLayout(new RowLayout(SWT.VERTICAL));
RowData rowdata=new RowData(50,15);
tm=new Text(shell,SWT.WRAP|SWT.V_SCROLL);
tx=new Text(shell,SWT.WRAP|SWT.V_SCROLL);
bx=new Button(shell,SWT.NONE);
bc=new Button(shell,SWT.NONE);
tm.setLayoutData(rowdata);
tx.setLayoutData(rowdata);
bx.setLayoutData(rowdata);
bx.setText("look");
bc.setText("insert");
bc.setLayoutData(rowdata);
bx.addSelectionListener(this);
bc.addSelectionListener(this);
shell.pack();
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}

public static void main(String args[]){
new n();
}

public void widgetSeleted(SelectionEvent e){
if(e.getSource()==bx){
try {
rf=new RandomAccessFile("LX5_5.txt", "rw");
String s = null;
try {
s = rf.readLine();
} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
n++;

if (n%2==0){

tm.append(s);

}
else{

tx.append(s);

}

} catch (FileNotFoundException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}

if(e.getSource()==bc){
try{
rf=new RandomAccessFile("LX5_5.txt", "rw");
rf.seek(rf.length());
rf.writeChars(tm.getText());
rf.writeChars(tx.getText());
rf.close();
}

catch (FileNotFoundException fnoe) {}
catch (IOException ioe) {}
}
}
}
}
还有如下异常:

widgetSeleted 这里写错了 应该是widgetSelected
注意看 你少写了个c
所以你这并没有复写 SelectionAdapter的widgetSelected ()方法 因此你的程序不会监听点击事件 当然也无从谈起 读写了

而且我还发现 你把if(e.getSource()==bc){ 写在了if(e.getSource()==bx){这个if字句里面 这是有问题的。。。。 如果没点击look 那你的写入代码都不会执行

大哥啊 怎么会有这个异常呢? 你原来的程序 我都在我这边运行通过的

/**
*
*/
/**
* @author zenglulin
*
*/
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import com.sun.org.apache.bcel.internal.util.BCELifier;

public class n extends SelectionAdapter{
final Display display;
final Shell shell;
private Button bx;
private Button bc;
int n;
private Text tm;
private Text tx;
private RandomAccessFile rf;

n(){
display=new Display();
shell=new Shell(display);
shell.setText("file");
shell.setLayout(new RowLayout(SWT.VERTICAL));
RowData rowdata=new RowData(50,15);
tm=new Text(shell,SWT.WRAP|SWT.V_SCROLL);
tx=new Text(shell,SWT.WRAP|SWT.V_SCROLL);
bx=new Button(shell,SWT.NONE);
bc=new Button(shell,SWT.NONE);
tm.setLayoutData(rowdata);
tx.setLayoutData(rowdata);
bx.setLayoutData(rowdata);
bx.setText("look");
bc.setText("insert");
bc.setLayoutData(rowdata);
bx.addSelectionListener(this);
bc.addSelectionListener(this);

shell.pack();
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}

public static void main(String args[]){
new n();

}

public void widgetSelected(SelectionEvent e){
if(e.getSource()==bx){
try {
rf=new RandomAccessFile("a.txt", "rw");
String s = null;
try {
s = rf.readLine();
} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
n++;

if (n%2==0){

tm.append(s);

}
else{

tx.append(s);

}

} catch (FileNotFoundException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}

}

if(e.getSource()==bc){
System.out.println("-----");
try{
rf=new RandomAccessFile("a.txt", "rw");
rf.seek(rf.length());
rf.writeChars(tm.getText());
rf.writeChars(tx.getText());
rf.close();
}

catch (FileNotFoundException fnoe) {}
catch (IOException ioe) {}
}
}
}
我这边的代码 你自己看看先
温馨提示:答案为网友推荐,仅供参考
相似回答
大家正在搜