按你说的。 我把代码改成了 static class Callback1 extends HTMLEditorKit.ParserCallback { try{ static FileWriter f = new FileWriter("c:/temp/output.txt"); public void handleText(char[] data,int pos){ f.write(new String(data)); } }catch(IOException e){ e.getMessage();} } 可是总是提示 Syntax error on token "try", invalid Modifiers 什么意思啊?
有中文么?有中文可能比较麻烦,没有的话试试File t = new File("c:\\test.txt"); DataOutputStream out = new DataOutputStream( new BufferedOutputStream( new FileOutputStream(t))); out.writeBytes("abcabc"); out.close();
BufferedWriter b; try { b = new BufferedWriter(new FileWriter("d:\\1.txt")); b.write("abc可以用中文"); b.close(); } catch (IOException e) { // TODO 自动生成 catch 块 e.printStackTrace(); }
我把代码改成了
static class Callback1 extends HTMLEditorKit.ParserCallback {
try{
static FileWriter f = new FileWriter("c:/temp/output.txt");
public void handleText(char[] data,int pos){
f.write(new String(data));
}
}catch(IOException e){
e.getMessage();}
}
可是总是提示
Syntax error on token "try", invalid Modifiers
什么意思啊?
DataOutputStream out = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(t)));
out.writeBytes("abcabc");
out.close();
BufferedWriter b;
try {
b = new BufferedWriter(new FileWriter("d:\\1.txt"));
b.write("abc可以用中文");
b.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
把try放在函数里.
但我这里的这个问题有点特殊。
能不能针对我的这个程序进行改动呢?谢谢了。
pw.println("xxx")
public static void main(String args[]){
try{
URL url=new URL("http://www.baidu.com");
InputStream is=url.openStream();
InputStreamReader isr=new InputStreamReader(is);
ParserDelegator parser = new ParserDelegator();
HTMLEditorKit.ParserCallback callback1 = new Callback1();
parser.parse( isr, callback1, true);
isr.close();
}catch(Exception e){
e.printStackTrace();
}
}
static class Callback1 extends HTMLEditorKit.ParserCallback{
static FileWriter f = new FileWriter("c:/temp/output.txt");
public void handleText(char[] data,int pos){
f.write(new String(data));
}
}
}
对异常和访问修饰符的概念不是很清晰
所以不知道new FileWriter(String) 会强制要求处理一个IOException 所以不能在成员定义的同时调用
不过,现在这个问题解决了。
结贴,散分!