zoukankan      html  css  js  c++  java
  • 字节流---Day30

    IO概述

      当我们在生活中把电脑上的数据拷贝到U盘或者硬盘上时,就是进行数据传输,按照数据的流动方向,我们分为输入(input)和输出(output),即就是所谓IO流

      Java中I/O操作主要是指使用 java.io 包下的内容,进行输入、输出操作。输入也叫做读取数据,输出也叫做作写出数据

    IO的分类

      1.根据数据流向:

        1.输入流:把数据从其他设备上读取到内存中的流。(硬盘--->>内存) 

        2.输出流:把数据从内存中写出到其他设备上的流。(内存--->>硬盘)

      2.根据数据类型:

        1.字节流:以字节为单位,读写数据的流

        2.字符流:以字符为单位,读写数据的流

    字节流

      一切文件皆为字节,当我们在进行文本、图片、视频等传输时,都是以字节来传输的。

    字节输出流【OutputStream】

      java.io.OutputStream 抽象类是表示字节输出流的所有类的超类,将指定的字节信息写出到目的地。它定义了字节输出流的基本共性功能方法。

        1.public void close() :关闭此输出流并释放与此流相关联的任何系统资源。

        2.public void flush() :刷新此输出流并强制任何缓冲的输出字节被写出。

        3.public void write(byte[] b) :将 b.length字节从指定的字节数组写入此输出流。

        4.public void write(byte[] b, int off, int len) :从指定的字节数组写入 len字节,从偏移量off开始输 出到此输出流。

        5.public abstract void write(int b) :将指定的字节输出流

        备注:close方法,当完成流的操作时,必须调用此方法,释放系统资源

    FileOutputStream类

      java.io.FileOutputStream 类是文件输出流,用于将数据写出到文件。 

      构造方法

        1.public FileOutputStream(File file) :创建文件输出流以写入由指定的File对象表示的文件。

        2.public FileOutputStream(String name) : 创建文件输出流以指定的名称写入文件。 

     1 package demosummary.io;
     2 
     3 import java.io.File;
     4 import java.io.FileNotFoundException;
     5 import java.io.FileOutputStream;
     6 
     7 /**
     8  *     1.public FileOutputStream(File file) :创建文件输出流以写入由指定的File对象表示的文件。
     9  *
    10  *     2.public FileOutputStream(String name) : 创建文件输出流以指定的名称写入文件。
    11  */
    12 public class IOFos {
    13     public static void main(String[] args) throws FileNotFoundException {
    14         //1.public FileOutputStream(File file) :创建文件输出流以写入由指定的File对象表示的文件。
    15         File file = new File("./src/demosummary/io/tmp/a.txt");
    16         FileOutputStream fos = new FileOutputStream(file);
    17         System.out.println(fos);
    18         //2.public FileOutputStream(String name) : 创建文件输出流以指定的名称写入文件。
    19         FileOutputStream fos1 = new FileOutputStream("./src/demosummary/io/tmp/b.txt");
    20         System.out.println(fos1);
    21     }
    22 }

    写出字节数据

       1.写出字节: write(int b) 方法,每次可以写出一个字节数据

     1 package demosummary.io;
     2 
     3 import java.io.FileOutputStream;
     4 import java.io.IOException;
     5 
     6 public class FosWrite {
     7     public static void main(String[] args) throws IOException {
     8         //创建FileOutputStream对象
     9         FileOutputStream fos = new FileOutputStream("./src/demosummary/io/tmp/a.txt");
    10         //写出数据到a.txt文件中
    11         fos.write(97);
    12         fos.write(98);
    13         fos.write(99);
    14         //释放资源
    15         fos.close();
    16     }
    17 }

      备注:

        1. 虽然参数为int类型四个字节,但是只会保留一个字节的信息写出。

        2. 流操作完毕后,必须释放系统资源,调用close方法,千万记得。

       2.写出字节数组: write(byte[] b) ,每次可以写出数组中的数据

     1 package demosummary.io;
     2 
     3 import java.io.FileOutputStream;
     4 import java.io.IOException;
     5 
     6 public class FosWrite2 {
     7     public static void main(String[] args) throws IOException {
     8         //创建FileOutputStream对象
     9         FileOutputStream fos = new FileOutputStream("./src/demosummary/io/tmp/c.txt");
    10         //将字符串转换成字节数组
    11         byte[] bytes = "java程序员".getBytes();
    12         //写出字节数组到c.txt文件中
    13         fos.write(bytes);
    14         //释放资源
    15         fos.close();
    16     }
    17 }

      3. 写出指定长度字节数组: write(byte[] b, int off, int len) ,每次写出从off索引开始,len个字节

     1 package demosummary.io;
     2 
     3 import java.io.FileOutputStream;
     4 import java.io.IOException;
     5 
     6 /**
     7  *   3. 写出指定长度字节数组: write(byte[] b, int off, int len) ,每次写出从off索引开始,len个字节
     8  */
     9 public class FosWrite3 {
    10     public static void main(String[] args) throws IOException {
    11         //创建FileOutputStream对象
    12         FileOutputStream fos = new FileOutputStream("./src/demosummary/io/tmp/a.txt");
    13         //将字符串转换成字节数组
    14         byte[] bytes = "abcjava程序员".getBytes();
    15         //写出相应的字符串到a.txt文件中
    16         fos.write(bytes,3,4);
    17         //释放资源
    18         fos.close();
    19     }
    20 }

    数据的追加续写

      1.public FileOutputStream(File file, boolean append) : 创建文件输出流以写入由指定的 File对象表示的 文件。

      2.public FileOutputStream(String name, boolean append) : 创建文件输出流以指定的名称写入文件

     1 package demosummary.io;
     2 
     3 import java.io.File;
     4 import java.io.FileOutputStream;
     5 import java.io.IOException;
     6 
     7 /**
     8  *   1.public FileOutputStream(File file, boolean append) 创建文件输出流以写入由指定的File对象表示的文件。
     9  *
    10  *   2.public FileOutputStream(String name, boolean append) 创建文件输出流以指定的名称写入文件
    11  */
    12 public class FosWrite4 {
    13     public static void main(String[] args) throws IOException {
    14         //1.public FileOutputStream(File file, boolean append) 创建文件输出流以写入由指定的File对象表示的文件
    15         // 创建File对象
    16         File file = new File("./src/demosummary/io/tmp/a.txt");
    17         //创建FileOutputStream对象
    18         FileOutputStream fos = new FileOutputStream(file,true);//true为追加,false为清空
    19         //将字符串转换成数据数组
    20         byte[] bytes = "java程序员".getBytes();
    21         //把"程序员"三个字符串写出到a.txt文件中
    22         fos.write(bytes,4,9);//一个中文三个字节
    23         //释放资源
    24         fos.close();
    25         /**
    26          * 执行前:java
    27          * 执行后:java程序员
    28          */
    29     }
    30 }

      备注:以上两个构造方法,参数中都需要传入一个boolean类型的值, true 表示追加数据, false 表示清空原有数据。 这样创建的输出流对象,就可以指定是否追加续写了

    写出换行

      Windows系统里,换行符号是  

     1 package demosummary.io;
     2 
     3 import java.io.File;
     4 import java.io.FileOutputStream;
     5 import java.io.IOException;
     6 
     7 public class FosWrite5 {
     8     public static void main(String[] args) throws IOException {
     9         //1.public FileOutputStream(File file, boolean append) 创建文件输出流以写入由指定的File对象表示的文件
    10         // 创建File对象
    11         File file = new File("./src/demosummary/io/tmp/b.txt");
    12         //创建FileOutputStream对象
    13         FileOutputStream fos = new FileOutputStream(file,true);//true为追加,false为清空
    14         //创建数组
    15         byte[] bytes = {97, 98, 99, 100, 101};
    16         //遍历数组
    17         for (byte b : bytes) {
    18             //写出数据到b.txt文件中
    19             fos.write(b);
    20             //写一个换一行
    21             fos.write("
    ".getBytes());
    22         }
    23         //释放资源
    24         fos.close();
    25         /**
    26          * 执行结果
    27          * a
    28          * b
    29          * c
    30          * d
    31          * e
    32          */
    33     }
    34 }

    字节输入流【InputStream】

      java.io.InputStream 抽象类是表示字节输入流的所有类的超类,可以读取字节信息到内存中。它定义了字节输入 流的基本共性功能方法。

        1.public void close() :关闭此输入流并释放与此流相关联的任何系统资源。

        2.public abstract int read() : 从输入流读取数据的下一个字节。

        3.public int read(byte[] b) : 从输入流中读取一些字节数,并将它们存储到字节数组 b中 。

    FileInputStream类

      java.io.FileInputStream 类是文件输入流,从文件中读取字节

      构造方法

        1.FileInputStream(File file) : 通过打开与实际文件的连接来创建一个FileInputStream ,该文件由文件系 统中的 File对象 file命名。

        2.FileInputStream(String name) : 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件 系统中的路径名 name命名。 

    读取字节数据

      1. 读取字节: read 方法,每次可以读取一个字节的数据,提升为int类型,读取到文件末尾,返回 -1 

     1 package demosummary.io;
     2 
     3 import java.io.FileInputStream;
     4 import java.io.IOException;
     5 
     6 public class FisRead1 {
     7     public static void main(String[] args) throws IOException {
     8         //创建FileInputStream对象
     9         FileInputStream fis = new FileInputStream("./src/demosummary/io/tmp/a.txt");
    10         //读取a.txt文件内容:abcd
    11         int read = fis.read();
    12         System.out.println((char)read);
    13         read = fis.read();
    14         System.out.println((char)read);
    15         read = fis.read();
    16         System.out.println((char)read);
    17         read = fis.read();
    18         System.out.println((char)read);
    19         read = fis.read();
    20         System.out.println(read);
    21         //释放资源
    22         fis.close();
    23         /**
    24          * a
    25          * b
    26          * c
    27          * d
    28          * -1
    29          */
    30     }
    31 }
  • 相关阅读:
    easyui源码翻译1.32--Droppable(放置)
    easyui源码翻译1.32--Draggable(拖动)
    easyui源码翻译1.32--Dialog(对话框窗口)
    easyui源码翻译1.32--DateTimeBox(日期时间输入框)
    easyui源码翻译1.32--DateBox(日期输入框)
    easyui源码翻译1.32--ComboTree(树形下拉框)
    easyui源码翻译1.32--ComboGrid(数据表格下拉框)
    我不曾忘记的初心-大厂小厂
    我不曾忘记的初心-屌丝逆袭
    我不曾忘记的初心-愿天堂没有代码
  • 原文地址:https://www.cnblogs.com/hpcz190911/p/11992962.html
Copyright © 2011-2022 走看看