Java Read files

Source: Internet
Author: User
Tags exit readfile

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.