Java文件导入

时间:2018-12-12 05:07:28

标签: java

任何人都可以帮助我弄清楚为什么以下代码总是抛出找不到文件的问题。 input.txt文件与该类位于同一文件夹中,我似乎无法弄清楚我在这里做错了什么。任何帮助将不胜感激。

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class FileAssignment extends JFrame implements ActionListener {

JTextArea tField;
JButton send;

public static void main(String[] args) {

    FileAssignment frame = new FileAssignment();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setSize(400, 200);
    frame.setVisible(true);

}

public FileAssignment() {

    GridBagConstraints layout;

    Font tFont = new Font("Arial", + Font.ITALIC, 16);
    Font bFont = new Font("Arial", Font.BOLD + Font.ITALIC, 18);

    setLayout(new GridBagLayout());
    layout = new GridBagConstraints();
    layout.gridx = 0;
    layout.gridy = 0;
    layout.insets = new Insets(10, 50, 10, 50);

    layout= new GridBagConstraints();
    layout.gridx = 0;
    layout.gridy = 1;
    layout.insets = new Insets(10, 10, 10, 10);

    tField = new JTextArea();
    tField.setEditable(false);

    send = new JButton("Show minimum wage employees");
    send.addActionListener(this);       

    add(tField, layout);
    add(send, layout);
    send.setHorizontalAlignment(JButton.CENTER);

    tField.setFont(tFont);
    send.setFont(bFont);

    setTitle("Employee Wages");

}

@Override
public void actionPerformed(ActionEvent arg0) {

    try {

        double p1 = 9999.99;
        double price;
        String name;

        Scanner scnr = new Scanner(new File("input.txt"));

        while (scnr.hasNext()) {

            name = scnr.nextLine();
            price = Double.parseDouble(scnr.nextLine());

            if (price < p1) {

                p1 = price;

            }

        }

        scnr.close();

        scnr = new Scanner(new File("input.txt"));

        while (scnr.hasNext()) {

            name = scnr.nextLine();
            price = Double.parseDouble(scnr.nextLine());

            if (price == p1) {

                p1 = price;
                tField.append( name + "\n");

            }

        }

        scnr.close();

    } 

    catch (IOException ex) {

        System.out.println("File read error");

    }

}

}

很抱歉有些奇怪的间距。

0 个答案:

没有答案