关于向硬盘写入文件的!

GamVan 2004-08-03 05:59:27
下面应该可向硬盘当前目录下写入一个txt文件,代码应该没错,我机器是2003 fat分区格式,应该不存在权限不足的问题吧???

可怎么就是生成不了文件呢????

package java3;
import java.io.*;
public class WriteFileExample
{
public static void main(String[] args)
{
try
{
File aFile=new File("WriteExample.txt"); //指定文件名
//建立输出流
FileOutputStream out= new FileOutputStream(aFile); byte[] b=new byte[1024];
String str="This is a test file";
b=str.getBytes(); //进行String到byte[]的转化
out.write(b); //写入文本内容
}
catch (IOException e)
{
System.out.println(e.toString());
}
}
}
...全文
105 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
asiaec 2004-08-04
  • 打赏
  • 举报
回复
赫赫 是你找不到生成了的文件吧 你指定个绝对路径看看
flywind63 2004-08-04
  • 打赏
  • 举报
回复
在out.write(b)后加上out.close()以关闭输出流
yaiba 2004-08-03
  • 打赏
  • 举报
回复
package java3;
import java.io.*;
public class WriteFileExample
{
public static void main(String[] args)
{
try
{
File aFile=new File("WriteExample.txt"); //指定文件名
//建立输出流
FileOutputStream out= new FileOutputStream(aFile); byte[] b=new byte[1024];
String str="This is a test file";
b=str.getBytes(); //进行String到byte[]的转化
out.write(b); //写入文本内容
}
catch (IOException e)
{
System.out.println(e.toString());
}
}
}

代码没错误
konet 2004-08-03
  • 打赏
  • 举报
回复
代码没有错。流操作完毕后最好加上out.close().
我相信文件已经在你的硬盘上生成(在你电脑的系统目录下)
为方便查看文件,你可以指定路径名。如:File f=new File("E://test.txt");
GamVan 2004-08-03
  • 打赏
  • 举报
回复
谢谢,我再试试!
ntzls 2004-08-03
  • 打赏
  • 举报
回复
2000下测试通过,没有异常提示吗?

2003没用过,权限问题可能性不大,可试用administrator登陆运行。

最后一招加上out.close();
myhotsun 2004-08-03
  • 打赏
  • 举报
回复
没问题啊!我试了可以阿!

62,614

社区成员

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

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