JAVA入门————IO流——各种流

数据输入输出流——DateInputStream

  • 优点:可以读写基本数据
 eg;	DataOutputStream bos = new DataOutputStream(new FileOutputStream("A.txt"));
        bos.writeInt(123);
        bos.writeDouble(2.75);
        bos.writeBoolean(true);
        bos.writeByte('a');
        bos.writeUTF("这是一句话");
        DataInputStream bis = new DataInputStream(new FileInputStream("A.txt"));
        System.out.println(bis.readInt());
        System.out.println(bis.readDouble());
        System.out.println(bis.readBoolean());
        System.out.println(bis.readByte());
        System.out.println(bis.readUTF());
        bis.close();
        bos.close();

内存操作流

  • 不关联任何文件 只在内存中读取数据
  • 内存中维护着一个缓冲区 可以往缓冲区中自行写入或读取数据
    关闭无效 所以无需关闭

操作字节数组——ByteArrayInputStream

ByteArrayInputStream( byte[] buf)——创建一个ByteArrayInputStream,使用 buf 作为其缓冲区数组。
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class demo2 {
    public static void main(String[] args) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos.write("好好学习".getBytes());
        baos.write("天天".getBytes());
        baos.write("爱java".getBytes());
        byte[] bytes = baos.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        byte[] bytes1 = new byte[1024 * 8];  //  缓冲区
        int len = bais.read(bytes1);
        System.out.println(len);————输出:31
        String s = new String(bytes1, 0, len);
        System.out.println(s);————输出:好好学习天天爱java
    }
}

操作字符数组——CharArrayWrite

eg		CharArrayWriter charArrayWriter = new CharArrayWriter();
        charArrayWriter.write("aaa");
        charArrayWriter.write("bbb");
        charArrayWriter.write("ccc");
        String ss = charArrayWriter.toString();
        System.out.println(ss);————输出:aaabbbccc

操作字符串数组——StringWriter

 eg:	StringWriter stringWriter = new StringWriter();
        stringWriter.write("abc");
        stringWriter.write("abc");
        stringWriter.write("abc");
        String sss = stringWriter.toString();
        System.out.println(sss);

需求:将两个mp3文件,拼接成一个mp3文件

你可能感兴趣的:(JAVA入门————IO流——各种流)