字节流

    8位  --- 1字节             16位--2个字节--1 字符   

字节输入流--->读取文件,读取数据到程序(内存)
字节输出流--->写数据到外部存储设备 

字节流输出流

OutputStream

JAVA高级基础(29)---字节输出流

注:更多方法请查找API

package org.lanqiao.outputstream.demo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class OutputStreamDemo {
	public static void main(String[] args) {
		//创建目标源---文件
		File file = new File("test.txt");
		//创建输出流,并让输出流执行目标文件
		OutputStream os = null;
		try {
			os = new FileOutputStream(file,true);
			//创建写出的内容
			String str = "Hello World";
			//将字符串转换位字节数组
			byte[] b = str.getBytes();
			//调用输出流的写的方法,将转换得到的字节数组全部写出到目标文件
			os.write(b);
			os.write("\r\n".getBytes());
			//写出字节数组的一部分
			os.write(b, 0, 5);
			//写一个字节
			os.write(97);
			//如何实现追加写
			//OutputStream os = new FileOutputStream(file,true);
			//如何实现写换行
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(os != null) {
					os.close();	
				}
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}
}

 

相关文章:

  • 2021-10-14
  • 2021-07-19
  • 2021-11-10
  • 2022-12-23
  • 2021-05-17
  • 2021-11-12
  • 2021-05-25
  • 2022-12-23
猜你喜欢
  • 2021-07-17
  • 2021-12-09
  • 2021-08-29
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
相关资源
相似解决方案