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

    xiaoxiao2022-07-07  211

    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(); } } } } } }

    运行结果:

    最新回复(0)