字节流OutputStream

字节流OutputStream
/**
 * 输出流OutputStream
 * @author bpe
 *
 */
public class OutputStreamTest {
     public static void main(String[] args) {
          // file类是磁盘写入类 具体的是在IO基础知识中
          File file = new File("D:\\bpe\\input4.txt");
          try {
              // 创建一个输出流
              OutputStream stream = new FileOutputStream(file);
              // 要写入的数据
              String str = "hello outputStream!";
              byte[] b = str.getBytes();
              // write(int a) 把指定的字节输入到文件中
              // stream.write(str.charAt(0)); //这里把h输入到了文件中
              // 写入文件
              // void write(byte[] b,int off,int len) 将字节数组中off开始,长度为len的字节数据输出到输出流中
              stream.write(b, 0, b.length / 2); // 输入文件的字符为hello out
              // stream.write(b);
              System.out.println("写入文件成功-----------");
              stream.flush();// 强制把任何被缓冲的已写的输出数据输出到输出流中/ 为什么要在close前flush呢? 避免在缓冲区的数据丢失。
              // 关闭流
              stream.close();
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
     }
}