[英]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.