【JavaSE 知识框架】11 IO流


 第11章 IO流

创作日期:2021-11-13


1.File类的使用

1.1 File类的概述

  • java.io.File类:文件或文件目录路径的抽象表示形式,与平台无关。
  • File能新建,删除,重命名文件和目录,但File不能访问文件内容本身,如果需要访问文件内容本身,则需要使用输入输出流。
  • 想要在Java程序中表示一个真实存在的文件或目录,那么必须有一个File对象,但是Java程序中的一个File对象,可能没有一个真实存在的文件或目录。
  • File对象可以作为参数传递给流的构造器

1.2 File类的常用构造器

  • public File(String pathname):以pathname为路径创建File对象,可以是绝对路径或者相对路径,如果pathname是相对路径,则默认的当前路径在系统属性user.dir中存储。
    • 绝对路径:是一个固定的路径,从盘符开始
    • 相对路径:是相对于某个位置开始
  • public File(String parent,String child):以parent为父路径,child为子路径创建File对象。
  • public File(File parent,String child):根据一个父File对象和子文件路径创建File对象
import java.io.File;

public class fileTest {
    public static void main(String[] args) {
        //构造器1
        File file1 = new File("hello.txt");
        File file2 = new File("G:\\新建文件夹\\txt\\fileTest\\hello.txt");
        System.out.println(file1);//hello.txt
        System.out.println(file2);//G:\新建文件夹\txt\fileTest\hello.txt

        //构造器2
        File file3 = new File("G:\\新建文件夹\\txt\\fileTest","hello.txt");
        System.out.println(file3);//G:\新建文件夹\txt\fileTest\hello.txt

        //构造器3
        File file4 = new File(file2,"he.txt");
        System.out.println(file4);//G:\新建文件夹\txt\fileTest\hello.txt\he.txt
    }
}

1.3 File类的路径分隔符

  • 路径中的每级目录之间用一个路径分隔符隔开。
  • 路径分隔符和系统相关:
    • windows和DOS系统默认使用“\”来表示
    • UNIX和URL使用“/”来表示
  • Java程序支持跨平台运行,因此路径分割符要慎用。为了解决这个隐患,File类提供了一个常量:public static final String separator。根据操作系统,动态的提供分隔符。

1.4 File类的常用方法

  • File类的获取功能
    • public String getAbsolutePath():获取绝对路径
    • public String getPath():获取路径
    • public String getParent():获取上层文件目录路径。若无,返回null
    • public long length() :获取文件长度(即:字数)。不能获取目录的长度
    • public long lastModified():获取最后一次的修改时间,毫秒值
    • public String[] list():获取指定目录下的所有文件或者文件目录的名称数组
    • public File[] listFiles():获取指定目录下所有文件或者文件目录的File数组
import java.io.File;
import java.util.Arrays;

public class fileTest {
    public static void main(String[] args) {
        File file2 = new File("G:\\新建文件夹\\txt");

        //public String getAbsolutePath():获取绝对路径
        System.out.println(file2.getAbsoluteFile()); //G:\新建文件夹\txt

        //public String getPath():获取路径
        System.out.println(file2.getPath());//G:\新建文件夹\txt

        //public String getParent():获取上层文件目录路径。若无,返回null
        System.out.println(file2.getParent());//G:\新建文件夹

        //public long length() :获取文件长度(即:字数)。不能获取目录的长度
        File file3 = new File("G:\\新建文件夹\\txt\\新建文本文档 (2).txt");
        System.out.println(file3.length());//78

        //public long lastModified():获取最后一次的修改时间,毫秒值
        System.out.println(file2.lastModified());//1638165098512

        //public String[] list():获取指定目录下的所有文件或者文件目录的名称数组
        System.out.println(Arrays.toString(file2.list()));//[a.txt, fileTest, b.txt]

        //public File[] listFiles():获取指定目录下所有文件或者文件目录的File数组
        System.out.println(Arrays.toString(file2.listFiles()));//[G:\txt\a.txt, G:\txt\fileTest, G:\txt\b.txt]
    }
}
  • File类的重命名功能
    • public boolean renameTo(File dest):把文件重命名为指定的文件路径
  • File类的判断功能
    • public boolean isDirectory():判断是否是文件目录
    • public boolean isFile():判断是否是文件
    • public boolean exists():判断是否存在
    • public boolean canRead():判断是否可读
    • public boolean canWrite():判断是否可写
    • public boolean isHidden():判断是否隐藏
import java.io.File;

public class fileTest {
    public static void main(String[] args) {
        //public boolean renameTo(File dest):把文件重命名为指定的文件路径
        File file1 = new File("G:\\File\\fileTest1\\a.txt");
        File file2 = new File("G:\\File\\fileTest2\\b.txt");
        System.out.println(file1.renameTo(file2));//true

        //public boolean isDirectory():判断是否是文件目录
        File file3 = new File("G:\\File\\txt");
        System.out.println(file3.isDirectory());//true

        //public boolean isFile():判断是否是文件
        File file4= new File("G:\\File\\txt\\河南省考.txt");
        System.out.println(file3.isFile());//false
        System.out.println(file4.isFile());//true

        //public boolean exists():判断是否存在
        System.out.println(file4.exists());//true

        //public boolean canRead():判断是否可读
        System.out.println(file4.canRead());//true

        //public boolean canWrite():判断是否可写
        System.out.println(file4.canWrite());//true

        //public boolean isHidden():判断是否隐藏
        System.out.println(file4.isHidden());//false
    }
}
  • File类的创建功能
    • public boolean createNewFile():创建文件,若文件存在,则不创建,返回false。
    • public boolean mkdir():创建文件目录。如果此文件目录存在,就不创建了。如果此文件目录的上层目录不存在,也不创建。
    • public boolean mkdirs():创建文件目录。如果上层文件目录不存在,一并创建。
    • 注意事项:如果你创建文件或者文件目录没有写盘符,那么,默认在项目路径下。
  • File类的删除功能
    • public boolean delete():删除文件或者文件夹
    • 删除注意事项:Java中的删除不走回收站,要删除一个文件目录,请注意该文件目录内不能包含文件或文件目录。
import java.io.File;

public class fileTest {
    public static void main(String[] args) throws Exception{

        //public boolean createNewFile():创建文件,若文件存在,则不创建,返回false。
        File file1 = new File("G:\\File\\txt\\fileTest\\a.txt");
        System.out.println(file1.createNewFile());//true

        //public boolean mkdir():创建文件目录。如果此文件目录存在,就不创建了。如果此文件目录的上层目录不存在,也不创建。
        File file2 = new File("G:\\File\\txt\\fileTest");
        System.out.println(file2.mkdir());//false

        //public boolean mkdirs():创建文件目录。如果上层文件目录不存在,一并创建。
        File file3 = new File("G:\\File\\txt1\\fileTest1");
        System.out.println(file3.mkdirs());//true

        //public boolean delete():删除文件或者文件夹
        File file4 = new File("G:\\File\\txt1\\fileTest1");
        System.out.println(file4.delete());//true
    }
}

1.5 File类的使用实例练习


2.IO流的原理及流的分类

2.1 Java IO原理

  • I/O是Input/Output的缩写,I/O技术是非常实用的技术,用于处理设备之间的数据传输。如读/写文件,网络通信等。
  • Java程序中,对于数据的输入/输出操作以"流(stream)"的方式进行。
  • java.io包下提供了各种"流"类和接口,用以获取不同种类的数据,并通过标准的方法输入或输出数据。
  • 输入input:读取外部数据(磁盘等存储设备的数据)到程序(内存)中。
  • 输出output:将程序(内存)数据输出到磁盘,光盘,等存储设备中。

2.2 流的分类

  • 按操作数据单位不同分为:字节流(8bit),字符流(16bit)
  • 按数据流的流向不同分为:输入流,输出流
  • 按流的角色不同分为:字节流,处理流
  • Java的IO流共涉及40多个类,实际上非常规则,都是从如下4个抽象基类派生的,这四个基类派生出来额子类名称都是以其父类名作为子类后缀。   

2.3 IO流体系


3.节点流或文件流

3.1 字符流:FileReader读入数据的基本操作

import java.io.File;
        import java.io.FileReader;
        import java.io.IOException;

public class fileTest {
    public static void main(String[] args) {
        FileReader fileReader = null;
        try {
            //1.实例化File类的对象,指明需要操作的文件
            File file = new File("D:\\Java\\JavaFoundationUp\\src\\text\\a\\a.txt");
            //2.提供具体的流
            fileReader = new FileReader(file);
            //3.数据的读入
            //read():返回读入的一个字符,如果达到文件末尾,返回-1

            //方式一
            /*int read = fileReader.read();
            while (read !=-1){
                System.out.print((char)read);
                read =fileReader.read();
            }*/

            //方式二
            int read;
            while ((read = fileReader.read()) != -1) {
                System.out.print((char) read);
            }

            //方式三
            /*char[] chars = new char[5];
            int len;
            while ((len = fileReader.read(chars))!=-1){
                for (int i = 0; i < len; i++) {
                    System.out.print(chars[i]);
                }
            }*/

            /*//方式四
            char[] chars = new char[5];
            int len;
            while ((len = fileReader.read(chars))!=-1){
                String str = new String(chars,0,len);
                System.out.print(str);
            }*/
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            try {
                //4.流的关闭
                if (fileReader != null) fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

3.2 字符流:FileWriter写出数据的基本操作

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class fileTest {
    public static void main(String[] args) throws IOException {

        //1.实例化File类的对象,指明需要操作的文件
        File file = new File("D:\\Java\\JavaFoundationUp\\src\\file\\b.txt");

        //2.提供FileWriter对象,用于数据的写出
        //FileWriter(file, true) 代表在原有文件上追加内容
        //FileWriter(file, false) 代表直接覆盖原有文件内容
        FileWriter fileWriter = new FileWriter(file, true);
        
        //3.写出的操作
        fileWriter.write("\nhello Java!");
        fileWriter.close();
    }
}

3.3 使用FileReader和FileWriter的组合应用

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ioText {
    public static void main(String[] args) {
        FileReader fileReader = null;
        FileWriter fileWriter = null;
        try {
            //1.实例化File类的对象,指明需要操作的文件
            File file1 = new File("D:\\Java\\JavaFoundationUp\\src\\file\\a.txt");
            File file2 = new File("D:\\Java\\JavaFoundationUp\\src\\file\\b.txt");

            //2.提供具体的流
            fileReader = new FileReader(file1);
            fileWriter = new FileWriter(file2, true);

            //3.读入和写出的操作
            int read;
            while ((read = fileReader.read()) != -1) {
                fileWriter.write(read);
            }

        } catch (Exception e) {
            System.out.println(e);
        } finally {
            try {
                //4.流的关闭
                if (fileReader != null && fileWriter != null) {
                    fileReader.close();
                    fileWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

3.4 字节流:FileInputStream和FileOutputStream的使用


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

//字节流对图片实现复制功能
public class ioText {
    public static void main(String[] args) {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            //1.实例化File类的对象,指明需要操作的文件
            File file1 = new File("D:\\Java\\JavaFoundationUp\\src\\file\\a.jpeg");
            File file2 = new File("D:\\Java\\JavaFoundationUp\\src\\file\\b.jpeg");
            //2.提供具体的流
            fileInputStream = new FileInputStream(file1);
            fileOutputStream = new FileOutputStream(file2);
            //3.读入和写出的操作
            byte[] bytes = new byte[1024];
            int len;
            while ((len = fileInputStream.read(bytes)) != -1) {
                    fileOutputStream.write(bytes,0,len);
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            try {
                //4.流的关闭
                if (fileInputStream != null && fileOutputStream != null) {
                    fileInputStream.close();
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

4.缓冲流和转换流

4.1 处理流之一:缓冲流的使用

  • 字节缓冲流        
    • BufferedInputStream(字节输入流)
    • BufferedOutputStream(字节输出流)

字节缓冲流实现对非文本文件的复制操作

import java.io.*;

public class ioText {
    public static void main(String[] args) {
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            //1.实例化File类的对象,指明需要操作的文件
            File file1 = new File("D:\\Java\\JavaFoundationUp\\src\\file\\a.jpeg");
            File file2 = new File("D:\\Java\\JavaFoundationUp\\src\\file\\b.jpeg");

            //2.提供节点流
            FileInputStream fileInputStream = new FileInputStream(file1);
            FileOutputStream fileOutputStream = new FileOutputStream(file2);

            //3.提供缓冲流
            bufferedInputStream = new BufferedInputStream(fileInputStream);
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

            //3.复制的内部:读入和写出的操作
            byte[] bytes = new byte[1024];
            int len;
            while ((len = bufferedInputStream.read(bytes)) != -1) {
                bufferedOutputStream.write(bytes, 0, len);
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            try {
                //4.流的关闭:只需关闭缓冲流即可
                if (bufferedInputStream != null && bufferedOutputStream != null) {
                    bufferedInputStream.close();
                    bufferedOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  • 字符缓冲流
    • BufferedReader(字符输入流)
    • BufferedWriter(字符输出流)

字符缓冲流实现对文本文件的复制操作

import java.io.*;

public class ioText2 {
    public static void main(String[] args) {
        long l1 = System.currentTimeMillis();
        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;
        try {
            //1.提供缓冲流
            bufferedReader = new BufferedReader(new FileReader
                    (new File("D:\\Java\\JavaFoundationUp\\src\\file\\a.txt")));
            bufferedWriter = new BufferedWriter(new FileWriter
                    (new File("D:\\Java\\JavaFoundationUp\\src\\file\\c.txt")));

            //2.复制的内部:读入和写出的操作
            char[] chars = new char[1024];
            int len;
            while ((len = bufferedReader.read(chars)) != -1) {
                bufferedWriter.write(chars, 0, len);
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            try {
                //3.流的关闭:只需关闭缓冲流即可
                if (bufferedReader != null && bufferedWriter != null) {
                    bufferedReader.close();
                    bufferedWriter.close();
                    long l2 = System.currentTimeMillis();
                    System.out.println("共耗时"+(l2-l1)+"毫秒");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

缓冲流的作用:提高流的读取和写入的速度,嵌套在已有的流的基础上

4.2 缓冲流的练习实例

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
//练习3
public class ioText2 {
    public static void main(String[] args) {
        FileReader fileReader = null;
        BufferedWriter bufferedWriter = null;
        try {
            //1.创建Map集合
            Map<Character, Integer> map = new HashMap();

            //2.遍历每个字符,每个字符出现的次数放在map中
            fileReader = new FileReader("D:\\Java\\JavaFoundationUp\\src\\file\\a.txt");
            int c = 0;
            while ((c = fileReader.read()) != -1) {
                char ch = (char) c;
                if (map.get(ch)==null) {
                    map.put(ch, 1);
                } else {
                    map.put(ch, map.get(ch) + 1);
                }
            }

            //3.把map数据存放到count.txt当中
            bufferedWriter = new BufferedWriter(new FileWriter(new File("D:\\Java\\JavaFoundationUp\\src\\file\\b.txt")));
            Set<Map.Entry<Character, Integer>> entries = map.entrySet();
            for (Map.Entry<Character, Integer> entry : entries) {
                switch (entry.getKey()) {
                    case ' ':
                        bufferedWriter.write("空格=" + entry.getValue());
                        break;
                    case '\t':
                        bufferedWriter.write("tab键=" + entry.getValue());
                        break;
                    case '\r':
                        bufferedWriter.write("回车=" + entry.getValue());
                        break;
                    case '\n':
                        bufferedWriter.write("换行=" + entry.getValue());
                        break;
                    default:
                        bufferedWriter.write(entry.getKey() + "=" + entry.getValue());
                        break;
                }
                bufferedWriter.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //4.流的关闭
                if (fileReader != null && bufferedWriter != null) {
                    fileReader.close();
                    bufferedWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

4.3 处理流之二:转换流的使用

        转换流提供了在字节流和字符流之间的转换,字节流中的数据都是字符时,转成字符流操作更高效,很多时候我们使用转换流来处理文件乱码问题,实现编码和解码的功能。       

Java API提供了两个转换流:

  • InptStreamReader:将InputStream转换为Reader(将一个字节的输入流转换为字符的输入流)
  • OutputStreamWriter:将Writer转换为OutputStream(将一个字符的输出流转换为字节的输出流)

转换过程

InputStreamReade:实现字节输入流到字符输入流的转换

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

//InputStreamReader的使用,实现字节的输入流到字符的输入流的转换
public class ioText {
    public static void main(String[] args) {
        InputStreamReader inputStreamReader = null;
        try {
            inputStreamReader = new InputStreamReader(new FileInputStream("D:\\Java\\JavaFoundationUp\\src\\file\\a.txt"),StandardCharsets.UTF_8);

            char[] chars = new char[1024];
            int len;
            while ((len = inputStreamReader.read(chars)) != -1) {
                String str = new String(chars, 0, len);
                System.out.println(str);
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            try {
                if (inputStreamReader != null) inputStreamReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

综合使用:InptStreamReader和OutputStreamWriter

import java.io.*;
import java.nio.charset.StandardCharsets;

//InputStreamReader和OutputStreamWriter的综合使用
//以UTF_8格式读入(解码),以GBK格式写出(编码)
public class ioText {
    public static void main(String[] args) {
        InputStreamReader inputStreamReader = null;
        OutputStreamWriter outputStreamWriter = null;
        try {
            inputStreamReader = new InputStreamReader(new FileInputStream
                    ("D:\\Java\\JavaFoundationUp\\src\\file\\a.txt"),StandardCharsets.UTF_8);
            outputStreamWriter = new OutputStreamWriter(new FileOutputStream
                    ("D:\\Java\\JavaFoundationUp\\src\\file\\b.txt"),"GBK");

            char[] chars = new char[1024];
            int len;
            while ((len = inputStreamReader.read(chars)) != -1) {
                outputStreamWriter.write(chars, 0, len);
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            try {
                if (inputStreamReader != null && outputStreamWriter != null) {
                    inputStreamReader.close();
                    outputStreamWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

4.4 字符编码的补充说明

编码表的由来:

        计算机只能识别二进制数据,早期由来是电信号。为了方便应用计算机,让它可以识别各个国家的文字,就将各个国家的文字用数字来表示,并一一对应,形成一张表,这就是编码表。

常见的编码表:

  • ASCII:美国标准信息交换码(用一个字节的7位可以表示)
  • ISO8859-1:拉丁码表,欧洲码表(用一个字节的8位可以表示)
  • GB2312:中文编码表。最多两个字节编码所有字符
  • GBK:中文编码升级,融合了更多的中文文字符号。最多两个字节编码
  • Unicode:国际标准编码,融合了目前人类使用的所有字符。为每个字符分配唯一的字符码。所有的文字都用两个字节来表示。
  • UTF-8:变长的编码方式,可以用1-4个字节来表示一个字符。                       
  • 顾名思义,UTF-8就是每次8个位传输数据,而UTF-16就是每次16个位。这是为传输而设计的编码,并使编码无国界。Unicode只是定义了一个庞大的,全球通用的字符集,并为每个字符规定了唯一确定的编号,具体存储成什么样的字节流,取决于字符编码方案。推荐Unicode编码是UTF-8和UTF-16。

5.标准输入,输出流

5.1 标准输入,输出流的概述

  • System.in和System.out分别代表了系统标准的输入输出设备,默认的设备是:键盘,输入设备,输出设备是:显示器。
  • System.in的类型是InputStream,System.out的类型是PrintStream,其是OutputStream的子类,FilterOutputStream的子类.

重定向:通过System类的setln,setOut方法对默认设备进行改变。

  • public static void setln(InputStream in)
  • public static void setOut(PrintStream out)

5.2 标准输入流,输出流的实例演示

import java.io.*;

//标准输入流的使用案例
public class ioText {
    public static void main(String[] args) {
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        try {
            inputStreamReader = new InputStreamReader(System.in);
            bufferedReader = new BufferedReader(inputStreamReader);
            while (true){
                System.out.println("请输入字符串:");
                String s = bufferedReader.readLine();
                if ("e".equalsIgnoreCase(s)||"exit".equalsIgnoreCase(s)){
                    System.out.println("程序结束");break;
                }else {
                    System.out.println(s.toUpperCase());
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

6.打印流和数据流

6.1 打印流的使用

打印流的作用:实现将基本数据类型的数据格式转化为字符串输出

  • PrintStream和PrintWriter,提供了一系列重载的print()和println()方法,用于多种数据类型的输出;
  • PrintStream和PrintWriter的输出不会抛出IO异常;
  • PrintStream和PrintWriter有自动flush功能;
  • PrintStream打印的所有字符都使用平台默认的字符编码转换为字节,在需要写入字符而不是写入字节的情况下,应该使用PrintWriter类;
  • System.out返回的是PrintStream的实例。  
import java.io.FileOutputStream;
import java.io.PrintStream;

//打印流的使用
public class ioText {
    public static void main(String[] args) {
        PrintStream printStream = null;
        try{
            //创建打印输出流,设置自动刷新模式
            printStream = new PrintStream(new FileOutputStream
                    ("D:\\Java\\JavaFoundationUp\\src\\file\\b.txt"),true);
            //System.setOut() 把标准输出流到控制台重定向输出到printStream流文件下
            if(printStream != null)System.setOut(printStream);

            /*for (int i = 0; i < 255; i++) {
                System.out.print((char) i + " ");
                if (i % 50 == 0) {
                    System.out.println();
                }
            }*/
            System.out.println('a');

        }catch (Exception e){
            e.printStackTrace();
        }finally {
          if(printStream != null)printStream.close();
        }
    }
}

6.2 数据流的使用

数据流的概述
        为了方便的操作Java语言的基本数据类型和String的数据,可以使用数据流。数据流有两个类:(用于读取和写出基本数据类型,String类的数据):  DataInputStream(InputStream)和DataOutputStream(OutputStream)。

DataInputStream中的方法

  • boolean readBoolean()        
  •  byte readByte()
  • char readChar()                     
  • float readFloat() 
  • double readDouble()             
  • short readShort()    
  • long readlong()                     
  • int readInt() 
  • string readUTF()                   
  • void readFully(byte[] b)             

DataOutputStream中的方法:将上述的方法的read改为write即可

import org.junit.Test;
import java.io.*;

//数据流的使用
public class ioText {
    //创建数据输出流,将内存中的字符串,基本数据类型的变量写出到文件中
    public static void main(String[] args) {
        DataOutputStream dataOutputStream = null;
        try {
            dataOutputStream = new DataOutputStream(new FileOutputStream
                    ("D:\\Java\\JavaFoundationUp\\src\\file\\b.txt"));
            dataOutputStream.writeUTF("李晓明");
            dataOutputStream.flush();
            dataOutputStream.writeInt(23);
            dataOutputStream.flush();
            dataOutputStream.writeBoolean(true);
            dataOutputStream.flush();
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            try {
                if (dataOutputStream != null) dataOutputStream.close();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    //创建数据输入流,将文件中的字符串,基本数据类型的变量值读取到内存当中
    @Test
    public void dataRead(){
        DataInputStream dataInputStream = null;
        try {
            //创建数据输出流,将内存中的字符串,基本数据类型的变量写出到文件中
            dataInputStream = new DataInputStream(
                    new FileInputStream("D:\\Java\\JavaFoundationUp\\src\\file\\b.txt"));

            String name = dataInputStream.readUTF();
            int age = dataInputStream.readInt();
            boolean isMale = dataInputStream.readBoolean();

            //必须按照数据写出文件的顺序来对文件内的数据进行读取到内存当中
            System.out.println("name:"+name);
            System.out.println("age:"+age);
            System.out.println("isMale:"+isMale);
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            try {
                if (dataInputStream != null) dataInputStream.close();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }
}

7.对象流和随机存取文件流

7.1 对象的序列化

  • 对象序列化机制允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。当其他程序获取了这种二进制流,就可以恢复成原来的Java对象
  • 序列化的好处在于可将任何实现了 Serializable 接口的对象转化为字节数据,使其在保存和传输时可被还原
  • 序列化是 RMI(Remote Method Invoke - 远程方法调用)过程的参数和返回值都必须实现的机制,而 RMI 是 JavaEE 的基础。因此序列化机制是JavaEE平台的基础
  • 如果需要让某个对象支持序列化机制,则必须让对象所属的类及其属性是可序列化的,为了让某个类是可序列化的,该类必须实现如下两个接口之一: Serializable 和 Externalizable,否则,会抛出异常
  • 实现 Serializable 接口的类都有一个表示序列化版本标识符的静态变量: 
    • serialVersionUID 用来表明类的不同版本间的兼容性。简言之,其目的是以序列化对象进行版本控制,有关各版本反序列化时是否兼容。如果类没有显示定义这个静态变量,它的值是Java运行时环境根据类的内部细节自动生成的。若类的实例变量做了修改,serialVersionUID可能发生变化。故建议,显示声明。
    public static final long serialVersionUID = 475463534532L;
  • 简单来说,Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的。在进行反序列化时,JVM会把传来的字节流中的serialVersionUID与本地相应实体类的serialVersionUID进行比较,如果相同就认为是一致的,可以进行反序列化,否则就会出现序列化版本不一致的异常。

7.2 对象流

  • ObjectInputStream和ObjectOutputStream
  • 用于存储和读取基本数据类型数据或对象的处理流、他的强大之处就是可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来。
  • 序列化:用 ObjectOutputStream 类保存基本类型数据或对象的机制
  • 反序列化:用 ObjectInputStream 类读取基本类型数据或对象的机制
  • ObjectInputStream 和 ObjectOutputStream 不能序列化 static 和transient 修饰的成员变量

7.3 使用对象流序列化对象

  • 若某个类实现了 Serializable 接口,该类的对象就是可以序列化的:创建一个 ObjectOutputStream;调用 ObjectOutputStream 对象的 writeObject(对象)方法输出可序列化对象 ;注意写出一次,操作flush()一次
  • 反序列化:创建一个 ObjectInputStream;调用 readObject() 方法读取流中的对象
  • 强调:如果某个类的属性不是基本数据类型或 String 类型,而是另一个引用类型,那么这个引用类型必须是可序列化的,否则拥有该类型的 Field 类也不能序列化。
//对象流的使用
public class ObjectOutputStreamText {
    //序列化
    public static void main(String[] args) {
        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(new FileOutputStream("object.dat"));
            objectOutputStream.writeObject("李晓明");
            objectOutputStream.flush();
            objectOutputStream.writeObject(new Person("李四"));
            objectOutputStream.flush();
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            try {
                if (objectOutputStream != null) objectOutputStream.close();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    //反序列化
    @Test
    public void ObjectInputStreamTest() {
        ObjectInputStream objectInputStream = null;
        try {
            objectInputStream = new ObjectInputStream(new FileInputStream("object.dat"));
            System.out.println((String) objectInputStream.readObject());
            Person person = (Person) objectInputStream.readObject();
            System.out.println(person.getName());
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            try {
                if (objectInputStream != null) objectInputStream.close();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }
}

class Person implements Serializable {

    public static final long serialVersionUID = 475463534532L;
    private String name;

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

7.4 随机存取文件流

  • 声明在 java.io 包下,但直接继承于 java.lang.Object类。并且它实现了DataInput,DataOutput这两个接口,也就意味着这个类既可以读也可以写。
  • RandomAccessFile 类支持“随机访问”的方式,程序可以直接跳到文件的任意地方来读,写文件:支持只访问文件的部分内容,可以向已存在的文件后追加内容。               
  • RandomAccessFile  对象包含一个记录指针,用以标示当前读写处的位置;
  • RandomAccessFile  类对象可以自由移动记录指针:                                                 
    • long getFilePointer():获取文件记录指针的当前位置;
    • void seek(long pos):将文件记录指针定位到pos位置。
  • 构造器:
    • public RandomAccessFile (File file,String mode)
    • public RandomAccessFile (String name,String mode)
  • 创建 RandomAccessFile 类实例需要指定一个mode参数,该参数指定 RandomAccessFile 的访问模式:
    • r:以只读方式打开;
    • rw:打开以便读取和写入
    • rwd:打开以便读取和写入,同步文件内容的更新
    • rws:打开以便读取和写入,同步文件内容和元数据的更新
  • 如果模式为只读r,则不会创建文件,而是会去读取一个已经存在的文件,如果读取的文件不存在则会出现异常。如果模式为rw读写。如果文件不存在则会去创建文件,如果存在则不会创建。

7.5 随机存取文件流的使用

//随机存取文件流的使用
public class RandomAccessFileTest {
    //文件的复制
    public static void main(String[] args) {
        RandomAccessFile randomAccessFile1 = null;
        RandomAccessFile randomAccessFile2 = null;
        try {
            randomAccessFile1 = new RandomAccessFile(new File("D:\\Java\\JavaFoundationUp\\src\\file\\a.jpeg"), "r");
            randomAccessFile2 = new RandomAccessFile(new File("D:\\Java\\JavaFoundationUp\\src\\file\\b.jpeg"), "rw");

            byte[] bytes = new byte[1024];
            int len;
            while ((len = randomAccessFile1.read(bytes))!=-1){
                randomAccessFile2.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (randomAccessFile1 != null && randomAccessFile2 != null) {
                try {
                    randomAccessFile1.close();
                    randomAccessFile2.close();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        }
    }

    //文件内容的覆盖效果
    @Test
    public void RandomAccessFileTest2() throws IOException {
        File file = new File("D:\\Java\\JavaFoundationUp\\src\\file\\a.txt");
        RandomAccessFile randomAccessFile1 = new RandomAccessFile(file,"rw");
/*      randomAccessFile1.seek(1);//从指定位置进行覆盖
        randomAccessFile1.write("11111".getBytes(StandardCharsets.UTF_8));*/
        randomAccessFile1.seek(file.length());//从文件末尾写入数据
        randomAccessFile1.write("a".getBytes());
        randomAccessFile1.close();
    }

    //文件内容的插入效果
    @Test
    public void RandomAccessFileTest3() throws IOException {
        File file = new File("D:\\Java\\JavaFoundationUp\\src\\file\\b.txt");
        RandomAccessFile randomAccessFile1 = new RandomAccessFile(file,"rw");
        
        randomAccessFile1.seek(2);
        StringBuilder stringBuilder = new StringBuilder((int) file.length());
        
        byte[] bytes = new byte[1024];
        int len;
        while ((len = randomAccessFile1.read(bytes))!=-1){
            stringBuilder.append(new String(bytes,0,len));
        }
        
        randomAccessFile1.seek(2);
        randomAccessFile1.write(("bb"+stringBuilder.toString()).getBytes());
        randomAccessFile1.close();
    }
}

8.NIO.2中Path,Paths,Files类的使用

8.1 Java NIO 概述

  • Java NIO(New IO,Non-Blocking IO)是从Java 1.4版本开始引入的一套新的IO API,可以替代标准的Java IO API。NIO与原来的IO有同样的作用和目的,但是使用的方式完全不同,NIO支持面向缓冲区的(IO是面向流的),基于通道的IO操作。NIO将以更加高效的方式进行文件的读写操作。
  • Java API中提供了两套NIO,一套是针对标准输入输出NIO,另一套就是网络编程NIO。
    • –>java.nio.channels.Channel
    • –>FileChannel:处理本地文件
    • –>SocketChannel:TCP网络编程的客户端的Channel
    • –>ServerSocketChannel:TCP网络编程的服务器端的Channel
    • –>DatagramChannel:UDP网络编程中发送端和接收端的Channel
  • 随着 JDK 7 的发布,Java对NIO进行了极大的扩展,增强了对文件处理和文件系统特效的支持,以至于我们称他们为 NIO.2。因为NIO 提供的一些功能,NIO已经成为文件处理中越来月重要的部分。

8.2 Path,Paths,Files类核心API

  • 早期的Java只提供了一个File类来访问文件系统,但File类的功能比较有限,所提供的方法性能也不高。而且,大多数方法在出错时仅返回失败,并不会提供异常信息。
  • NIO.2 为了弥补这种不足,引入了Path接口,代表一个平台无关的平台路径,描述了目录结构中文件的位置。Path可以看成是File类的升级版本,实际引用的资源也可以不存在。
import java.io.File;
File file = new File("index.html");
等同于:
import java.nio.file.Path; 
import java.nio.file.Paths; 
Path path = Paths.get("index.html");

  • 同时,NIO.2在java.nio.file包下还提供了Files,Paths工具类,Files包含了大量静态的工具方法来操作文件;Paths则包含了两个返回Path的静态工厂方法。
  • Paths 类提供的静态 get() 方法来获取 Path 对象:
    • static Path get(String first,String...more):将多个字符串串连成路径
    • static Path get(URI uri):返回指定uri对应的Path路径

8.3 Path接口的常用方法

  • String toString() : 返回调用 Path 对象的字符串表示形式
  • boolean startsWith(String path) : 判断是否以 path 路径开始
  • boolean endsWith(String path) : 判断是否以 path 路径结束
  • boolean isAbsolute() : 判断是否是绝对路径
  • Path getParent():返回Path对象的整个路径,不包含 Path 对象指定的文件路径
  • Path getRoot() :返回调用 Path 对象的根路径
  • Path getFileName() : 返回与调用 Path 对象关联的文件名
  • int getNameCount() : 返回Path 根目录后面元素的数量
  • Path getName(int idx) : 返回指定索引位置 idx 的路径名称
  • Path toAbsolutePath() : 作为绝对路径返回调用 Path 对象
  • Path resolve(Path p) :合并两个路径,返回合并后的路径对应的Path对象
  • File toFile(): 将Path转化为File类的对象
public class PathTest {
    //如何使用Paths实例化Path
    @Test
    public void test1() {
        Path path1 = Paths.get("d:\\nio\\hello.txt");//new File(String filepath)
        Path path2 = Paths.get("d:\\", "nio\\hello.txt");//new File(String parent,String filename);

        System.out.println(path1);
        System.out.println(path2);

        Path path3 = Paths.get("d:\\", "nio");
        System.out.println(path3);
    }

    //Path中的常用方法
    @Test
    public void test2() {
        Path path1 = Paths.get("d:\\", "nio\\nio1\\nio2\\hello.txt");
        Path path2 = Paths.get("hello.txt");

//		String toString() : 返回调用 Path 对象的字符串表示形式
        System.out.println(path1);

//		boolean startsWith(String path) : 判断是否以 path 路径开始
        System.out.println(path1.startsWith("d:\\nio"));
//		boolean endsWith(String path) : 判断是否以 path 路径结束
        System.out.println(path1.endsWith("hello.txt"));
//		boolean isAbsolute() : 判断是否是绝对路径
        System.out.println(path1.isAbsolute() + "~");
        System.out.println(path2.isAbsolute() + "~");
//		Path getParent() :返回Path对象包含整个路径,不包含 Path 对象指定的文件路径
        System.out.println(path1.getParent());
        System.out.println(path2.getParent());
//		Path getRoot() :返回调用 Path 对象的根路径
        System.out.println(path1.getRoot());
        System.out.println(path2.getRoot());
//		Path getFileName() : 返回与调用 Path 对象关联的文件名
        System.out.println(path1.getFileName() + "~");
        System.out.println(path2.getFileName() + "~");
//		int getNameCount() : 返回Path 根目录后面元素的数量
//		Path getName(int idx) : 返回指定索引位置 idx 的路径名称
        for (int i = 0; i < path1.getNameCount(); i++) {
            System.out.println(path1.getName(i) + "*****");
        }

//		Path toAbsolutePath() : 作为绝对路径返回调用 Path 对象
        System.out.println(path1.toAbsolutePath());
        System.out.println(path2.toAbsolutePath());
//		Path resolve(Path p) :合并两个路径,返回合并后的路径对应的Path对象
        Path path3 = Paths.get("d:\\", "nio");
        Path path4 = Paths.get("nioo\\hi.txt");
        path3 = path3.resolve(path4);
        System.out.println(path3);

//		File toFile(): 将Path转化为File类的对象
        File file = path1.toFile();//Path--->File的转换
        Path newPath = file.toPath();//File--->Path的转换
    }
}

8.3 Files类

java.nio.file.Files 用于操作文件或目录的工具类。

  • Files类的常用方法
    • Path copy(Path src, Path dest, CopyOption … how) : 文件的复制
    • Path createDirectory(Path path, FileAttribute<?> … attr) : 创建一个目录
    • Path createFile(Path path, FileAttribute<?> … arr) : 创建一个文件
    • void delete(Path path) : 删除一个文件/目录,如果不存在,执行报错
    • void deleteIfExists(Path path) : Path对应的文件/目录如果存在,执行删除
    • Path move(Path src, Path dest, CopyOption…how) : 将 src 移动到 dest 位置
    • long size(Path path) : 返回 path 指定文件的大小
  • Files常用方法:用于判断
    • boolean exists(Path path, LinkOption … opts) : 判断文件是否存在
    • boolean isDirectory(Path path, LinkOption … opts) : 判断是否是目录
    • boolean isRegularFile(Path path, LinkOption … opts) : 判断是否是文件
    • boolean isHidden(Path path) : 判断是否是隐藏文件
    • boolean isReadable(Path path) : 判断文件是否可读
    • boolean isWritable(Path path) : 判断文件是否可写
    • boolean notExists(Path path, LinkOption … opts) : 判断文件是否不存在
  • Files常用方法:用于操作内容
    • SeekableByteChannel newByteChannel(Path path, OpenOption…how) : 获取与指定文件的连接,how 指定打开方式。
    • DirectoryStream newDirectoryStream(Path path) : 打开 path 指定的目录
    • InputStream newInputStream(Path path, OpenOption…how):获取 InputStream 对象
    • OutputStream newOutputStream(Path path, OpenOption…how) : 获取 OutputStream 对象
/**
 * Files工具类的使用:操作文件或目录的工具类
 */
public class FilesTest {

	@Test
	public void test1() throws IOException{
		Path path1 = Paths.get("d:\\nio", "hello.txt");
		Path path2 = Paths.get("atguigu.txt");
		
//		Path copy(Path src, Path dest, CopyOption … how) : 文件的复制
		//要想复制成功,要求path1对应的物理上的文件存在。path1对应的文件没有要求。
//		Files.copy(path1, path2, StandardCopyOption.REPLACE_EXISTING);
		
//		Path createDirectory(Path path, FileAttribute<?> … attr) : 创建一个目录
		//要想执行成功,要求path对应的物理上的文件目录不存在。一旦存在,抛出异常。
		Path path3 = Paths.get("d:\\nio\\nio1");
//		Files.createDirectory(path3);
		
//		Path createFile(Path path, FileAttribute<?> … arr) : 创建一个文件
		//要想执行成功,要求path对应的物理上的文件不存在。一旦存在,抛出异常。
		Path path4 = Paths.get("d:\\nio\\hi.txt");
//		Files.createFile(path4);
		
//		void delete(Path path) : 删除一个文件/目录,如果不存在,执行报错
//		Files.delete(path4);
		
//		void deleteIfExists(Path path) : Path对应的文件/目录如果存在,执行删除.如果不存在,正常执行结束
		Files.deleteIfExists(path3);
		
//		Path move(Path src, Path dest, CopyOption…how) : 将 src 移动到 dest 位置
		//要想执行成功,src对应的物理上的文件需要存在,dest对应的文件没有要求。
//		Files.move(path1, path2, StandardCopyOption.ATOMIC_MOVE);
		
//		long size(Path path) : 返回 path 指定文件的大小
		long size = Files.size(path2);
		System.out.println(size);

	}

	@Test
	public void test2() throws IOException{
		Path path1 = Paths.get("d:\\nio", "hello.txt");
		Path path2 = Paths.get("atguigu.txt");
//		boolean exists(Path path, LinkOption … opts) : 判断文件是否存在
		System.out.println(Files.exists(path2, LinkOption.NOFOLLOW_LINKS));

//		boolean isDirectory(Path path, LinkOption … opts) : 判断是否是目录
		//不要求此path对应的物理文件存在。
		System.out.println(Files.isDirectory(path1, LinkOption.NOFOLLOW_LINKS));

//		boolean isRegularFile(Path path, LinkOption … opts) : 判断是否是文件

//		boolean isHidden(Path path) : 判断是否是隐藏文件
		//要求此path对应的物理上的文件需要存在。才可判断是否隐藏。否则,抛异常。
//		System.out.println(Files.isHidden(path1));

//		boolean isReadable(Path path) : 判断文件是否可读
		System.out.println(Files.isReadable(path1));
//		boolean isWritable(Path path) : 判断文件是否可写
		System.out.println(Files.isWritable(path1));
//		boolean notExists(Path path, LinkOption … opts) : 判断文件是否不存在
		System.out.println(Files.notExists(path1, LinkOption.NOFOLLOW_LINKS));
	}

	/**
	 * StandardOpenOption.READ:表示对应的Channel是可读的。
	 * StandardOpenOption.WRITE:表示对应的Channel是可写的。
	 * StandardOpenOption.CREATE:如果要写出的文件不存在,则创建。如果存在,忽略
	 * StandardOpenOption.CREATE_NEW:如果要写出的文件不存在,则创建。如果存在,抛异常
	 */
	@Test
	public void test3() throws IOException{
		Path path1 = Paths.get("d:\\nio", "hello.txt");

//		InputStream newInputStream(Path path, OpenOption…how):获取 InputStream 对象
		InputStream inputStream = Files.newInputStream(path1, StandardOpenOption.READ);

//		OutputStream newOutputStream(Path path, OpenOption…how) : 获取 OutputStream 对象
		OutputStream outputStream = Files.newOutputStream(path1, StandardOpenOption.WRITE,StandardOpenOption.CREATE);
//		SeekableByteChannel newByteChannel(Path path, OpenOption…how) : 获取与指定文件的连接,how 指定打开方式。
		SeekableByteChannel channel = Files.newByteChannel(path1, StandardOpenOption.READ,StandardOpenOption.WRITE,StandardOpenOption.CREATE);

//		DirectoryStream<Path>  newDirectoryStream(Path path) : 打开 path 指定的目录
		Path path2 = Paths.get("e:\\teach");
		DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path2);
		Iterator<Path> iterator = directoryStream.iterator();
		while(iterator.hasNext()){
			System.out.println(iterator.next());
		}
	}
}

上一节:【JavaSE 知识框架】10 泛型

下一节:【JavaSE 知识框架】12 网络编程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

深山老Java

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值