这个小程序总报错是怎么回事呢?对的啊??

来源:百度知道 编辑:UC知道 时间:2024/06/19 11:24:07
import java.io.*;

public class FileWriter {
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("d:\\bak\\unicode.dat");//报错
for (int c = 0; c <= 50000; c++) {
fw.write(c);//报错
}
fw.close();//报错
} catch (IOException e1) {
e1.printStackTrace();
System.out.println("文件写入错误");
System.exit(-1);
}
}
}

你的fw声明有问题,应该为java.io.FileWriter fw = null
以下是我用FileWriter的方法,可以参考一下:
File file = new File("e:\\my.txt");
FileWriter fw = new FileWriter(file);
fw.write("the file is my file");
fw.close();

java.io.FileWriter fw = null;
try {
fw = new java.io.FileWriter("d:\\bak\\unicode.dat");

这几行要这么写.要带上java.io.FileWriter包名
因为你定义的类和API中的类的名字是一样的,如果不带包名,他默认找的是你的类中的方法,但是你类中没有这些方法,所以报错了

不能用FileWriter做类名 这是已经定义好的类