Java io问题, 菜鸟求解

and888 2005-04-10 07:16:10
《Java就业培训教程》P247源码
程序清单:FileStream.java
import java.io.*;
public class FileStream // 输入/出都以字节为单位
{
public static void main(String[] args)
{
File f = new File("hello.txt");
System.out.println("问题1: 创建文件的同时, 文件被打开. 这么说对吗?");
try
{
FileOutputStream out = new FileOutputStream(f); // 创建一个输出流,把内部数据输出到指定的外部 File 对象中。
byte buf[]="www.it315.org".getBytes(); // 按照平台缺省的字符编码方式把该 String 转换成字节,并把结果存到一新的字节数组中。
out.write(buf); // 将指定字节数组中 b.length 字节写入当前文件输出流。
out.close(); // 内存->(外部)文件.
}
catch(Exception e)
{
System.out.println(e.getMessage());
}

System.out.println("问题2: 文件已被关闭, 在未打开的情况下, 怎样读取数据?");
try
{// 把指定文件中的数据读出来
FileInputStream in = new FileInputStream(f);
byte [] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
...全文
110 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
and888 2005-04-11
  • 打赏
  • 举报
回复
又发行了一个新问题: 在输出中

try
{// 把指定文件中的数据读出来
FileInputStream in = new FileInputStream(f);
byte [] buf = new byte[1024]; // 请问哪句指令使文件中的数据读入 byte [] buf
int len = in.read(buf); // 将当前输入流中 b.length 个字节数据读到一个字节数组中
System.out.println(new String(buf,0,len));
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
woanon 2005-04-10
  • 打赏
  • 举报
回复
帮顶
CharlesXavier 2005-04-10
  • 打赏
  • 举报
回复
1,创建文件{File f = new File("hello.txt"); }

1~,打开文件{FileOutputStream out = new FileOutputStream(f);}

1~~,写文件{out.write(buf);}


2,文件关闭{out.close();}

2~,打开文件{FileInputStream in = new FileInputStream(f);}

2~~,读文件{byte [] buf = new byte[1024];int len = in.read(buf);}
and888 2005-04-10
  • 打赏
  • 举报
回复
问题1: 创建文件的同时, 文件被打开. 这么说对吗?");
问题2: 文件已被关闭, 在未打开的情况下, 怎样读取数据?");

oyljerry 2005-04-10
  • 打赏
  • 举报
回复
就是这样的
killme2008 2005-04-10
  • 打赏
  • 举报
回复
问题是什么?

62,614

社区成员

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

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