以代码来说话吧,以下是实现同一功能的两段不同的代码。
第一段,两个FindChar类代码的对比:
class
FindChar
...
{
public static void main(String[] args) throws Exception...{
if(2!=args.length)...{
throw new 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!");
}
}
public static void main(String[] args) throws Exception...{
if(2!=args.length)...{
throw new 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
...
{
public static void main(String[] args) throws IOException...{
if(2!=args.length)...{
throw new 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!");
}
}
public static void main(String[] args) throws IOException...{
if(2!=args.length)...{
throw new 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
...
{
public static void 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方法,否则看不到输出
}
}
public static void 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方法,否则看不到输出
}
}
class
Concat
...
{
public static void main(String[] args) throws IOException ...{
InputStream in;
// 如果无参数,则用System.in做为输入流,或者取出参数对应的文件做为输入流
if(0==args.length)
in = System.in;
else...{
InputStream fileIn,buffIn;
List<InputStream> inputs = new ArrayList<InputStream>(args.length);
for(int i=0; i<args.length; i++)...{
fileIn = new FileInputStream(args[i]);
buffIn = new BufferedInputStream(fileIn);
inputs.add(buffIn);
}
Enumeration<InputStream> files = Collections.enumeration(inputs);
in = new SequenceInputStream(files);
}
// 把输入流中的信息输出到控制台上来
int ch;
while(-1!=(ch=in.read()))
System.out.write(ch); // 用write方法,用print输出汉字会有问题
System.out.flush(); // 调用flush方法,否则看不到输出
}
}
public static void main(String[] args) throws IOException ...{
InputStream in;
// 如果无参数,则用System.in做为输入流,或者取出参数对应的文件做为输入流
if(0==args.length)
in = System.in;
else...{
InputStream fileIn,buffIn;
List<InputStream> inputs = new ArrayList<InputStream>(args.length);
for(int i=0; i<args.length; i++)...{
fileIn = new FileInputStream(args[i]);
buffIn = new BufferedInputStream(fileIn);
inputs.add(buffIn);
}
Enumeration<InputStream> files = Collections.enumeration(inputs);
in = new SequenceInputStream(files);
}
// 把输入流中的信息输出到控制台上来
int ch;
while(-1!=(ch=in.read()))
System.out.write(ch); // 用write方法,用print输出汉字会有问题
System.out.flush(); // 调用flush方法,否则看不到输出
}
}
结论:差别很大。