怎样把string里的东西生成一个文件和把文件的内容读到string中

josephlong 2002-02-25 09:56:57
请给个例子 谢谢
...全文
57 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
rinehart 2002-02-25
  • 打赏
  • 举报
回复
把string里的东西生成一个文件:
FileOutputStream out = null;
out = new FileOutputStream("C:\a.txt");
DataOutputStream textout = null;
textout = new DataOutputStream(out);
String val = "要写入的字符串";
textout.writeBytes(val);
textout.close();
out.close();
rinehart 2002-02-25
  • 打赏
  • 举报
回复
文件的内容读到string中:
FileInputStream in = null;
in = new FileInputStream("C:\a.txt");
int num = in.available();
byte[] buffer = new byte[num];
in.read(buffer);
String str = new String(buffer,0);
hexiaofeng 2002-02-25
  • 打赏
  • 举报
回复
看看java.io.RandomAccessFile 类
Patrick_DK 2002-02-25
  • 打赏
  • 举报
回复
去看看java.io package吧

hccpro 2002-02-25
  • 打赏
  • 举报
回复
sorry,上面忘了in.close();
hccpro 2002-02-25
  • 打赏
  • 举报
回复
写:
PrintWriter out = new PrintWriter(new FileOutputStream("a.txt"));
out.println("xxxx");
out.close();

读:
BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream("a.txt")));
String s = null;
while((s=in.readLine())!=null) System.out.println(s);

23,407

社区成员

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

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