[英]I can't use PrinterWriter in my program, could anyone help me figure it out?
imageToPPMFile(picture,ysize,xsize,maxIntensity,fname);
}//end of main//
public static void imageToPPMFile (int[][][]image, int rows, int cols, int maxintensity, String fname) throws Exception
我在这里尝试使用的PrintWriter
不会将颜色打印到文件'fname',因为当我抛出上面的Exception时,程序会要求声明或捕获。 但是,例外是我的老师给我的,因此我需要保留它。 谁能告诉我PrintWriter
和/或Exception
怎么了?
PrintWriter outp = new PrintWriter(fname);
int ysize = rows;
int xsize = cols;
int red, green, blue;
outp.println("P3");
outp.println(rows + " " + cols);
outp.println(maxintensity);
for (int r=0; r<ysize; r++)
{ for (int c=0; c<xsize; c++)
{ red = image[c][r][0];
outp.print(red + " ");
green = image[c][r][1];
outp.print(green + " ");
blue = image[c][r][2];
outp.print(blue + " ");
}
}//Adding a PrintWriter.outp.close() here results in the variable not being found
}
}
在第一种情况下,您应该close
PrintWriter
。 在与创建PrintWriter对象相同的作用域中调用outp.close()
。
至于使用throws Exception
,请看以下示例,您将了解它:
public static void foo() throws Exception {
// Some code here. Possible occurring of an error.
}
要正确使用此方法,应在try-catch
块或声明throws Exception
另一个方法中调用此方法。 如从下面的main
方法:
public static void main(String args[]) {
// Your other code
// Call the method that may throw an exception
try {
foo();
} catch(Exception ex) {
}
// Any other code you want
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.