Java创建文件出错:Error - java.io.IOException: No such file or directory

王桑的一天 2011-09-27 02:12:18

public void writeMsg(String text,String fileName)
try {
File file = new File(fileName);
if (!file.exists())
{
file.createNewFile();
}
FileWriter file1 = new FileWriter(fileName,true);
file1.write(text+"\r\n");
file1.flush();
file1.close();
} catch (IOException e) {
}
}

writeMsg("hello","C:\\123.txt");

09-27 13:27:35.068: ERROR/writeMsg(1137): Error - java.io.IOException: No such file or directory

如果我先手动创建123.txt,也一样出错。我用的是Eclipse
...全文
1862 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyWalker_ONLY 2011-09-27
  • 打赏
  • 举报
回复
没有错误的代码让楼主调出了错误,楼主果然厉害
王桑的一天 2011-09-27
  • 打赏
  • 举报
回复
我再看看,谢谢大家
柯本 2011-09-27
  • 打赏
  • 举报
回复
我也试了下,上面的代码的确没问题,不行的话,调试下,看看到底哪里出错了
lijiemissfei 2011-09-27
  • 打赏
  • 举报
回复
你的代码木有问题的。你的系统是Windows系统吧。Linux就是 / .你在我的电脑输入栏上输入C:\123.txt,看是否能打开你的文件。如果这样可以找到,File类就可以找到的。
台城柳月 2011-09-27
  • 打赏
  • 举报
回复


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Test
{

public static void main(String[] args)
{
writeMsg("hello","C:\\123.txt");

}

public static void writeMsg(String text, String fileName)
{
try
{
File file = new File(fileName);
if (!file.exists())
{
file.createNewFile();
}
FileWriter file1 = new FileWriter(fileName, true);
file1.write(text + "\r\n");
file1.flush();
file1.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}

}




我刚试了下,好像没有问题,只是楼主的代码不完整,
writeMsg(String text,String fileName)
后面少"{"括号,
catch (IOException e) {}里少了异常处理

62,634

社区成员

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

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