我没有问题让它运行但是当我点击“查找排名”按钮时它崩溃了。我尝试在BlueJ和NetBeans中运行它,两者都有相同的问题。我收到以下错误:
java.io.FileNotFoundException: /Users/Marque/Desktop/Years (Is a directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at java.io.FileReader.<init>(FileReader.java:58)
at Main.readFile(Main.java:128)
at Main$1.actionPerformed(Main.java:94)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
以下是我的代码。
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main {
Map<String,String> fileMap;
JFrame f;
JPanel yearPanel;
JLabel yearLabel;
JComboBox yearBox;
JPanel genderPanel;
JLabel genderLabel;
JComboBox genderBox;
JPanel namePanel;
JLabel nameLabel;
JTextField tf1;
JPanel textPanel;
JTextField textField;
Main(){
fileMap = new HashMap<String,String>();
fileMap.put("2006", "/Users/Marque/Desktop/Years/2006.txt");
fileMap.put("2007", "/Users/Marque/Desktop/Years/2007.txt");
fileMap.put("2008", "/Users/Marque/Desktop/Years/2008.txt");
fileMap.put("2009", "/Users/Marque/Desktop/Years/2009.txt");
fileMap.put("2010", "/Users/Marque/Desktop/Years/2010.txt");
String years[]={"2006","2007","2008","2009","2010"};
String genders[]={"MALE","FEMALE"};
f=new JFrame("Baby Name Popularity");
yearPanel= new JPanel();
yearLabel =new JLabel("Year");
yearPanel.add(yearLabel);
yearBox=new JComboBox<String>(years);
yearPanel.add(yearBox);
yearPanel.setLayout(new GridLayout(1, 2));
f.add(yearPanel);
genderPanel= new JPanel();
genderLabel =new JLabel("Gender");
genderPanel.add(genderLabel);
genderBox=new JComboBox<String>(genders);
genderPanel.add(genderBox);
genderPanel.setLayout(new GridLayout(1, 2));
f.add(genderPanel);
namePanel= new JPanel();
nameLabel =new JLabel("NAME");
namePanel.add(nameLabel);
tf1 = new JTextField();
namePanel.add(tf1);
namePanel.setLayout(new GridLayout(1, 2));
f.add(namePanel);
JPanel buttonPanel= new JPanel();
buttonPanel.setLayout(new GridLayout(1, 2));
JButton jb =new JButton("Find Ranking");
buttonPanel.add(jb,0);
f.add(buttonPanel);
textPanel= new JPanel();
textField = new JTextField();
textPanel.add(textField);
textPanel.setLayout(new GridLayout(1, 2));
f.add(textPanel);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String year = (String)yearBox.getSelectedItem();
String gender =(String)genderBox.getSelectedItem();
String name =(String)tf1.getText();
readFile("Years");
}
});
f.setLayout(new GridLayout(5, 2,10,20));
f.setSize(400,500);
f.setVisible(true);
}
public static void main(String args[])
{
new Main();
}
public void readFile(String FILENAME){
if(fileMap.containsKey(FILENAME))
FILENAME = fileMap.get("/Users/Marque/Desktop/Years");
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader("/Users/Marque/Desktop/Years");
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(FILENAME));
boolean found =false;
String name = (String)tf1.getText().trim();
while ((sCurrentLine = br.readLine()) != null) {
sCurrentLine=sCurrentLine.replaceAll("\\s+"," ");
for(int i=0;i<sCurrentLine.split(" ").length;i++)
{
String s=sCurrentLine.split(" ")[i];
if(s.equalsIgnoreCase(name)){
found=true;
textField.setText(""+s+" was #"+sCurrentLine.split(" ")[i+1]+" in "+yearBox.getSelectedItem()+"!");
}
}
if(!found)
{
textField.setText("NAME NOT FOUND");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
答案 0 :(得分:0)
参考:
https://docs.oracle.com/javase/7/docs/api/java/io/FileReader.html
该类声明它接受3种类型的参数输入:
FileReader(File file);
FileReader(FileDescriptor fd);
FileReader(String fileName);
在您明确声明需要传递文件或文件位置路径时,您似乎正在传递目录。