/* 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);
}
}
}
java写文件
最后更新 2017-11-12 08:00 星期日 所属:
JAVA 教程 浏览:453
