接收键盘的输入,并原样输出到屏幕上
此外,还有将键盘输入的数据按顺序存放到文件中
使用方法: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);
}
}
}
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!


