java notfond,未报告的异常java.io.FileNotFoundException;必须被抓住或宣布被抛出

I am creating a class -- just a class, no main() and I am receiving the error of "unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown" at this line:

FileOutputStream outStr = new FileOutputStream(FILE, true);

I don't understand; I put in a try{} catch{} block and it's still reporting the error.

Additionally, it's also reporting an "illegal start of type" for the try and both catch lines, and it's also saying that ';' is expected for both catch lines.

I'm using the NetBean IDE, FYI.

Thank you for any help.

Here is the full code:

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.PrintStream;

import java.io.FileNotFoundException;

public class UseLoggingOutputStream

{

String FILE = "c:\\system.txt";

try

{

FileOutputStream outStr = new FileOutputStream(FILE, true);

}

catch(FileNotFoundException fnfe)

{

System.out.println(fnfe.getMessage());

}

catch(IOException ioe)

{

System.out.println(ioe.getMessage());

}

}

解决方案

You need to put the file processing statements inside a method:

import java.io.FileOutputStream;

import java.io.FileNotFoundException;

public class UseLoggingOutputStream {

public void myMethod() {

String file = "c:\\system.txt";

try {

FileOutputStream outStr = new FileOutputStream(file, true);

} catch(FileNotFoundException fnfe) {

System.out.println(fnfe.getMessage());

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值