我正在制作一个“保存”#39;到特定文件并打开'来自特定文件。保存功能正在运行,但由于我已经实施了“打开”功能。功能现在已停止工作。运行打开功能时,我将其内容打印到控制台,但此时控制台中显示以下错误,控制台中只显示数组列表的第一项。 '开放'并且'保存'功能就在我的代码的底部,但就在我的两个课程之前' Record'和' Getrecord'这些用于“保存”。并且'打开'功能。我很困惑,不知道如何继续,因为我不知道什么是错的。
代码如何显示:
miOpen.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent arg0) {
FileInputStream fis=null;
try{
fis = new FileInputStream("testsave.txt");
}
catch (FileNotFoundException e){
e.printStackTrace();
}
System.out.println("TEST OPEN B4");
String openstring = new String();
BufferedReader bwo = new BufferedReader(new InputStreamReader(fis));
try {
while( (openstring=bwo.readLine()) != null) {
String [] g = openstring.split(",");
System.out.println("TEST OPEN");
System.out.println(openstring);
getrecordsarray.add(new Getrecord(g[0], Double.parseDouble(g[1]), Double.parseDouble(g[2]), Double.parseDouble(g[3]), Double.parseDouble(g[4]), Double.parseDouble(g[5]), Double.parseDouble(g[6]), Double.parseDouble(g[7])));
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
fis.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}});
miSave.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent arg0) {
fileChooser.setTitle("testsave1.txt");
try (BufferedWriter bw = new BufferedWriter(new FileWriter("testsave1.txt", true))) {
for(Record r : records)
bw.write(r.name + "," + r.age + "," + r.gender + "," + r.ethnicity + "," + r.answerA + "," + r.answerB + "," + r.answerC + "," + count);
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("SAVED");
if (autoreturn==1){
System.out.println("saved fade");
FadeTransition ft = new FadeTransition(Duration.millis(3000), savemsg1);
ft.setFromValue(0);
ft.setToValue(1);
ft.play();
FadeTransition ft2 = new FadeTransition(Duration.millis(3000), savemsgl1);
ft2.setFromValue(0);
ft2.setToValue(1);
ft2.play();
}
}});
}
void saveFile(File save)
{
FileOutputStream fos=null;
try{
fos = new FileOutputStream(save);
}
catch (FileNotFoundException e){
e.printStackTrace();
}
try{
fos.close();
} catch (IOException e){ e.printStackTrace();
}
}
}
当打开&#39>时,控制台会抛出此错误。按钮运行:
Exception in thread "JavaFX Application Thread" java.lang.ArrayIndexOutOfBoundsException: 1
at SurveyApp$10.handle(SurveyApp.java:1181)
at SurveyApp$10.handle(SurveyApp.java:1)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
答案 0 :(得分:1)
显然,您从文件中读到的文字会尝试通过&#39;,&#39;并不包含您期望的硬编码数组索引的结构:
getrecordsarray.add(new Getrecord(g[0], Double.parseDouble(g[1]), Double.parseDouble(g[2]), Double.parseDouble(g[3]), Double.parseDouble(g[4]), Double.parseDouble(g[5]), Double.parseDouble(g[6]), Double.parseDouble(g[7])));