【问题标题】:How to read / write in application如何在应用程序中读/写
【发布时间】:2014-07-27 05:32:45
【问题描述】:

我正在尝试制作一个小型应用程序,当玩家单击按钮时,它会从文件中获取值,然后如果文件中存在银行编号,则将其添加到文件中,如果没有,则使用金额:

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

【问题讨论】:

    标签: java javascript eclipse javafx scenebuilder


    【解决方案1】:

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

    修复路径名后

    现在扫描器抱怨(在第 54 行)虽然没有要扫描的下一个令牌,但调用了 next()。通常,调用 `hasNext()?并且仅在返回 true 时继续。

    然而,在写入完成之前写入文件并读取文件(即已调用 close())的想法是不行的。

    我不确定你想用你的代码实现什么,所以我不能提出改进建议。

    【讨论】:

    • 查看我的答案的补充。
    • pastebin.com/RzcxMH1u 好的,我摆脱了错误,但是如何从accounts.txt 中获取数值? (不是银行卡号,是账户余额)
    • 这仍然是同时读写同一个文件?不要这样做。您试图实现什么?银行业务——甚至不是家庭银行业务——都不是那样运作的。
    • 我只是想找到一种方法来在文件中存储带有名称的值,而银行业务是我首先想到的。我将如何更改我当前的代码,以便它首先读取文件然后写入它?
    • 在前面,读取文件的所有(进入合适的集合(或地图),关闭它。运行您的应用程序后(存款,取款,新客户,.. .) 在终止之前,将集合(或映射)写入该文件。是否在更新后也写入文件(以免在崩溃或电源故障的情况下丢失任何数据)取决于您对事情的重视程度;-)
    【解决方案2】:

    我建议使用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();
        }
    

    【讨论】:

      猜你喜欢
      • 2021-03-06
      • 1970-01-01
      • 2012-11-27
      • 2015-09-05
      • 1970-01-01
      • 1970-01-01
      • 2011-08-16
      • 1970-01-01
      • 2019-02-18
      相关资源
      最近更新 更多