Java中其它输入输出流_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java中其它输入输出流

Java中其它输入输出流

 2018/10/22 9:18:32  andrew7676  程序员俱乐部  我要评论(0)
  • 摘要:1.ByteArrayInputStream流以array数组为对象读取。(char)c转换成char类型才能打印真正的对象内容。in.reset();重载流读写的位置。importjava.io.ByteArrayInputStream;publicclassByteArrayInputStreamTest1{publicstaticvoidmain(String[]args){Stringtemp="abc";byte[]b=temp.getBytes()
  • 标签:输出 Java
1. ByteArrayInputStream流
class="java" name="code">
以array数组为对象读取。
(char)c 转换成char类型才能打印真正的对象内容。
in.reset();重载流读写的位置。

import java.io.ByteArrayInputStream;
public class ByteArrayInputStreamTest1 {
    public static void main(String[] args) {
        String temp = "abc";
        byte[] b = temp.getBytes();
        ByteArrayInputStream in = new ByteArrayInputStream(b);
        for (int i = 0; i < temp.length(); i++) {
            int c;
            while(-1 != (c = in.read())){
                if (0 == i){
                    System.out.println((char)c);
                } else {
                    System.out.println(Character.toUpperCase((char)c));
                }
            }
        }
        System.out.println();
        in.reset();
    }
}


2. ByteArrayOutputStream流
将array数组的内容输出到文件中。

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class ByteArrayOutputStreamTest1{
    public static void main(String[] args) throws Exception{
        ByteArrayOutputStream f = new ByteArrayOutputStream();
        String str = "hello world welcome";
        byte[] buffer = str.getBytes();
        f.write(buffer);
        byte[] result = f.toByteArray();
        for(int i = 0; i < result.length; i++){
            System.out.println((char)result[i]);
        }
        OutputStream os = new FileOutputStream("test.txt");
        f.writeTo(os);
        f.close();
        os.close();
    }
}


3. DataStream流
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("data.txt")));
写入的是二进制的文件。
DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream("data.txt")));
读取文件中的类型的内容。(先写什么,先读什么(否则会发生读取数据不正确))

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class DataStream1 {
    public static void main(String[] args) throws Exception {
        DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("data.txt")));
        byte b = 3;
        int i = 12;
        char ch = 'a';
        float f = 3.3f;
        dos.writeByte(b);
        dos.writeInt(i);
        dos.writeChar(ch);
        dos.writeFloat(f);
        dos.close();
        DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream("data.txt")));
        // 读和写的顺序要保持一致
        System.out.println(dis.readByte());
        System.out.println(dis.readInt());
        System.out.println(dis.readChar());
        System.out.println(dis.readFloat());
        dis.close();
    }
}


4. 自定义
import java.io.IOException;
import java.io.InputStream;
public class MyOwnStream1{
    public static void main(String[] args) throws Exception{
        byte[] b = new byte[16];
        for(int i = 0; i < b.length; i++){
            b[i] = (byte)i;
        }
        MyByteArrayInputStream mbais = new MyByteArrayInputStream(b);
        while(true){
            int c = mbais.read();
            if(c < 0){
                break;
            }
            System.out.print(c + " ");
        }
        System.out.println();
    }
}
class MyByteArrayInputStream extends InputStream{
    protected byte[] data;
    protected int ptr = 0;
    public MyByteArrayInputStream(byte[] b){
        this.data = b;
    }
    @Override
    public int read() throws IOException{
        return (ptr < data.length) ? (data[ptr++]) : -1;
    }
}

read()方法的实现。

import java.io.IOException;
import java.io.InputStream;
public class MyOwnStream2 extends InputStream{
    protected byte[] data;
    protected int ptr = 0;
    protected int mark = 0;
    public MyOwnStream2(byte[] b){
        this.data = b;
    }
    public int read(){
        return (ptr < data.length) ? (data[ptr++]) : -1;
    }
    @Override
    public int available() throws IOException{
        return data.length - ptr;
    }
    @Override
    public void close() throws IOException{
        ptr = data.length;
    }
    @Override
    public synchronized void mark(int readlimit){
        this.mark = readlimit;
    }
    @Override
    public synchronized void reset() throws IOException{
        if(mark < 0 || mark >= data.length){
            throw new IOException("the position is not valid");
        }
        ptr = mark;
    }
    @Override
    public boolean markSupported(){
        return true;
    }
    @Override
    public int read(byte[] b, int off, int len) throws IOException{
        if(this.ptr >= data.length || len < 0){
            return -1;
        }
        if((this.ptr + len) > data.length){
            len = data.length - this.ptr;
        }
        if(len == 0){
            return 0;
        }
        System.arraycopy(data, ptr, b, off, len);
        ptr += len;
        return len;
    }    
}
发表评论
用户名: 匿名