我这边的IDE,缓冲输出流的构造方法不支持放一个文件输出流进去,以附代码,请赐教!谢谢

我这边的IDE,缓冲输出流的构造方法不支持放一个文件输出流进去,以附代码,请赐教!谢谢

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.imooc.step03.h_iostream.h3_16;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
 
/**
 * 缓冲输出流测试类
 
 * @author 15136
 *
 */
public class BufferedOutputStream {
 
    public static void main(String[] args) {
 
        // 创建一个文件
        File txtFile = new File("D:/Test/file/newFile.txt");
        // 如果文件不存在则创建,存在则不创建
        if (!txtFile.exists()) {
            try {
                txtFile.createNewFile();
            catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("文件创建成功!");
        else {
            System.out.println("文件不存在,无需再创建!");
        }
 
        // 开始时间、结束时间
        long startTime01 = System.currentTimeMillis();
        long endTime01 = System.currentTimeMillis();
        // 所用时间
        long useTime01 = 0;
        try {
            // 使用文件输出流写入字符到文件
            FileOutputStream fos01 = new FileOutputStream(txtFile);
            for (int i = 1; i <= 100000; i++) {
                try {
                    fos01.write(1);
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            useTime01 = endTime01 - startTime01;
            System.out.println(txtFile.getName() + "不使用缓冲流来写");
            System.out.println("用时为:" + useTime01);
            // 关闭文件输出流
            fos01.close();
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
 
        // 开始时间、结束时间
        long startTime02 = System.currentTimeMillis();
        long endTime02 = System.currentTimeMillis();
        // 所用时间
        long useTime02 = 0;
        try {
            // 使用文件输出流写入字符到文件
            OutputStream fos02 = new FileOutputStream(txtFile);
            BufferedOutputStream bos = new BufferedOutputStream(fos02);
            for (int i = 1; i <= 100000; i++) {
                try {
                    bos.write(1);
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            useTime02 = endTime02 - startTime02;
            System.out.println(txtFile.getName() + "使用缓冲流来写");
            System.out.println("用时为:" + useTime02);
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
         
    }
 
}

http://img1.sycdn.imooc.com//climg/5cf480090001797205160138.jpg

http://img1.sycdn.imooc.com//climg/5cf4800a0001e52506370299.jpg


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,类名不能使用BufferedOutputStream,否则会识别不出来io包下的BufferedOutputStream类。

http://img1.sycdn.imooc.com//climg/5cf48d970001f05c03940164.jpg

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

  • 易表非凡 提问者 #1
    对自己表示很绝望
    2019-06-03 11:04:32
  • 易表非凡 提问者 #2
    老师我感觉我没救了,Java从入门到放弃
    2019-06-03 11:05:26
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码