2014-05-11 144 views
0

这是我的主类:如何将IOExcpetion添加到此代码?

public class Table extends java.applet.Applet implements Runnable 
{ 
    public void init() 
    { 
     Balla.addBall(); 
    } 
} 

这是巴拉方法:当我从complie它

public static void addBall()throws IOException 
    { 
     Random generator = new Random(); 
     Ball b = new Ball(100,100,8,Color.blue,generator.nextInt(4)+1,generator.nextInt(4)+1); 
     FileWriter fw = new FileWriter("C:\\temp_Jon\\BallData.ballz",true); 
     PrintWriter pw = new PrintWriter(fw,true); 
     pw.print(b.getX()+" "+b.getY()+" "+b.getRadius()+" "+b.color+" "+b.speedX+" "+b.speedY); 
     fw.close(); 
     pw.close(); 
    } 

现在我的问题来了。它告诉我,我在init方法未报告的IOException异常,但是当我添加的方法抛出IOException异常它告诉我:
错误:的init()表中的Applet

不能覆盖的init()我怎样才能解决这个问题和/或如何解决这个问题而不用修改所有的东西?

回答

0

改变这种

public static void addBall()throws IOException 
    { 
     Random generator = new Random(); 
     Ball b = new Ball(100,100,8,Color.blue,generator.nextInt(4)+1,generator.nextInt(4)+1); 
     FileWriter fw = new FileWriter("C:\\temp_Jon\\BallData.ballz",true); 
     PrintWriter pw = new PrintWriter(fw,true); 
     pw.print(b.getX()+" "+b.getY()+" "+b.getRadius()+" "+b.color+" "+b.speedX+" "+b.speedY); 
     fw.close(); 
     pw.close(); 
    } 

public static void addBall() 
    { 
     Random generator = new Random(); 
     Ball b = new Ball(100,100,8,Color.blue,generator.nextInt(4)+1,generator.nextInt(4)+1); 
     try 
{ 
     FileWriter fw = new FileWriter("C:\\temp_Jon\\BallData.ballz",true); 
     PrintWriter pw = new PrintWriter(fw,true); 
     pw.print(b.getX()+" "+b.getY()+" "+b.getRadius()+" "+b.color+" "+b.speedX+" "+b.speedY); 
     fw.close(); 
     pw.close(); 
    } 
catch(Exception e) 
{ 
} 
} 

,看看它是否工作

+0

我的意思是它摆脱了错误,但它实际上并没有以往任何时候都显得写入文件。 – JWPM77

+0

在catch块中添加此行并查看错误:e.printStackTrace(); – Satya

+0

它告诉我,我没有权限编辑/写入文件 – JWPM77