java-如何用输入流写3000遍“爱你三千次”。

A:IO流概述
IO流用来处理设备之间的数据传输
Java对数据的操作是通过流的方式
Java用于操作流的对象都在IO包中
B:IO流分类
a:按照数据流向
输入流 读入数据
输出流 写出数据
b:按照数据类型
字节流 可以读写任何类型的文件 比如音频 视频 文本文件
字符流 只能读写文本文件

用字节输出流写3000遍“爱你三千次”。
package Demo;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MyTest {
    public static void main(String[] args) throws FileNotFoundException {

        FileOutputStream out=null;
        for (int i = 1; i <= 3000; i++) {
            out= new FileOutputStream("a.txt",true);

            try {
                if(i%9==0){
                    out.write("爱你三千遍!".getBytes());
                    out.write("\r\n".getBytes());
                }else{

                    out.write("爱你三千遍!".getBytes());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(out!=null){
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
        }
        
}

运行结果:
在这里插入图片描述

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值