前提・実現したいこと
テキストフィールドに書いた実数値をファイルとして保存して読み込みたいのですが、readメソッドでエラーが起きてしまいます。どなたか使えない原因と改善策を教えて頂けませんでしょうか?
発生している問題・エラーメッセージ
75と77行目のreadメソッドでエラーが発生しています。
Exception in thread "JavaFX Application Thread" java.lang.Error: Unresolved compilation problems: 型 FilterInputStream のメソッド read(byte[]) は引数 (int) に適用できません 型 FilterInputStream のメソッド read(byte[]) は引数 (int) に適用できません at kadai.sixteen$SampleEventHandler.handle(sixteen.java:75) at kadai.sixteen$SampleEventHandler.handle(sixteen.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.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.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.Node.fireEvent(Node.java:8411) at javafx.scene.control.Button.fire(Button.java:185) at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182) at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96) at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89) 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.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:394) at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432) at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431) at com.sun.glass.ui.View.handleMouseEvent(View.java:555) at com.sun.glass.ui.View.notifyMouse(View.java:937) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177) at java.lang.Thread.run(Thread.java:748)
該当のソースコード
package kadai; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.stage.FileChooser; import javafx.stage.Stage; public class sixteen extends Application{ private Label lb; private TextField tf[] = new TextField[7]; private Button bt1,bt2; public static void main(String[]args) { launch(args); } public void start(Stage stage)throws Exception{ lb = new Label("実数を入力してください"); bt1 = new Button("読込"); bt2 = new Button("保存"); for(int i = 0;i<tf.length;i++) { String num = (Double.valueOf(i).toString()); tf[i] = new TextField(num); } BorderPane bp = new BorderPane(); HBox hb1 = new HBox(); for(int i = 0;i<tf.length;i++) { hb1.getChildren().add(tf[i]); } HBox hb2 = new HBox(); hb2.getChildren().add(bt1); hb2.getChildren().add(bt2); bp.setTop(lb); bp.setCenter(hb1); bp.setBottom(hb2); bt2.setOnAction(new SampleEventHandler()); bt1.setOnAction(new SampleEventHandler()); Scene sc = new Scene(bp,400,400); stage.setScene(sc); stage.setTitle("サンプル"); stage.show(); } class SampleEventHandler implements EventHandler<ActionEvent>{ public void handle(ActionEvent e) { FileChooser fc = new FileChooser(); fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("バイナリファイル","*.bin")); if(e.getSource() == bt1) { try {File flo = fc.showOpenDialog(new Stage()); if(flo != null) { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(flo)); for(int i = 0;i<tf.length;i++) { double num = Double.parseDouble(tf[i].getText()); int num1 = (int) num; bis.read(num1); int num2 = (int)((num-num1)*1000); bis.read(num2); tf[i].setText(Double.valueOf(num2).toString()); } bis.close(); } } catch(Exception ex) { ex.printStackTrace(); } }else if(e.getSource() == bt2) { try { File fls = fc.showSaveDialog(new Stage()); if(fls != null) { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fls)); for(int i = 0;i<tf.length;i++) { double num = Double.parseDouble(tf[i].getText()); int num1 = (int) num; bos.write(num1); int num2 = (int)((num-num1)*1000); bos.write(num2); } bos.close(); } } catch(Exception ex){ ex.printStackTrace(); } } } } }
試したこと
調べたところ、readメソッドは整数値しか使えないので、整数値を1000倍にすることを試しました。
補足情報(FW/ツールのバージョンなど)
jdk14
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/01/29 02:00
2021/01/29 11:20
2021/01/29 11:37
2021/01/29 11:51
2021/01/29 11:53
2021/01/29 12:07
2021/01/29 12:16
2021/02/04 15:13 編集
2021/02/04 16:52
2021/02/05 03:52