`

文件中的流

阅读更多

1、base64inputStram

public class Base64InputStream extends FilterInputStream {

    private byte[] decodedContent;

    private int pos;

    private int count;

    public Base64InputStream(InputStream in) throws IOException {
        super(in);
        pos = 0;
        byte[] encoded = new byte[in.available()];
        this.in.read(encoded);

        if (Base64.isArrayByteBase64(encoded)) {
            decodedContent = Base64.decodeBase64(encoded);
        }
        else {
            decodedContent = encoded;
        }
        count = decodedContent.length;
    }

    public synchronized int read(byte[] b) throws java.io.IOException {
        int len = 0;
        int off = 0;
        len = b.length;
        return read(b, off, len);
    }

    public synchronized int read(byte[] b, int off, int len) throws java.io.IOException {
        if (b == null) {
            throw new NullPointerException();
        }
        else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) {
            throw new IndexOutOfBoundsException();
        }
        if (pos >= count) {
            return -1;
        }
        if (pos + len > count) {
            len = count - pos;
        }
        if (len <= 0) {
            return 0;
        }
        System.arraycopy(decodedContent, pos, b, off, len);
        pos += len;
        return len;

    }

    public synchronized int read() throws java.io.IOException {
        if (pos >= count) {
            return -1;
        }
        return decodedContent[pos++] & 0xff;

    }

    public int available() throws java.io.IOException {
        return count;
    }

    public long skip(long n) throws java.io.IOException {
        if (pos + n > count) {
            n = count - pos;
        }
        if (n < 0) {
            return 0;
        }
        pos += n;
        return n;

    }

    public boolean markSupported() {
        return false;
    }

    public String toString() {
        try {
            return new String(decodedContent, "GBK");
        }
        catch (UnsupportedEncodingException ex) {
            throw new RuntimeException("不支持的编码规范:GBK");
        }
    }

	public byte[] getDecodedContent() {
		return decodedContent;
	}
}

 2、将字节流转化为文件

byte[] b="已知的信息"
FileOutputStream fos = new FileOutputStream(new File("D:/img12.jpg"));
fos.write(b, 0, b.length);
fos.flush();
 

 

3、读取文件为字节流

FileInputStream fs = new FileInputStream("c://bugs.jpg"); 
byte[] b = new byte[fs.available()]; 
fs.read(b); 
fs.close();

 4、将文件流转化为字节流

public String encode(InputStream in)throws IOException{
		sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
		byte[] data = new byte[in.available()];
		in.read(data);
		return encoder.encode(data);
	}
	
	public byte[] decode(String base64Str)throws IOException{
		sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
		return decoder.decodeBuffer(base64Str);
	}

 5将文件读入并输出为文件

FileOutputStream fos = new FileOutputStream(new File("D:/img12.jpg"));

			byte[] b = new byte[1024];
			int i = 0;
			FileInputStream fis = new java.io.FileInputStream(“D;/imag11.jpg”);
			while ((i = fis.read(b)) > 0) {
				fos .write(b, 0, i);
			}
			fos .flush();
			fos .close();

 6、将文件内容写入文件中,即String串

fileOutputStream.write(string.getBytes());
fileOutputStream.flush();
fileOutputStream.close();

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics