为什么运行结果只显示这个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class BufferStream { public static void main(String[] args) { // TODO Auto-generated method stub try { System.out.println( "one.txt不用缓冲流来写:" ); FileOutputStream fos= new FileOutputStream( "one.txt" , true ); FileInputStream fis= new FileInputStream( "one.txt" ); long start=System.currentTimeMillis(); int t= 0 ; while (t< 10 ){ char i= 'a' ; fos.write(i); t++; } int n= 0 ; while ((n=fis.read())!=- 1 ){ System.out.print(( char )n); } long end=System.currentTimeMillis(); System.out.println( "\n" + "用时:" +(end-start)); fos.close(); fis.close(); System.out.println( "**********************************" ); System.out.println( "two.txt用缓冲流来写:" ); FileOutputStream fos2= new FileOutputStream( "two.txt" , true ); BufferedOutputStream bos= new BufferedOutputStream(fos2); FileInputStream fis2= new FileInputStream( "two.txt" ); BufferedInputStream bis= new BufferedInputStream(fis2); long start1=System.currentTimeMillis(); int t1= 0 ; while (t1< 10 ){ char i= 'a' ; bos.write(i); t1++; } //bos.flush(); int n1= 0 ; while ((n1=bis.read())!=- 1 ){ System.out.print(( char )n1); } long end1=System.currentTimeMillis(); System.out.println( "用时:" +(end1-start1)); bos.close(); bis.close(); fos2.close(); fis2.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); } } } |
0
收起
正在回答 回答被采纳积分+1
3回答
好帮手慕小班
2019-07-16 20:02:05
同学你好,本题目为了展示加上缓冲流的效率更高哦!并没有要求读出数据,所以这里可以不使用read方法来读哦!并且根据题目要求,向文件中写入100000个字符哦,例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | public class BufferStream { public static void main(String[] args) { // TODO Auto-generated method stub try { System.out.println( "one.txt不用缓冲流来写:" ); FileOutputStream fos= new FileOutputStream( "one.txt" , true ); // FileInputStream fis=new FileInputStream("one.txt"); long start=System.currentTimeMillis(); int t= 0 ; while (t< 100000 ){ char i= 'a' ; fos.write(i); t++; } // int n=0; // while((n=fis.read())!=-1){ // System.out.print((char)n); // } long end=System.currentTimeMillis(); System.out.println( "\n" + "用时:" +(end-start)); fos.close(); // fis.close(); System.out.println( "**********************************" ); System.out.println( "two.txt用缓冲流来写:" ); FileOutputStream fos2= new FileOutputStream( "two.txt" , true ); BufferedOutputStream bos= new BufferedOutputStream(fos2); //FileInputStream fis2=new FileInputStream("two.txt"); // BufferedInputStream bis=new BufferedInputStream(fis2); long start1=System.currentTimeMillis(); int t1= 0 ; while (t1< 100000 ){ char i= 'a' ; bos.write(i); t1++; } //bos.flush(); // int n1=0; // while((n1=bis.read())!=-1){ // System.out.print((char)n1); // } long end1=System.currentTimeMillis(); System.out.println( "用时:" +(end1-start1)); bos.close(); // bis.close(); fos2.close(); // fis2.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); } } } |
运行效果如下:
这就是题目要求的效果哦!
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧