所以我得到了ClassCastException:无法将java.lang.Class强制转换为com.glostrode.Management.Link在以下方法(文件的第32行)中指示的行上,发生异常的行:public Link getLink(String name) { txtFile f = new txtFile("plugins/PrimeManager/linksList.txt"); List<String> lines = f.getLines(); int i; txtFile f2; for(i=0; i<lines.size(); i++){ f2 = new txtFile("plugins/PrimeManager/links/"+lines.get(i)+".txt"); if(f2.getLines().isEmpty()) { return null; } Object o = f2.getObject(); Link li = (Link) o;// THIS LINE HERE if(li.name == lines.get(i)){ return li; } } return null;}该getLines()方法返回,其中List<String>包含txtFile对象的初始化指定的文件中的行。该getObject()方法如下:public Object getObject(){ try { FileInputStream i = new FileInputStream(this.file); ObjectInputStream o = new ObjectInputStream(i); Object r = o.readObject(); o.close(); i.close(); return r; } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } return null; }}根据我目前的理解,所返回的对象o.readObject()应可转换为其原始形式(读取的文件包含一个Link对象)。我使用以下方法将其添加到文件中:public void addObject(Object obj){ try { FileOutputStream f = new FileOutputStream(this.file); ObjectOutputStream o = new ObjectOutputStream(f); o.writeObject(obj); o.flush(); o.close(); f.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }}txtFile构造函数:public txtFile(String path) { this.path = path; this.file = new File(path); if(!this.file.exists()){ try { this.file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } }}
添加回答
举报
0/150
提交
取消