字节输出流

字节

ASCII码:一个英文字母(不分大小写)占一个字节的空间,一个中文汉字占两个字节的空间。一个二进制数字序列,在计算机中作为一个数字单元,一般为8位二进制数,换算为十进制。最小值0,最大值255。如一个ASCII码就是一个字节。
UTF-8编码:一个英文字符等于一个字节,一个中文(含繁体)等于三个字节。在流文件中以字节储存。可通过byte.toString();反编译
Unicode编码:一个英文等于两个字节,一个中文(含繁体)等于两个字节。
符号:英文标点占一个字节,中文标点占两个字节。举例:英文句号“.”占1个字节的大小,中文句号“。”占2个字节的大小。

public abstract class

OutputStream

把存在于内存中的各种类型数据转换成byte型然后读取到OutputStream中。进行传输
一般情况,首先new一个输出流,然后往字节里面write byte.
extends Object
implements Closeable, Flushable
IO包中最大抽象类。要使用此类,必须实例化该类的子类。

抽象方法
void close()
Closes this output stream and releases any system resources associated with this stream.
void flush()
Flushes this output stream and forces any buffered output bytes to be written out. 刷新输出流并把之前所有缓存输出
void write(byte[] b)
Writes b.length bytes from the specified byte array to this output stream. 写入
void write(byte[] b, int off, int len)
Writes len bytes from the specified byte array starting at offset off to this output stream. 写入特定长度特定offset(初始位置)的字节
abstract void write(int b)
Writes the specified byte to this output stream.

FileOutputStream

A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open.
Constructors Constructor and Description
FileOutputStream(File file)
Creates a file output stream to write to the file represented by the specified File object.
FileOutputStream(File file, boolean append)
Creates a file output stream to write to the file represented by the specified File object.
FileOutputStream(FileDescriptor fdObj)
Creates a file output stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system.
FileOutputStream(String name)
Creates a file output stream to write to the file with the specified name.
FileOutputStream(String name, boolean append)
Creates a file output stream to write to the file with the specified name.

e.g

package 字节流;

import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;

public class OutputStream {
public static void main(String[] args) throws UnsupportedEncodingException {
String s = “h是的撒打我elloword”;
byte[] b = s.getBytes(“UTF-8”);
java.io.OutputStream out = new FileOutputStream(file);
out.write(b);
out.close();

}

}

Method Summary
same as OutputStream`s method

ByteArrayOutputStream

This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it. The data can be retrieved using toByteArray() and toString().
Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

Fields
protected byte[] buf
The buffer where data is stored.
protected int count
The number of valid bytes in the buffer.

Constructor and Description
ByteArrayOutputStream()
Creates a new byte array output stream.
ByteArrayOutputStream(int size)
Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.

Method Summary
void close()
Closing a ByteArrayOutputStream has no effect.
void reset()
Resets the count field of this byte array output stream to zero, so that all currently accumulated output in the output stream is discarded.
int size()
Returns the current size of the buffer.
byte[] toByteArray()
Creates a newly allocated byte array.
String toString()
Converts the buffer’s contents into a string decoding bytes using the platform’s default character set.
String toString(int hibyte)
Deprecated.
This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the toString(String enc) method, which takes an encoding-name argument, or the toString() method, which uses the platform’s default character encoding.
String toString(String charsetName)
Converts the buffer’s contents into a string by decoding the bytes using the named charset.
void write(byte[] b, int off, int len)
Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
void write(int b)
Writes the specified byte to this byte array output stream.
void writeTo(OutputStream out)
Writes the complete contents of this byte array output stream to the specified output stream argument, as if by calling the output stream’s write method using out.write(buf, 0, count).

eg
package 字节流;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class dsa {
public static void main(String[] args) throws IOException {
String s = “d的撒大帝带我去额”;
byte[] b = s.getBytes(“utf-8”);
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(b);
for (int i = 0; i < b.length; i++) {
System.out.println(b[i]);
}
System.out.println(out.toString(“utf-8”));
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值