import java.io.*;public class Buffer
{
public static void main(String[] args)
{
String s2="i am a dreamer"
+" i dream of my life away"
+" i am a dreamer"
+" who dreams of better days";
try {
String s;
long t1=System.currentTimeMillis();
BufferedReader in4 = new BufferedReader(
new StringReader(s2));
//in4.reset();
PrintWriter out1 = new PrintWriter(
new BufferedWriter(new FileWriter("IODemo.out")));
int lineCount = 1;
while((s = in4.readLine()) != null )
out1.println(lineCount++ + ": " + s);
out1.close();
long t2=System.currentTimeMillis();
System.out.println(t2-t1);
} catch(EOFException e) {
System.err.println("End of stream");
}
catch(IOException e)
{
throw new RuntimeException(e);
}
try {
int i;
long t1=System.currentTimeMillis();
StringReader in4 = new StringReader(s2);
in4.reset();//企图重新设置流状态
PrintWriter out1 = new PrintWriter("IODemo.in");
int lineCount = 1;
while((i = in4.read()) != -1 )
out1.print((char)i);
out1.close();
long t2=System.currentTimeMillis();
System.out.println(t2-t1);
} catch(EOFException e) {
System.err.println("End of stream");
}
catch(IOException e)
{
throw new RuntimeException(e);
}
}
}程序的输出结果不确定,在我的机器上有的时候是16和0,有的时候是0和16,我觉得应该与流的设置有关,但不知道该怎么改。
{
public static void main(String[] args)
{
String s2="i am a dreamer"
+" i dream of my life away"
+" i am a dreamer"
+" who dreams of better days";
try {
String s;
long t1=System.currentTimeMillis();
BufferedReader in4 = new BufferedReader(
new StringReader(s2));
//in4.reset();
PrintWriter out1 = new PrintWriter(
new BufferedWriter(new FileWriter("IODemo.out")));
int lineCount = 1;
while((s = in4.readLine()) != null )
out1.println(lineCount++ + ": " + s);
out1.close();
long t2=System.currentTimeMillis();
System.out.println(t2-t1);
} catch(EOFException e) {
System.err.println("End of stream");
}
catch(IOException e)
{
throw new RuntimeException(e);
}
try {
int i;
long t1=System.currentTimeMillis();
StringReader in4 = new StringReader(s2);
in4.reset();//企图重新设置流状态
PrintWriter out1 = new PrintWriter("IODemo.in");
int lineCount = 1;
while((i = in4.read()) != -1 )
out1.print((char)i);
out1.close();
long t2=System.currentTimeMillis();
System.out.println(t2-t1);
} catch(EOFException e) {
System.err.println("End of stream");
}
catch(IOException e)
{
throw new RuntimeException(e);
}
}
}程序的输出结果不确定,在我的机器上有的时候是16和0,有的时候是0和16,我觉得应该与流的设置有关,但不知道该怎么改。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货