从包含整数的文件中读取-Java [英] Read from a file containing integers - java

查看:63
本文介绍了从包含整数的文件中读取-Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从包含三个数字的文件中读取。该文件如下所示:

I am trying to read from a file that contains three numbers. The file looks like this:

45
20
32

我的代码如下:

import java.awt.Color;
import java.awt.Desktop.Action;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
import javax.swing.Timer;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JSlider;
import javax.swing.border.TitledBorder;


public class practise implements ActionListener {

int menuCount = 0;
int [] fileValues = new int[3];

JFrame frame1 = new JFrame();
JPanel[] panels = new JPanel[3];

JMenuItem menuitemMyDialog1 = new JMenuItem( "Open File" );
JMenuItem menuitemMyDialog2 = new JMenuItem( "EXIT" );
JMenuBar menuBar = new JMenuBar( );
JMenu menuData = new JMenu( "Menu" );

Label label = new Label();

JSlider slider = new JSlider( JSlider.VERTICAL,0,100,20);;

Timer timer = new Timer(1000,new TimerAction());

void go(){

    frame1.setTitle("Referred Coursework");
    frame1.setSize(600, 300);
    frame1.setVisible(true);
    buildGUI();


    menuitemMyDialog1.addActionListener( this );
    menuData.add( menuitemMyDialog1 );

    //buildGUI();

    menuitemMyDialog2.addActionListener( this );
    menuData.add( menuitemMyDialog2 );

    menuBar.add( menuData );
    frame1.setJMenuBar( menuBar );

}
int b = 0;
class TimerAction implements ActionListener{
    public void actionPerformed(ActionEvent e){
        if(b == 3){ timer.stop(); }
        slider.setValue(fileValues[b]);
        b++;
    }
}


@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub


    if(arg0.getSource() == menuitemMyDialog1){
        menuCount = 1;
        String inputValue = JOptionPane.showInputDialog("File Open dialog box");
        label.setSize(80,80);
        label.setText(inputValue);
        label.setLocation(40,160);

        //fileValues[1] = 27;  fileValues[0] = 2;  fileValues[2] = 62;


        try {
            FileReader file = new FileReader("temperature.txt");
            BufferedReader buf = new BufferedReader(file);
            int i = 0;

            String s = null;
            while((s = buf.readLine()) != null){
            fileValues[i] = Integer.parseInt(s);
            i++;
            }
        }catch (Exception e){e.printStackTrace();}

        Arrays.sort(fileValues);
        for (int i : fileValues){
            System.out.println(i);
        }

        timer.start();


    }
    if(arg0.getSource() == menuitemMyDialog2){

        frame1.dispose();
        System.exit(0);
    }

    }

public void buildGUI(){
    layoutComponents();
}

public void layoutComponents(){



    JLabel label1 = new JLabel();

    JSlider slider2,slider3;
    //JProgressBar bar = new JProgressBar( JProgressBar.VERTICAL,1000, 1020 );

    panels[0] = new JPanel();
    panels[1] = new JPanel();
    panels[2] = new JPanel();

    panels[1].setBorder( new TitledBorder( "Temperature" ) );

    slider.setMajorTickSpacing(20);
    slider.setPaintTicks( true );
    slider.setPaintLabels( true );
    slider.setMinorTickSpacing(10);

    panels[1].add( slider );

    panels[1].setBackground(Color.orange);

    frame1.setLayout( new GridLayout( 1,2 ) );
    for ( int i = 0; i < panels.length;i++ ){
            frame1.add( panels[i] );
        }
}

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    practise obj = new practise();
    obj.go();


}

}

该程序可以编译,并且没有错误。但是当我输出数组文件值的内容时,我得到:
0
0
0

The program compiles alright and gives no errors. But when I output the contents of the array fileValues I get: 0 0 0

任何帮助将不胜感激。谢谢!

Any help would be appreciated. Thanks!

更新我检查了FileReader的异常,现在它显示了FileNotFoundException。这很奇怪,因为该文件存在于项目文件夹中。有建议吗?

Update I reviewed the exception for FileReader and now it is showing a FileNotFoundException. This is strange as the file exists in the project folder. Any suggestions??

推荐答案

您需要提供 temperature.txt的完整路径。

You need to provide the full path for "temperature.txt".

您将忽略I / O操作发送的异常:

You ignore the exceptions sent by your I/O operations:

    try {
        FileReader file = new FileReader("temperature.txt");
        BufferedReader buf = new BufferedReader(file);
        int i = 0;

        String s = null;
        while ((s = buf.readLine()) != null) {
            fileValues[i] = Integer.parseInt(s);
            i++;
        }
    } catch (Exception e) {
    }

如果用以下内容替换catch块:

If you replace the catch block by something like:

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

您应该得到一个自我解释的信息。

You should get a self explanatory message.

这篇关于从包含整数的文件中读取-Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆