Frage

Hallo zusammen, ich arbeite an einem in Java erstellten Hex-Dump-Dienstprogramm.Ich habe einige Probleme mit dem Scanner, den ich vor der Hex/ASCII-Logik verwenden würde.

Im Moment habe ich die Logik und ein paar andere Dinge aus Debugging-Gründen ausgeschlossen, aber wenn jemand weiß, was los ist, würde ich es gerne wissen!Hinweise werden sehr geschätzt.

package filedumputility;

import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class FileDump extends JFrame {

public FileDump() throws Exception{
    setTitle("File Dump Utility");
    add(new DumpPanel());
}

//Main method
public static void main(String[] args) throws Exception {
    FileDump frame = new FileDump();
    frame.setSize(500, 250);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setVisible(true);
}
//Class to creaste components
class DumpPanel extends JPanel {
    private JFileChooser fileChooser = new JFileChooser();
    private JTextArea dumpArea = new JTextArea(11, 20);
    private JTextField fileName = new JTextField(16);
    private JButton newButton = new JButton("New File");
    private JButton clearButton = new JButton("Clear");
    private JButton dumpButton = new JButton("Dump File");
    private JScrollPane scrollPane = new JScrollPane(dumpArea);
    private JPanel p = new JPanel();
    private Scanner input;

    //Class to add components and read the file
    public DumpPanel() throws Exception {
        p.setLayout(new FlowLayout());
        setLayout(new BorderLayout());

        add(p, BorderLayout.NORTH);
        p.add(fileName);
        p.add(dumpButton);
        p.add(newButton);
        p.add(clearButton);
        add(scrollPane, BorderLayout.SOUTH);

        dumpArea.setEditable(false);
        fileName.setEditable(false);

        //Create event for the new file button
        newButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){                    

                if(fileChooser.showOpenDialog(null)
                        == JFileChooser.APPROVE_OPTION) {

                    //Get the file
                    java.io.File file = fileChooser.getSelectedFile();

                //Set file name bar to the selected file
                fileName.setText(file.getName());
                }
            }
        });

        dumpButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(fileChooser.showOpenDialog(null)
                        == JFileChooser.APPROVE_OPTION) {

                    //Get the file
                    java.io.File file = fileChooser.getSelectedFile();
                    //Create scanner
                    Scanner input = new Scanner(file);

                }
            }
        });

        clearButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                dumpArea.setText(" ");
                fileName.setText(" ");
            }
        });
    }
}
}
War es hilfreich?

Lösung

Ihr einziges Problem, das ich sehen kann, ist der Scanner-Konstruktor löst eine FileNotFoundException aus;Daher müssen Sie es in einen Try/Catch-Block einschließen.

Es sollte etwa so aussehen (nur für den Anfang):

dumpButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        if(fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {

            //Get the file
            java.io.File file = fileChooser.getSelectedFile();
            //Create scanner
            try {
                Scanner input = new Scanner(file);
            } catch (FileNotFoundException ex) {
                System.err.println("Error opening " + file.getName());
            }
        }
    }
});

Vielleicht finden Sie auch die Tutorial zu Java-Ausnahmen hilfreich.

Andere Tipps

Um Ihr Beispiel zu kompilieren, können Sie das einwickeln new Scanner Linie in einem Versuch wie diesen:

            Scanner input = null;
            try {
                input = new Scanner(file);
            } catch(FileNotFoundException ex) {
                // handle this condition appropriately
            }

Sobald Sie dies getan haben, können Sie die Zeilen der Datei so verarbeiten, wie Sie es beabsichtigt haben.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top