当我尝试编译时,我不断收到此消息:解析时到达文件末尾有人知道为什么吗?我唯一想到的是它与我的花括号有关。我试图移动大括号,添加它们并删除它们,但我无法弄清楚这一点。错误发生在最后一行代码中。
import java.awt.event.ActionEvent; //Next group of lines import various Java classes
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.GridLayout;
import java.text.*;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class ReadTextFile extends JFrame
{
public static void main(String[] args) throws IOException {
//Creates Window
final JFrame frame = new JFrame();
frame.setSize(450, 300); //Sets size of the window
frame.setTitle("Read a Text File"); //Adds title to the GUI
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create labels and fields
JLabel Firstlbl = new JLabel("First Text Line");
final JTextField FirstField = new JTextField(20);
FirstField.setText("1st");
JLabel Secondlbl = new JLabel("Second Text Line");
final JTextField SecondField = new JTextField(20);
SecondField.setText("2nd");
JLabel Thirdlbl = new JLabel("Third Text Line");
final JTextField ThirdField = new JTextField(20);
ThirdField.setText("3rd");
JLabel ButtonLabel = new JLabel("Click button to read text from file.");
final JButton button = new JButton("Click Here");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4,2));
panel.add(ButtonLabel);
panel.add(button);
panel.add(Firstlbl);
panel.add(FirstField);
panel.add(Secondlbl);
panel.add(SecondField);
panel.add(Thirdlbl);
panel.add(ThirdField);
frame.add(panel);
class CalculateListener implements ActionListener {
private boolean readFile(String fileName)
{
boolean flag = true;
try{
//initialize the file object
BufferedReader reader=new BufferedReader(new FileReader(fileName));
String line;
int counter = 0;
//reading the lines
while((line = reader.readLine()) != null)
{
counter++;
}
reader.close();
//if file has less then 6 line
if(counter<6)
{
//return the message
JOptionPane.showMessageDialog(this, "Error: This must have minimum 6 lines\nEnter another file name and try again.", "FILE LINES ERROR", JOptionPane.ERROR_MESSAGE);
flag = false;
}
if(flag){
//initialize the array wtih line counter
lines = new String [counter];
reader=new BufferedReader(new FileReader(fileName));
//reading the lines
counter =0;
while((line = reader.readLine()) != null)
{
//set the array elements with the lines of the file
lines[counter++] = line;
}
reader.close();
}
}
catch(IOException ioe) //exception if any
{
JOptionPane.showMessageDialog(this, "Error"+ioe.getMessage(), "FILE READING ERROR", JOptionPane.ERROR_MESSAGE);
}
catch(Exception e) //exception if any
{
JOptionPane.showMessageDialog(this, "Error"+e.getMessage(), "GENERAL ERROR", JOptionPane.ERROR_MESSAGE);
}
return flag;
}
//method to handle action of button
public void actionPerformed (ActionEvent ae)
{
if(ae.getSource()== displayButton)
{
resultTextArea.setText("");
String fileName = "input.txt";
//call the function readFile() with file name
if(readFile(fileName))
{
for(int i=0; i< lines.length; i++)
{
if(i%2==0)
{
//display the array elements to text area
resultTextArea.append(lines[i]+"\n");
}
}
}
}
}
}
}