我正在尝试从包含三个数字的文件中读取。该文件如下所示:
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();
}
}
程序编译正常,没有错误。但是当我输出数组fileValues的内容时,我得到: 0 0 0
任何帮助将不胜感激。谢谢!
更新我查看了FileReader的异常,现在它显示了FileNotFoundException。这很奇怪,因为该文件存在于项目文件夹中。有什么建议??
答案 0 :(得分:1)
您需要提供“temperature.txt”的完整路径。
您忽略了I / O操作发送的异常:
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块:
} catch (Exception e) {
System.out.println(e.getMessage());
}
你应该得到一个自我解释的信息。