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

    一 字节输出流OutputStream

      OutputStream此抽象类是表示输出字节流的所有类的超类。操作的数据都是字节,定义了输

    出字节流的基本共性功能方法。

    输出流中定义都是写write方法,如下图:

     1.FileOutputStream类

      OutputStream有很多子类,其中子类FileOutputStream可用来写入数据到文件。

      FileOutputStream,即文件输出流是用于将数据写入 File的输出流。

     

     2.FileOutputStream类写入数据到文件中

      将数据写到文件中,代码演示:

    public class FileOutputStreamDemo {
        public static void main(String[] args) throws IOException {
            //需求:将数据写入到文件中。
            //创建存储数据的文件。
            File file = new File("c:\file.txt");
            //创建一个用于操作文件的字节输出流对象。一创建就必须明确数据存储目的地。
            //输出流目的是文件,会自动创建。如果文件存在,则覆盖。
            FileOutputStream fos = new FileOutputStream(file);
            //调用父类中的write方法。
            byte[] data = "abcde".getBytes();
            fos.write(data);
            //关闭流资源。
            fos.close();
        }
    }

    3.给文件中续写和换行

      继续查阅FileOutputStreamAPI。发现在FileOutputStream的构造函数中,可以接受一个boolean类型的值,

    如果值true,就会在文件末位继续添加。

       给文件中续写数据和换行,代码演示:

    public class FileOutputStreamDemo2 {
        public static void main(String[] args) throws Exception {
            File file = new File("c:\file.txt");
            FileOutputStream fos = new FileOutputStream(file, true);
            String str = "
    "+"aaa";
            fos.write(str.getBytes());
            fos.close();
        }
    }

    二 字节输入流InputStream

      通过前面的学习,我们可以把内存中的数据写出到文件中,那如何想把内存中的数据读到内存中,我们通过

    InputStream可以实现。InputStream此抽象类是表示字节输入流的所有类的超类。,定义了字节输入流的基本

    共性功能方法。

       int read():读取一个字节并返回,没有字节返回-1.

      int read(byte[]): 读取一定量的字节数,并存储到字节数组中,返回读取到的字节数。

    1.FileInputStream类

      InputStream有很多子类,其中子类FileInputStream可用来读取文件内容。

      FileInputStream 从文件系统中的某个文件中获得输入字节。

     

     2.FileInputStream类读取数据read方法

      在读取文件中的数据时,调用read方法,实现从文件中读取数据

       从文件中读取数据,代码演示:

    public class FileInputStreamDemo {
        public static void main(String[] args) throws IOException {
            File file = new File("c:\file.txt");
            //创建一个字节输入流对象,必须明确数据源,其实就是创建字节读取流和数据源相关联。
            FileInputStream fis = new FileInputStream(file);
            //读取数据。使用 read();一次读一个字节。
            int ch = 0;
            while((ch=fis.read())!=-1){
                System.out.println("ch="+(char)ch);
            }
            // 关闭资源。
            fis.close();
        }
    }

    3.读取数据read(byte[])方法

      在读取文件中的数据时,调用read方法,每次只能读取一个,太麻烦了,于是我们可以定义数组作为临时的存

    储容器,这时可以调用重载的read方法,一次可以读取多个字符。

    public class FileInputStreamDemo2 {
        public static void main(String[] args) throws IOException {
            /*
             * 演示第二个读取方法, read(byte[]);
             */
            File file = new File("c:\file.txt");
            // 创建一个字节输入流对象,必须明确数据源,其实就是创建字节读取流和数据源相关联。
            FileInputStream fis = new FileInputStream(file);        
            //创建一个字节数组。
            byte[] buf = new byte[1024];//长度可以定义成1024的整数倍。        
            int len = 0;
            while((len=fis.read(buf))!=-1){
                System.out.println(new String(buf,0,len));
            }
            fis.close();
        }
    }
  • 相关阅读:
    在ElementUI的 MessageBox 弹框 进行api接口请求
    Vue 报错 (Emitted value instead of an instance of Error) the "scope" attribute for scoped slots have been deprecated and replaced by "slot-scope" since 2.5. The new "slot-scope" attribute can also be u
    子组件 修改父组件的属性值
    1、Flutter---配置
    Vant轮播预览图片
    vue 中 v-for 和 :key 配套使用
    js--两数之和
    blazor 中没有 blazor WebAssembly App 模板
    Deepin 安装vue-cli
    C# Linq Join & Lambda Join
  • 原文地址:https://www.cnblogs.com/jiejava/p/13415191.html
Copyright © 2011-2022 走看看