5.字节流
5.1 利用 文件字节输出流 向文件写入数据FileOutputStream
public class Test01 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("io.txt");
fos.write("123abc".getBytes(), 2, 3);
fos.close();
}
}
public class Test02 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("io.txt",true);
fos.write("123abc".getBytes());
fos.close();
}
}
public class Test03 {
public static void main(String[] args) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream("io.txt",true);
fos.write("123abc".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
5.2 利用 文件字节输入流 读取文件里的数据FileInputStream
public class Test04 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("io.txt");
int read;
while((read = fis.read()) != -1){
System.out.println((char)read);
}
fis.close();
}
}
public class Test06 {
public static void main(String[] args) throws IOException{
FileInputStream fis = new FileInputStream("io.txt");
byte[] bs = new byte[1024];
int len;
while((len = fis.read(bs)) != -1){
System.out.println(new String(bs, 0, len));
}
fis.close();
}
}
public class Test07 {
public static void main(String[] args){
FileInputStream fis = null;
try {
fis = new FileInputStream("io.txt");
byte[] bs = new byte[1024];
int len;
while((len = fis.read(bs)) != -1){
System.out.println(new String(bs, 0, len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
5.3 深入 带缓冲区的字节输出流 FileOutputStream
public class Test04 {
public static void main(String[] args) throws IOException{
FileOutputStream fos = new FileOutputStream("io.txt"); 基础流
fos.write("1".getBytes());
fos.write("2".getBytes());
fos.write("3".getBytes());
fos.write("a".getBytes());
fos.write("b".getBytes());
fos.write("c".getBytes());
fos.close();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("io.txt"));
bos.write("1".getBytes());
bos.write("2".getBytes());
bos.write("3".getBytes());
bos.write("a".getBytes());
bos.write("b".getBytes());
bos.write("c".getBytes());
bos.close();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("io.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("io.txt"), 2048);
}
}
5.4 利用 带缓冲区的字节输出流 向文件写入数据BufferedOutputStream
public class Test01 {
public static void main(String[] args) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("io.txt"));
bos.write("123abc".getBytes());
bos.close();
}
}
public class Test02 {
public static void main(String[] args) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("io.txt",true));
bos.write("123abc".getBytes());
bos.close();
}
}
public class Test03 {
public static void main(String[] args){
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream("io.txt",true));
bos.write("123abc".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
5.5 手撕BufferedOutputStream底层源码
public class FilterOutputStream extends OutputStream {
protected OutputStream out;
public FilterOutputStream(OutputStream out) {
this.out = out;
}
public void write(byte[] b) throws IOException {
this.write(b, 0, b.length);
}
@SuppressWarnings("try")
public void close() throws IOException {
try (OutputStream ostream = out) {
flush();
}
}
}
public class BufferedOutputStream extends FilterOutputStream {
protected byte[] buf;
protected int count;
public BufferedOutputStream(OutputStream out) {
this(out, 8192);
}
public BufferedOutputStream(OutputStream out, int size) {
super(out);
if (size <= 0) {
throw new IllegalArgumentException("Buffer size <= 0");
}
buf = new byte[size];
}
public synchronized void write(byte b[], int off, int len) throws IOException {
if (len >= buf.length) {
flushBuffer();
out.write(b, off, len);
return;
}
if (len > buf.length - count) {
flushBuffer();
}
System.arraycopy(b, off, buf, count, len);
count += len;
}
private void flushBuffer() throws IOException {
if (count > 0) {
super.out.write(buf, 0, count);
count = 0;
}
}
public synchronized void flush() throws IOException {
flushBuffer();
super.out.flush();
}
}
FileOutputStream fos = new FileOutputStream("io.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write("1".getBytes());
bos.write("2".getBytes());
bos.write("3".getBytes());
bos.write("a".getBytes());
bos.write("b".getBytes());
bos.write("c".getBytes());
bos.close();
5.6 利用 带有缓冲区的字节输入流 读取文件里的数据 BufferedInputStream
public class Test05 {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("io.txt"));
byte[] bs = new byte[1024];
int len;
while((len = bis.read(bs)) != -1){
System.out.println(new String(bs, 0, len));
}
bis.close();
}
}
5.7 拷贝文件
public class Copy {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("麻生希.mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.mp4"));
byte[] bs = new byte[1024];
int len;
while((len = bis.read(bs)) != -1){
bos.write(bs, 0, len);
}
bis.close();
bos.close();
}
}
总结