网站首页 新闻首页 网页设计图形动画软件编程网站开发办公软件操作系统数据库网络技术认证考试范文资料黑客攻防 书籍教程 进入论坛

java写文件

http://www.diybl.com/ 2007-2-6  网络 点击:  [ 评论 ]
文章搜索:    【点击打包该文章】

/* Writefile.java
接收键盘的输入,并原样输出到屏幕上
此外,还有将键盘输入的数据按顺序存放到文件中
使用方法:java Writefile 文件名
要结束此程序时,请在行的开始部分输入一个#号。
*/

import java.io.*;

// Class Writefile
public class Writefile
{
public static void main(String[] args)
{
byte[] buff = new byte[1024];
boolean cont = true; // 循环控制变量
FileOutputStream outfile = null; // 文件输出对象

// 生成对象outfile,准备输出到文件
try
{
outfile = new FileOutputStream(args[0]);
}
catch (FileNotFoundException e)
{
System.err.println("文件不存在");
System.exit(1);
}

// 行首没有输入句号时执行如下循环
while (cont)
{
try
{
int n = System.in.read(buff); // 从System.in读入数据
System.out.write(buff, 0, n); // 写入到System.out中
if (buff[0]==´#´)
{
cont = false;
}
else
{
outfile.write(buff, 0, n);
}

}
catch (Exception e)
{
System.exit(1);
}
}

// 关闭文件
try
{
outfile.close();
}
catch (IOException e)
{
System.err.println("文件错误");
System.exit(1);
}
}
}

欢迎光临DIY部落,点击这里查看更多文章教程   【点击打包该文章】
如果图片或页面不能正常显示请点击这里 站内搜索:   

文章评论

请您留言