服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java读文件


  /* 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);
}
}
}

扫描关注微信公众号