请高手指教一下,这两种写法有什么区别 |
|
40分 |
第一种创建具有指定文件名称且不带自动行刷新的新 PrintWriter。此便捷构造方法创建必要的中间 OutputStreamWriter,后者将使用此 Java 虚拟机实例的默认 charset 进行字符编码。 ,第二种就是你自己指定了OutputStreamWriter,因为FileWriter是OutputStreamWriter的子类
|
PrintWriter out=new PrintWriter(“OutFile.txt”);
等价于 Writer writer1=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(“OutFile.txt”))); PrintWriter out=new PrintWriter(writer1); ————————————— PrintWriter out=new PrintWriter(new FileWriter(“OutFile.txt”)); 中的FileWriter(“OutFile.txt”) 会创建FileOutputStream public FileWriter(String fileName) throws IOException { super(new FileOutputStream(fileName)); } FileWriter 是 BufferedWriter的子类 http://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html |