2014-05-11 199 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