服务热线:13616026886

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

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

使用bufferedreader读取文件

/*
 * copyright (c) ian f. darwin, http://www.darwinsys.com/, 1996-2002.
 * all rights reserved. software written by ian f. darwin and others.
 * $id: license,v 1.8 2004/02/09 03:33:38 ian exp $
 *
 * redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * this software is provided by the author and contributors ``as is''
 * and any express or implied warranties, including, but not limited
 * to, the implied warranties of merchantability and fitness for a particular
 * purpose are disclaimed.  in no event shall the author or contributors
 * be liable for any direct, indirect, incidental, special, exemplary, or
 * consequential damages (including, but not limited to, procurement of
 * substitute goods or services; loss of use, data, or profits; or business
 * interruption) however caused and on any theory of liability, whether in
 * contract, strict liability, or tort (including negligence or otherwise)
 * arising in any way out of the use of this software, even if advised of the
 * possibility of such damage.
 
 * java, the duke mascot, and all variants of sun's java "steaming coffee
 * cup" logo are trademarks of sun microsystems. sun's, and james gosling's,
 * pioneering role in inventing and promulgating (and standardizing) the java 
 * language and environment is gratefully acknowledged.
 
 * the pioneering role of dennis ritchie and bjarne stroustrup, of at&t, for
 * inventing predecessor languages c and c++ is also gratefully acknowledged.
 */

import java.io.bufferedreader;
import java.io.filenotfoundexception;
import java.io.filereader;
import java.io.ioexception;
import java.io.inputstreamreader;

/**
 * read a file and print, using bufferedreader and system.out
 */
public class catfile {

  public static void main(string[] av) {
    catfile c = new catfile();
    if (av.length == 0)
      c.process(new bufferedreader(new inputstreamreader(system.in)));
    else
      for (int i = 0; i < av.length; i++)
        try {
          c.process(new bufferedreader(new filereader(av[i])));
        catch (filenotfoundexception e) {
          system.err.println(e);
        }
  }

  /** print one file, given an open bufferedreader */
  public void process(bufferedreader is) {
    try {
      string inputline;

      while ((inputline = is.readline()) != null) {
        system.out.println(inputline);
      }
      is.close();
    catch (ioexception e) {
      system.out.println("ioexception: " + e);
    }
  }
}

扫描关注微信公众号