就像问题中所说的那样,我正在尝试编写一个程序,该程序将接收输入的信息并将该信息转换为二进制顺序访问文件。据我所知,该部分工作正常(没有异常,文件存在写入的内容,但是我无法分辨)。我也可以很好地打开二进制文件,但是当我尝试从中读取二进制文件时,每次都会得到NullPointerException,但是我知道一个事实,即该文件至少具有 something 。
我们将不胜感激!谢谢。
此方法打开二进制文件以供输出
private int OpenFileOut() {
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = jfc.showOpenDialog(this);
if (result == jfc.CANCEL_OPTION) {
return 1;
}
file = jfc.getSelectedFile();
try {
fon = new FileOutputStream(file);
out = new DataOutputStream(fon);
}
catch ( IOException ioe ) {
JOptionPane.showMessageDialog( this, ioe.toString(), "Error",
JOptionPane.ERROR_MESSAGE );
System.exit( 1 );
}
JOptionPane.showMessageDialog(null, "Successfully Opened");
return 0;
}
这下一部分将写入二进制文件
private void AddToFile() {
String name = "";
String id = "";
String qu = "";
String numin = "";
String bp = "";
for (int x = 0; x < tools.getNumberOfToolItems(); x++) {
name = name + tools.toolArray[x].getName() + " ";
id = id + tools.toolArray[x].getID() + " ";
qu = qu + tools.toolArray[x].getQuality() + " ";
numin = numin + tools.toolArray[x].getNumberInStock() + " ";
bp = bp + tools.toolArray[x].getPrice() + " ";
}
StringTokenizer names = new StringTokenizer(name," ");
StringTokenizer ids = new StringTokenizer(id," ");
StringTokenizer qus = new StringTokenizer(qu," ");
StringTokenizer numins = new StringTokenizer(numin," ");
StringTokenizer bps = new StringTokenizer(bp," ");
String nhold;
int idhold;
int quahold;
int numinhold;
double bphold;
for(int x = 0; x < tools.getNumberOfToolItems(); x++) {
try {
nhold = names.nextToken();
System.out.println(nhold);
out.writeUTF(nhold);
idhold = Integer.parseInt(ids.nextToken());
out.writeInt(idhold);
quahold = Integer.parseInt(qus.nextToken());
out.writeInt(quahold);
numinhold = Integer.parseInt(numins.nextToken());
out.writeInt(numinhold);
bphold = Double.parseDouble(bps.nextToken());
out.writeDouble(bphold);
}
catch (IOException ioe) {
JOptionPane.showMessageDialog(null, ioe.toString(), "Error",
JOptionPane.ERROR_MESSAGE);
System.exit( 1 );
}
}
try {
out.close();
}
catch (IOException ioe) {
JOptionPane.showMessageDialog(null, ioe.toString(), "Error",
JOptionPane.ERROR_MESSAGE);
System.exit( 1 );
}
}
下一部分将打开INput的二进制文件
private void OpenFileIn() {
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = jfc.showOpenDialog(this);
if (result == jfc.CANCEL_OPTION) {
return;
}
file = jfc.getSelectedFile();
try {
FileInputStream fin = new FileInputStream(file);
BufferedInputStream bin = new BufferedInputStream(fin);
in = new DataInputStream(bin);
}
catch (FileNotFoundException fnf) {
System.exit(1);
}
JOptionPane.showMessageDialog(null,"Successfully Opened");
}
最后,最后一部分是给我的问题,它应该从二进制文件中读取。在第一个“读取”中,它抛出NPException
public void Populate() {
int x = 2;
try {
while (true) {
String nme = in.readUTF();
int ida = in.readInt();
int quali = in.readInt();
int nums = in.readInt();
double pb = in.readDouble();
tools.insert(nme,ida,quali,nums,pb);
x++;
System.out.println(x);
}
}
catch (EOFException eof) {
JOptionPane.showMessageDialog(null,"File successfully read");
}
catch (IOException ioe) {
JOptionPane.showMessageDialog( this, ioe.toString(), "Error",
JOptionPane.ERROR_MESSAGE );
System.exit( 1 );
}
try {
in.close();
}
catch (IOException ioe) {
JOptionPane.showMessageDialog( this, ioe.toString(), "Error",
JOptionPane.ERROR_MESSAGE );
System.exit( 1 );
}
}
如果有人可以帮助我阅读本书,我会亲吻他们,因为我花了很长时间盯着这个屏幕。
编辑:
根据要求,以下是我称为“填充”和“ OpenFile”的代码:
if (eve.getSource() == popFile) {
Populate();
}
if (eve.getSource() == openBin) {
OpenBSAFile();
}
public void OpenBSAFile() {
OpenFileOut();
}