帮忙看一下这里哪里有错误好吗

来源:百度知道 编辑:UC知道 时间:2024/07/05 01:53:30
import java.io.*;

public class test{
public static void main(String[] args){
int i = 0;
try {
FileInputStream f = new FileInputStream("d:/java/test.java");
} catch (FileNotFoundException e) {
System.out.println("文件未找到");
System.exit(-1);
}
try {
while ((i = f.read()) != -1) {
System.out.print((char)i);
}
f.close();
} catch ( IOException ee){
System.out.println("文件读取错误"); System.exit(-1);
}
}
}

编译说
D:\Java>javac test.java
test.java:17: 非法字符: \65289
} catch ( IOException ee){
^
test.java:20: 需要 '{'
}
^
test.java:21: 需要 '}'
}
^
3 错误

实在不明白

第一个非法字符是你的"){"是全角(中文)环境下输入的,应该改为半角(英文)下输入。这个改过就可以解决了。
不过你还有一个错误就是“f找不到”,这是因为你f的生命周期没搞明白。
我把你的程序改之后为:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class T {
public static void main(String[] args) {
int i = 0;
FileInputStream f=null;
try {
f= new FileInputStream("d:/java/test.java");
} catch (FileNotFoundException e) {
System.out.println("文件未找到");
System.exit(-1);
}
try {
while ((i = f.read()) != -1) {
System.out.print((char)i);
}
f.close();
} catch ( IOException ee){
System.out.println("文件读取错误"); System.exit(-1);
}

}

}

知道了,这个是因为你设置的变量有问题,因为变量有作用域的;
try {
FileInputStream f = new FileInputStream("d:/java/test.java");
} catch (FileNotFoundException e) {
Syste