IO流的分类

  流向:

    输入流  读出数据

    输出流 写出数据

  数据类型

    字节流

      字节输入流 读取数据 inputstream

      字节输出流 写出数据 outputstream

    字符流

      字符输入流 读取数据 Reader

      字符输出流 写出数据 Write

注意:通常情况按照数据类型来区分

*****需求:向文本文件输出一句话hello world

  分析:这个时候最好是采用字符流 但是字符流是在字节流之后  故采用的是字节输出流(写一句话)‘

  outputsteam是一个抽象类 所以不能实例化,这样就去寻找子类,因为是文件,然后确定为fileoutputstream(每种基类的子类都是以父类名作为后缀名)

FileOutputStream的构造方法:
 *   FileOutputStream(File file)
 *  FileOutputStream(String name)

字节输出流操作步骤:
 *   A:创建字节输出流对象
 *   B:写数据
 *   C:释放资源

 public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
// 创建字节输出流对象
// FileOutputStream(File file)
// File file = new File("fos.txt");
// FileOutputStream fos = new FileOutputStream(file);
// FileOutputStream(String name)
FileOutputStream fos = new FileOutputStream("fos.txt");
/*
* 创建字节输出流对象了做了几件事情:
* A:调用系统功能去创建文件
* B:创建fos对象
* C:把fos对象指向这个文件
*/ //写数据
fos.write("hello,IO".getBytes());
fos.write("java".getBytes()); //释放资源
//关闭此文件输出流并释放与此流有关的所有系统资源。
fos.close();
/*
* 为什么一定要close()呢?
* A:让流对象变成垃圾,这样就可以被垃圾回收器回收了
* B:通知系统去释放跟该文件相关的资源
*/
//java.io.IOException: Stream Closed
//fos.write("java".getBytes());
}
}

字节输出流操作步骤:
 * A:创建字节输出流对象
 * B:调用write()方法
 * C:释放资源
 *
 * public void write(int b):写一个字节
 * public void write(byte[] b):写一个字节数组
 * public void write(byte[] b,int off,int len):写一个字节数组的一部分

 public class FileOutputStreamDemo2 {
public static void main(String[] args) throws IOException {
// 创建字节输出流对象
// OutputStream os = new FileOutputStream("fos2.txt"); // 多态
FileOutputStream fos = new FileOutputStream("fos2.txt"); // 调用write()方法
//fos.write(97); //97 -- 底层二进制数据 -- 通过记事本打开 -- 找97对应的字符值 -- a
// fos.write(57);
// fos.write(55); //public void write(byte[] b):写一个字节数组
byte[] bys={,,,,};
fos.write(bys); //public void write(byte[] b,int off,int len):写一个字节数组的一部分
fos.write(bys,,); //释放资源
fos.close();
}
}

* 如何实现数据的换行?
 *   为什么现在没有换行呢?因为你值写了字节数据,并没有写入换行符号。
 *   如何实现呢?写入换行符号即可呗。
 *   刚才我们看到了有写文本文件打开是可以的,通过windows自带的那个不行,为什么呢?
 *   因为不同的系统针对不同的换行符号识别是不一样的?
 *   windows:\r\n
 *   linux:\n
 *   Mac:\r
 *   而一些常见的个高级记事本,是可以识别任意换行符号的。
 *
 * 如何实现数据的追加写入?
 *   用构造方法带第二个参数是true的情况即可

 public class FileOutputStreamDemo3 {
public static void main(String[] args) throws IOException {
// 创建字节输出流对象
// FileOutputStream fos = new FileOutputStream("fos3.txt");
// 创建一个向具有指定 name 的文件中写入数据的输出文件流。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。
FileOutputStream fos = new FileOutputStream("fos3.txt", true); // 写数据
for (int x = ; x < ; x++) {
fos.write(("hello" + x).getBytes());
fos.write("\r\n".getBytes());
} // 释放资源
fos.close();
}
}

最新文章

  1. JavaScript学习笔记(四)——jQuery插件开发与发布
  2. 百度Site App的uaredirect.js实现手机访问,自动跳转网站手机版
  3. NVelocity 表格行奇偶样式变换
  4. $.ajax()方法参数详解
  5. [Emacs] 常用快捷键-- 生存指南
  6. unity3d WorldComposer1 卫星地图生成地形
  7. 编译Apache Hadoop2.2.0源代码
  8. codeforces #310 div1 C
  9. YII设置用户访问过滤
  10. ucos内存管理原理详解
  11. hiredis的各种windows版本
  12. 《C语言点滴》书评
  13. C++回顾day02---&lt;继承相关问题&gt;
  14. Python - 浅谈Python的编译与反编译
  15. orleans发送广播消息
  16. mongoose+koa2 按照_id更新多条数据,删除数组中的字段,然后添加新的字段,$pull和$or结合使用
  17. js中的运算符优先级
  18. win10 + VS2010 + OpenCV2.4.10重编译OpenCV开发环境搭建
  19. mtk 无线配置文件生效过程
  20. php后台管理员权限相关表结构

热门文章

  1. 外星联络(bzoj 2251)
  2. poj2723 2sat判断解+二分
  3. $.post()用法例子
  4. Codeforces Beta Round #57 (Div. 2) E. Enemy is weak
  5. Palindrome--poj 1159(最长公共子字符串+滚动数字)
  6. Windows如何在cmd命令行中查看、修改、删除与添加、设置环境变量
  7. SharePoint中取得ACL和组中用户数量
  8. iOS中3种正则表达式的使用
  9. How to Use SFTP ?
  10. Buildroot构建指南——根文件系统(Rootfs)【转】