如何在应用程序中读/写

时间:2014-07-27 05:32:45

标签: java javascript eclipse javafx scenebuilder

我正在尝试创建一个小应用程序,当玩家点击按钮时,它会从文件中获取值,然后如果银行编号在文件中,则会添加到该文件中,如果没有,则创建一个新的银行编号。金额:

package application;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;


public class Main extends Application {
        @Override
        public void start(Stage primaryStage) {
                try {
                        //BorderPane root = new BorderPane();
                        Parent root = FXMLLoader.load(getClass().getResource("Root.fxml"));
                        Scene scene = new Scene(root,400,400);
                        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
                        primaryStage.setScene(scene);
                        primaryStage.show();
                } catch(Exception e) {
                        e.printStackTrace();
                }
        }

        public static void main(String[] args) {
                launch(args);
        }
        @FXML
        private TextField banknumber;

        @FXML
        private TextField original;

        @FXML
        private TextField deposited;

        @FXML
        private TextField output;
        @FXML
        protected void onClick(ActionEvent event) throws FileNotFoundException{
                PrintStream diskWriter = new PrintStream(new File("accounts.txt"));
                Scanner diskReader = new Scanner(new File("accounts.txt"));
                int origbal;
                int addtobal = Integer.parseInt(deposited.getText());
                String number = banknumber.getText();
                if(diskReader.next().equals(number)){
                        origbal = diskReader.nextInt();
                        int newbal = origbal+addtobal;
                        diskWriter.println("Number: " + number + " Total: " + newbal);
                }
                else{
                        origbal = Integer.parseInt(original.getText());
                        int newbal = origbal+addtobal;
                        diskWriter.println("Number: " + number + " Total: " + newbal);
                }
                int newbal = origbal+addtobal;
                output.setText("You have added $" + addtobal + " to bank number: " + number + "for a total of $" + newbal);
                diskWriter.close();
        }
}

我收到错误但我不知道它有什么问题(行号与粘贴箱匹配):

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
        at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
        at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
        at javafx.event.Event.fireEvent(Unknown Source)
        at javafx.scene.Node.fireEvent(Unknown Source)
        at javafx.scene.control.Button.fire(Unknown Source)
        at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
        at com.sun.javafx.scene.control.skin.SkinBase$4.handle(Unknown Source)
        at com.sun.javafx.scene.control.skin.SkinBase$4.handle(Unknown Source)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
        at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
        at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
        at javafx.event.Event.fireEvent(Unknown Source)
        at javafx.scene.Scene$MouseHandler.process(Unknown Source)
        at javafx.scene.Scene$MouseHandler.process(Unknown Source)
        at javafx.scene.Scene$MouseHandler.access$1900(Unknown Source)
        at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
        at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
        at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
        at com.sun.glass.ui.View.notifyMouse(Unknown Source)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
        at com.sun.glass.ui.win.WinApplication$3$1.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.reflect.misc.Trampoline.invoke(Unknown Source)
        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
        ... 45 more
Caused by: java.util.NoSuchElementException
        at java.util.Scanner.throwFor(Unknown Source)
        at java.util.Scanner.next(Unknown Source)
        at application.Main.onClick(Main.java:54)
        ... 54 more

2 个答案:

答案 0 :(得分:0)

您在代码中使用的文件名是" accounts",not" accounts.txt"。另外,我没有看到正在定义的按钮。

修复路径名称后

现在,扫描程序抱怨(第54行)虽然没有下一个要扫描的令牌,但仍会调用next()。通常,一个人调用`hasNext()?并且只有在返回true时才进行。

然而,在写完之前写一个文件并从中读取文件的想法已经完成(即调用了close())是禁止的。

我不确定您希望使用您的代码实现什么目标,因此我无法提出改进建议。

答案 1 :(得分:0)

我建议使用java.util.properties将文件中的属性加载到HashMap中,并允许您提取值。

假设“accounts.txt”的格式为

banknumber1=origbal1 banknumber2=origbal2

    @FXML
    protected void onClick(ActionEvent event) throws FileNotFoundException{
        Properties p = new Properties();
        InputStream is = new InputStream("accounts.txt");

        p.load(is);
        is.close();

        String number = banknumber.getText();
        String origbal = p.getProperty(number);
        int addtobal = Integer.parseInt(deposited.getText());

        if (origbal != null)
            p.setProperty(number, Integer.parseInt(origbal) + addtobal);

        OutputStream os = new OutputStream("accounts.txt");
        p.store(os);
        os.close();
    }