新建FileWriter对象,Eclipse总是报错的呢

ykpiverson 2012-05-04 11:24:49
String filePath = "C://test.doc";
File file = new File(filePath);
try{
if(!file.exists())
file.createNewFile();
FileWriter fw = new FileWriter(new File(file);//创建文件字符输出流
BufferedWriter bw = new BufferedWriter(fw);

显示错误为The constructor FileOutputStream(File) is undefined
...全文
322 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
五哥 2012-05-04
  • 打赏
  • 举报
回复
import java.io.File;
import java.io.FileOutputStream;

public class Test1 {

public static void main(String[] args) {
File file = new File("C:/","word.txt");
FileOutputStream out = null ;
try{
if(!file.exists()){
file.createNewFile(); //如果文件不存在,则创建新文件
}
out = new FileOutputStream(file);
byte buy[] ="java编程宝典,程序员的黄金搭档".getBytes(); //创建byte型数组
out.write(buy); //将数组的信息写入文件中
}catch(Exception e){
e.printStackTrace();
}finally{
if (out != null ){
try{
out.close() ;
}catch(Exception ex){
ex.printStackTrace() ;
}
}

}
}
}
五哥 2012-05-04
  • 打赏
  • 举报
回复

import java.io.File;
import java.io.FileOutputStream;


public class Test1 {

public static void main(String[] args) {
File file = new File("C:/","word.txt");
try{
if(!file.exists()){
file.createNewFile(); //如果文件不存在,则创建新文件
}
FileOutputStream out = new FileOutputStream(file);
byte buy[] ="java编程宝典,程序员的黄金搭档".getBytes(); //创建byte型数组
out.write(buy); //将数组的信息写入文件中
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
}


以上代码没有问题

ykpiverson 2012-05-04
  • 打赏
  • 举报
回复
错了,显示错误为The constructor FileWriter(File) is undefined。
刚才写的错误时这段代码的
File file = new File("C:/","word.txt");
try{
if(!file.exists()){
file.createNewFile(); //如果文件不存在,则创建新文件
}
FileOutputStream out = new FileOutputStream(file);
byte buy[] ="java编程宝典,程序员的黄金搭档".getBytes(); //创建byte型数组
out.write(buy); //将数组的信息写入文件中
out.close();
}catch(Exception e){
e.printStackTrace();
}
五哥 2012-05-04
  • 打赏
  • 举报
回复
像你的程序 ,使用第二种写法比较好点。即 3楼的回答
ykpiverson 2012-05-04
  • 打赏
  • 举报
回复
我找的原因了,原来是因为我 把类名写为FileOutputStream了,另一个也是这样的错误

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧