import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class RandomAccessFileTest
{
public static void main(String [] args)
{
RandomA ra = new RandomA("单词录入与查询");
}
}
class RandomA extends JFrame implements ActionListener
{
File file = new File("英语单词.txt");
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JButton jb1 = new JButton("录入");
JButton jb2 = new JButton("显示");
JTextArea jta = new JTextArea(10,30);
JTextField jtd1 = new JTextField(8);
JTextField jtd2 = new JTextField(8);
JLabel jl1 = new JLabel("输入单词");
JLabel jl2 = new JLabel("单词解释");
RandomA(String s)
{
super(s);
setLayout(new BorderLayout());
add(jp1,"North");
jp1.add(jl1);
jp1.add(jtd1);
jp1.add(jl2);
jp1.add(jtd2);
jp1.add(jb1);
//add(jp1);
add(jp2,"Center");
jp2.add(jb2);
jp2.add(jta);
//add(jp2);
setSize(400,350);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
jb1.addActionListener(this);
jb2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jb1)
{
Write();
}
if (e.getSource() == jb2)
{
Xian();
}
}
public void Write()
{
try
{
RandomAccessFile raf1 = new RandomAccessFile(file,"rw");
if (file.exists())
{
long l = file.length();
raf1.seek(l);
}
raf1.writeUTF("单词:"+jtd1.getText());
raf1.writeUTF("解释:"+jtd2.getText());
raf1.close();
}
catch (IOException e)
{
System.out.println(e);
}
}
public void Xian()
{
int num = 1;
try
{
RandomAccessFile raf2 = new RandomAccessFile(file,"r");
String s = null;
while((s = raf2.readUTF())!=null)
{
jta.append("\n"+num+" "+s);
jta.append(" "+raf2.readUTF());
jta.append("\n___________________");
num++;
}
raf2.close();
}
catch (IOException e)
{
System.out.println(e);
}
}
}
为什么一点击显示按键就出现这么的错误 录入没有问题
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class RandomAccessFileTest
{
public static void main(String [] args)
{
RandomA ra = new RandomA("单词录入与查询");
}
}
class RandomA extends JFrame implements ActionListener
{
File file = new File("英语单词.txt");
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JButton jb1 = new JButton("录入");
JButton jb2 = new JButton("显示");
JTextArea jta = new JTextArea(10,30);
JTextField jtd1 = new JTextField(8);
JTextField jtd2 = new JTextField(8);
JLabel jl1 = new JLabel("输入单词");
JLabel jl2 = new JLabel("单词解释");
RandomA(String s)
{
super(s);
setLayout(new BorderLayout());
add(jp1,"North");
jp1.add(jl1);
jp1.add(jtd1);
jp1.add(jl2);
jp1.add(jtd2);
jp1.add(jb1);
//add(jp1);
add(jp2,"Center");
jp2.add(jb2);
jp2.add(jta);
//add(jp2);
setSize(400,350);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
jb1.addActionListener(this);
jb2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jb1)
{
Write();
}
if (e.getSource() == jb2)
{
Xian();
}
}
public void Write()
{
try
{
RandomAccessFile raf1 = new RandomAccessFile(file,"rw");
if (file.exists())
{
long l = file.length();
raf1.seek(l);
}
raf1.writeUTF("单词:"+jtd1.getText());
raf1.writeUTF("解释:"+jtd2.getText());
raf1.close();
}
catch (IOException e)
{
System.out.println(e);
}
}
public void Xian()
{
int num = 1;
try
{
RandomAccessFile raf2 = new RandomAccessFile(file,"r");
String s = null;
while((s = raf2.readUTF())!=null)
{
jta.append("\n"+num+" "+s);
jta.append(" "+raf2.readUTF());
jta.append("\n___________________");
num++;
}
raf2.close();
}
catch (IOException e)
{
System.out.println(e);
}
}
}
为什么一点击显示按键就出现这么的错误 录入没有问题
把jta.append(" "+raf2.readUTF());换成s看看
你第一次取的时候,其实file文件是空的吧。如果是空的,则文件的指针指向末尾,raf2.readUTF()之后就会报错
readUTF()我也整不明白,容易报错
写入内容我用的FileWriter,可以追加内容
读的话我觉得保险点直接用byte[]操作吧,这样也能解决编码问题
我吧你的write()和xian()改了下能运行了你看看有帮助吗?
public void Write()
{
FileWriter writer = null;
try {
String inputContent = "单词:"+jtd1.getText()+"\n"+"解释:"+jtd2.getText();
// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
writer = new FileWriter(file, true);
writer.write(inputContent);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(writer != null){
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void Xian()
{
DataInputStream dis = null;
try
{
dis = new DataInputStream(new FileInputStream(file));
int len = dis.available();
byte[] data = new byte[len];
String content = null;
dis.read(data);
content = new String(data, "GBK");//读的编码要和文件编码一致
jta.setText(content);;
System.out.println(content);
dis.close();
}
catch (IOException e)
{
e.printStackTrace();
} finally{
try {
if (dis!= null) {
dis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}