| |
/* readfile.java 读取文件的内容,并将原样输出至屏幕上 使用方法:java readfile 文件名 */
import java.io.*;
public class readfile { public static void main(string[] args) { byte[] buff = new byte[1024]; boolean cont = true; fileinputstream infile = null;
// 生成对象infile 准备读取文件 try { infile = new fileinputstream(args[0]); } catch (filenotfoundexception e) { system.err.println("没有找到文件"); system.exit(1); }
while (cont) { try { int n = infile.read(buff); // 从文件读取数据 system.out.write(buff, 0, n); // 写入system.out中 } catch (exception e) { cont = false; } }
try { infile.close(); } catch (ioexception e) { system.err.println("文件错误"); system.exit(1); } } }
|
|