File 我怎样才能在这段代码中添加IOException?
这是我的主要课程:File 我怎样才能在这段代码中添加IOException?,file,ioexception,filewriter,printwriter,throws,File,Ioexception,Filewriter,Printwriter,Throws,这是我的主要课程: public class Table extends java.applet.Applet implements Runnable { public void init() { Balla.addBall(); } } 这是巴拉方法: public static void addBall()throws IOException { Random generator = new Random();
public class Table extends java.applet.Applet implements Runnable
{
public void init()
{
Balla.addBall();
}
}
这是巴拉方法:
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()无法覆盖小程序中的init() 我怎样才能绕过这个问题和/或如何在不修改大部分内容的情况下修复这个问题?更改此选项
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();
}
到
然后看看它是否工作我的意思是它消除了错误,但实际上似乎从来没有写入文件;这是最基本的,将路径更改为其他驱动器,例如D:,您应该通过。也请接受答案
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)
{
}
}