ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java IO流

2022-07-27 22:00:42  阅读:116  来源: 互联网

标签:文件 Java 字节 int printStackTrace IO catch String


Java IO流

什么是IO?

就是Input和Output,通过IO可以完成硬盘文件的读和写。

输入(Input)、读(Read)、输入流(InputStream):从硬盘到内存中。

输出(Output)、写(Write)、输出流(OutputStream):从内存到硬盘中。

IO流的分类

以内存作为参照物,按照流的方向分类:

  • 往内存中去,叫做输入
  • 从内存中出来,叫做输出。

按照读取数据的不同进行分类:

  • 字节流:按照字节的方式读取数据,一次读取1个字节Byte,即一次读取8个二进制。这种流什么类型的文件都可以读取:文本文件、图片、声音文件、视频文件。
  • 字符流:按照字符的方式读取数据,一次读取1个字符char。这种流只能读取纯文本文件,比如txt文件,不能读取word。比如中文字符就是占2个字节,使用字节流就只能读取一半。

IO流的类型

四个分支:

java.io.InputStream 字节输入流

java.io.OutputStream 字节输出流

java.io.Reader 字符输入流

java.io.Writer 字符输出流

以上都是抽象类,都实现了Closeable接口。

结论:以”Stream“结尾的都是字节流,以”Reader“和”Writer“结尾的都是字符流。

用完了流:一定要用close关闭流。

输出流都实现了Flushable接口,都是可刷新的。所以在输出流最终输出之后,一定要使用flush()对管道进行清空管道。

流的具体类型

文件专属

FileInputStream

FileOutputStream

FileReader

FileWriter

转换流(将字节流转换成字符流):

InputStreamReader

OutputStreamWriter

缓冲流

BufferedReader

BufferedWriter

BufferedInputStream

BufferedOutputStream

数据流专属

DataInputStream

DataOutputStream

标准输出流

PrintWriter

PrintStream

对象专属流

ObjectInputStream

ObjectOutputStream

字节输入流FileInputStream

将磁盘文件以字节方式读入内存(即程序中使用)中。

构造方法:

  • FileInputStream(File file):通过打开与实际文件的连接创建一个 输入流。
  • FileInputStream(FileDescriptor fdobj):通过文件描述符,创建输入流。
  • FileInputStream(String name):通过路径name,创建输入流。【常用】

常用方法:

  • int read():读取一个字节,返回字节码。
  • int read(byte[] b):从一个输入流中读取一定数量的字节,并将这些字节存储到其缓冲作用的数组b中,返回的是读取到的字节数。减少硬盘和内存的交互,提高程序的执行效率。
  • int available():获得剩余可读取的字节数量。
  • long skip(long n):跳过n个字节读取数据。

代码如下:

public static void main(String[] args) {

    FileInputStream in = null;
    try {
        in = new FileInputStream("C:\\Users\\Administrator\\Desktop\\实验.txt");
        int data = 0;
        while ((data=in.read()) != -1){
            //in.read()方法返回的是int类型,返回的为字节的ascii码,如果没有字节则返回-1
            System.out.println(data);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        //防止输入流为空。只有不为空时才可以关闭
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

注意:IDEA默认路径是工程的根路径

使用byte数组读取

public class IOTest02 {
    public static void main(String[] args) {

        FileInputStream in = null;
        try {
            in = new FileInputStream("C:\\Users\\Administrator\\Desktop\\实验.txt");
            byte[] bytes = new byte[4];
            int dataCount = 0;
            while ((dataCount=in.read(bytes)) != -1){
                //dataCpunt为byte数组读取到的字节数,有多少个输出多少个
                System.out.print(new String(bytes,0,dataCount));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //防止输入流为空。只有不为空时才可以关闭
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

字节输出流FileOutputStream

构造方法:

  • FileOutputStream(File file):以指定的File文件创建文件输出流
  • FileOutputStream(File file,boolean append):以指定的File文件创建文件输出流,append为true,那么字节被写入文件的末尾。
  • FileOutputStream(FileDescriptor fdobj):以指定的文件描述符文件创建文件输出流
  • FileOutputStream(String name):以指定的文件路径名创建文件输出流
  • FileOutputStream(String name,boolean append):以指定的文件路径名创建文件输出流,append为true,那么字节被写入文件的末尾。

常用方法:

  • void write(byte[] b):将 b.length个字节从指定的字节数组写入此文件输出流。
  • void write(byte[] b,int off,int len):将 len字节从位于偏移量 off的指定字节数组写入此文件输出流。
  • void write(int b):将指定的字节写入此文件输出流。

代码如下:

public static void main(String[] args) {
        FileOutputStream out = null;
        try {
            out = new FileOutputStream("myfile",true);
//            byte[] bytes = {97,98,99};
            String str  = "噼里啪啦";
            byte[] bytes = str.getBytes();
            out.write(bytes);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

复制

利用输入输出流进行复制

代码如下:

public static void main(String[] args) {
    FileInputStream in = null;
    FileOutputStream out = null;
    byte[] bytes = new byte[1024*1024];
    try {
        in = new FileInputStream("E:\\team work\\主方程建模\\主方程运算.pdf");
        out = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\主方程运算.pdf");
        int dataCount = 0;
        while((dataCount = in.read(bytes))!=-1){
            out.write(bytes,0,dataCount);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(out !=null){
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

字符输入流FileReader

将磁盘文件以字符方式读入内存(即程序中使用)中。只能读取文本文件。

构造方法:

  • FileReader(File file):通过打开与实际文件的连接创建一个输入流。
  • FileReader(FileDescriptor fdobj):通过文件描述符,创建输入流。
  • FileReader(String filename):通过路径name,创建输入流。【常用】

常用方法:

  • int read():读取一个字符,返回字节码。
  • int read(char[] c):从一个输入流中读取一定数量的字符,并将这些字节存储到其缓冲作用的数组c中,返回的是读取到的字符数。减少硬盘和内存的交互,提高程序的执行效率。
  • long skip(long n):跳过n个字符读取数据。

``

public static void main(String[] args) {
    FileReader reader = null;
            try {
        reader = new FileReader("C:\\Users\\Administrator\\Desktop\\实验.txt");
        char[] c = new char[1024*512];//1MB
        int dataCount = 0;
        while((dataCount = reader.read(c))!=-1){
            System.out.print(new String(c,0,dataCount));
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
}

字符输入流FileWriter

构造方法:

  • FileWriter(File file):以指定的File文件创建文件输出流
  • FileWriter(File file,boolean append):以指定的File文件创建文件输出流,append为true,那么字节被写入文件的末尾。
  • FileWriter(FileDescriptor fdobj):以指定的文件描述符文件创建文件输出流
  • FileWriter(String name):以指定的文件路径名创建文件输出流
  • FileWriter(String name,boolean append):以指定的文件路径名创建文件输出流,append为true,那么字节被写入文件的末尾。

常用方法:

  • void write(int c):用数字输入
  • void write(char[] c):用字符数组输入
  • void write(char[] c,off,len):用字符数组输入,能够选取
  • void write(String s):用字符串输入
  • void write(String s,int off,int len):用字符串输入,能够选取
  • void append():在文件原内容后边添加。

复制:通过字符流复制,只能对文本文件进行复制。

标签:文件,Java,字节,int,printStackTrace,IO,catch,String
来源: https://www.cnblogs.com/fkddb/p/16526674.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有