importjava.io.*;publicclass写文件{publicstaticvoidmain(String[]args)throwsArrayIndexOutOfBoundsException{byte[]buff=newbyte[1023];booleancont=true;//循环控制变量FileOutpu...
import java.io.*;
public class 写文件 {
public static void main(String[] args)
throws ArrayIndexOutOfBoundsException {
byte[] buff = new byte[1023];
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);
}
}
}
请高手帮忙,怎么就搞不明白错在什么地方了
展开