目录
I表示Input,数据从硬盘进内存的过程,称之为读
O表示Output,数据从内存到硬盘的过程,称之为写
(按照流的方向,是以内存为参照物再进行读写的)

(纯文本文件:用记事本打开能读得懂的,就是纯文本文件)
步骤:1.创建字节输出流的对象 2.写数据 3.释放资源
代码演示:
- public class byteStreamDemo_01 {
- public static void main(String[] args) throws IOException {
- //1.创建字节输出流对象
- FileOutputStream fos = new FileOutputStream("D:\\a.txt");
- //2.写数据
- fos.write(97);
- //3.释放资源
- fos.close();
- }

代码示例:
- public class byteStreamDemo_03 {
- public static void main(String[] args) throws IOException {
- //1.创建字节输出流对象
- FileOutputStream fos = new FileOutputStream("myByteStream\\a.txt");
- //2.写数据
- byte[] bys = {97, 98, 99, 100, 101, 102, 103};
- //第一个参数是数组名,第二个参数是从哪个索引开始写,第三个参数代表写几个
- fos.write(bys, 1, 2);
-
- //3.释放资源
- fos.close();
- }
写完数据后加换行符:
windows:\r\n
Linux:\n
mac:\r
代码示例:
- public class byteStreamDemo_04 {
- public static void main(String[] args) throws IOException {
- FileOutputStream fos = new FileOutputStream("myByteStream\\a.txt");
- fos.write(97);
- fos.write("\r\n".getBytes());
- fos.write(98);
- fos.write("\r\n".getBytes());
- fos.write(99);
- fos.write("\r\n".getBytes());
- fos.write(100);
- fos.write("\r\n".getBytes());
- fos.write(101);
- fos.close();
- }
在创建文件输出流已指定的名称写入文件,第二个参数为续写开关,若写true,则打开续写开关,不会清空文件里面的内容,默认为false关闭
代码演示:
- public class byteStreamDemo_05 {
- public static void main(String[] args) throws IOException {
- //在第二个参数是续写开关,写入true表示打开续写,默认是false关闭
- FileOutputStream fos = new FileOutputStream("myByteStream\\a.txt", true);
- fos.write(97);
-
- fos.write(98);
-
- fos.write(99);
-
- fos.write(100);
-
- fos.write(101);
- fos.close();
- }
- public class byteStreamDemo_06 {
- public static void main(String[] args) throws IOException {
- FileOutputStream fos = null;
- try {
- fos = new FileOutputStream("D:\\a.txt");
- fos.write(97);
-
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- //finally里面的代码一定会被执行
- if (fos != null) {
- try {
- fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- }
- }
- }