【发布时间】:2016-09-11 13:02:52
【问题描述】:
因此,我正在开发一个项目,该项目可以自动执行从角色表到掷骰子的所有操作,以打造我喜欢玩的桌面 RPG。我正在尝试存储可以在执行应用程序开始时访问的字符数据(字符名称、2 个统计数据数组和 2 个统计数据数组)。 This 到目前为止一直很有帮助。
但是,我还想显示名称和统计信息,以便用户确认这是他们想要使用的字符数据。而且我无法以可读格式显示数据。这是我的代码(您会发现我在底部遇到的问题,但如果您在此过程中看到任何其他可以优化的内容,我将不胜感激任何反馈:-)“:
import java.io.*;
import javax.swing.JOptionPane;
public class fengShuiFiles implements Serializable {//start class
private FileOutputStream outFile;
private ObjectOutput objectWriter;
private FileInputStream inFile;
private ObjectInputStream objectReader;
public void WriteFile(String fileNameIn, String[] sArray1, String[] sArray2,
String[] sArray3, String[] sArray4) {
try {
outFile = new FileOutputStream(fileNameIn + ".txt", true);
objectWriter = new ObjectOutputStream(outFile);
objectWriter.writeObject(sArray1);
objectWriter.writeObject(sArray2);
objectWriter.writeObject(sArray3);
objectWriter.writeObject(sArray4);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "I/O occurred during a write operation\nFor more",
"information see console output.",
"Read File", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
} // End try/catch
} // End Open
//not sure if I'll need this. Keeping it for now just in case
//public void writeRecords(String textRecords)
//{
// outFile.close();
// pw.println(textRecords);
//} // End WriteRecords
public void ReadFile(String fileNamein) throws FileNotFoundException {
fengShuiFiles[] sArray1, sArray2, sArray3, sArray4;
try {
inFile = new FileInputStream(fileNamein + ".txt");
objectReader = new ObjectInputStream(inFile);
sArray1 = (fengShuiFiles[]) objectReader.readObject();
sArray2 = (fengShuiFiles[]) objectReader.readObject();
sArray3 = (fengShuiFiles[]) objectReader.readObject();
sArray4 = (fengShuiFiles[]) objectReader.readObject();
} catch (IOException | ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, "I/O error occurred opening a",
"file\nFor more information see console output.",
"Read File", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
} // End try/catch
for (int x = 0; x < sArray1.length; x++) {
}
}
public void closeFile() {
try {
outFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // End closeFile
}//end class
那么,for 语句中的 sArray1.length 指向底部?它出现了一条错误消息,指出 sArray1 可能尚未初始化。而且我无法弄清楚为什么以及如何获得该长度,以便我可以以可读的方式打印出数组。有没有人有任何想法?谢谢。
【问题讨论】:
标签: java