这个是我第一次敲的读取输入的方法运行,可以正常
static HashMap ops=new HashMap();
static{
try {
File f1=new File("E:\\app.txt");
FileWriter fw = new FileWriter(f1);
BufferedWriter bw=new BufferedWriter(fw);
bw.write("+:com.cisst.Add");
bw.newLine();
bw.write("-:com.cisst.Jian'");
bw.newLine();
bw.close();
fw.close();
FileReader fr=new FileReader(f1);
BufferedReader br=new BufferedReader(fr);
String line=null;
String[] sps=null;
while((line=br.readLine())!=null){
sps=line.split(":");
ops.put(sps[0],sps[1]);
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Calculator createJiSuan(String op) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
Calculator ca = null;
ca = (Calculator)Class.forName(ops.get(op)).newInstance();
return ca;
这是我第二敲的,两个基本一样,这个就是运行报错
static HashMap<String,String> ops=new HashMap<String,String>();
static{
try {
File f1=new File("E:\\yunsuan.txt");
FileWriter fw=new FileWriter(f1);
BufferedWriter bw=new BufferedWriter(fw);
bw.write("+:com.lianxi.Add");
bw.newLine();
bw.write("-:com.lianxi.Jian'");
bw.newLine();
bw.close();
fw.close();
FileReader fr=new FileReader(f1);
BufferedReader br=new BufferedReader(fr);
String line=null;
String[] sps=null;
while((line=br.readLine())!=null){
sps=line.split(":");
ops.put(sps[0],sps[1]);
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Calculator createJiSuan(String op) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
Calculator ca = null;
ca = (Calculator)Class.forName(ops.get(op)).newInstance();
return ca;
求指教
是不是你的txt文件格式有问题?