class FindChar...{ publicstaticvoid main(String[] args) throws Exception...{ if(2!=args.length)...{ thrownew Exception("need char and file"); } char ch = args[0].charAt(0); FileReader fileIn =new FileReader(args[1]); LineNumberReader in =new LineNumberReader(fileIn); int r; while(-1!=(r=in.read()))...{ if(ch==r)...{ System.out.println("char "+ch+" at line "+in.getLineNumber()); return; } } System.out.println("char "+ch+" not found!"); } }
class FindChar ...{ publicstaticvoid main(String[] args) throws IOException...{ if(2!=args.length)...{ thrownew IllegalArgumentException("need char and file"); } int match = args[0].charAt(0); FileReader fileIn =new FileReader(args[1]); LineNumberReader in =new LineNumberReader(fileIn); int ch; while(-1!=(ch=in.read()))...{ if(match==ch)...{ System.out.println("'"+(char)ch+"' at line "+in.getLineNumber()); return; } } System.out.println("'"+(char)match+"' not found!"); } }
第二段,两个Concat 类代码的对比:
class Concat ...{ publicstaticvoid main(String[] args) throws IOException ...{ InputStream in; // 如果无参数,则用System.in做为输入流,或者取出参数对应的文件做为输入流 if(0==args.length) in = System.in; else...{ ArrayList<BufferedInputStream> arrayList =new ArrayList<BufferedInputStream>(args.length); for(int i=0; i<args.length; i++)...{ FileInputStream fi =new FileInputStream(args[i]); BufferedInputStream bi =new BufferedInputStream(fi); arrayList.add(bi); } SequenceInputStream si =new SequenceInputStream(Collections.enumeration(arrayList)); in = si; } // 把输入流中的信息输出到控制台上来 int ch; while(-1!=(ch=in.read())) System.out.write(ch); // 用write方法,用print输出汉字会有问题 System.out.flush(); // 调用flush方法,否则看不到输出 } }