9.File类和IO流

File类和IO流

1 File类

1.1 File类概述

​ 声明在java.io包下,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对象。

注意:不同的系统下路径分隔符不同,会造成一些兼容性问题。

​ 例如:windows和dos下为:\:当存在在字符串中的时候需要进行转义,该字符也可以代表转义。

​ UNIX,URL为:/

因此可以使用File.separator来解决兼容性问题。

File file2 = new File("E:" + File.separator + "projects" + File.separator + "ideacode" + File.separator +"atguigu" + File.separator + "javaseSenior" + File.separator + "06io" + File.separator + "XiXi.txt");
@Test
public void testFile1(){
    System.out.println("****构造器1:new File(String pathname)****");
    //相对路径的方式,在idea中相当于当前model的根目录下
    File file=new File("Hello.txt");
    //绝对路径
    File file1 = new File("E:\\projects\\ideacode\\atguigu\\javaseSenior\\06io\\hi.txt");
    //解决不同系统下的路径分隔符兼容性问题
    File file2 = new File("E:" + File.separator + "projects" + File.separator + "ideacode" + File.separator
            + "atguigu" + File.separator + "javaseSenior" + File.separator + "06io" + File.separator + "XiXi.txt");
    //此时硬盘中还没有文件,仅仅是在内存中创建了一个对象
    System.out.println(file);
    System.out.println(file1);
    System.out.println(file2);
    System.out.println("****构造器2:new File(String parent,String child)****");
    //可以是文件或者文件目录
    File io = new File("E:\\projects\\ideacode\\atguigu\\javaseSenior\\06io", "io");
    System.out.println(io);
    System.out.println("****构造器3:new File(File parentFile,String childPath)****");
    File file3 = new File(io, "io.txt");
    System.out.println(file3);
}

运行结果:
****构造器1:new File(String pathname)****
Hello.txt
E:\projects\ideacode\atguigu\javaseSenior\06io\hi.txt
E:\projects\ideacode\atguigu\javaseSenior\06io\XiXi.txt
****构造器2:new File(String parent,String child)****
E:\projects\ideacode\atguigu\javaseSenior\06io\io
****构造器3:new File(File parentFile,String childPath)****
E:\projects\ideacode\atguigu\javaseSenior\06io\io\io.txt

1.3 常见方法

1 File 类的获取功能(硬盘中有无对其影响比较大)

  • public String getAbsolutePath():获取绝对路径,从磁盘开始
  • public String getPath() :获取路径
  • public String getName() :获取名称
  • public String getParent():获取上层文件目录路径。若无,返回null
  • public long length() :获取文件长度(即:字节数)。不能获取目录的长度。
  • public long lastModified() :获取最后一次的修改时间,毫秒值
  • public String[] list() :获取指定目录下的所有文件或者文件目录的名称数组
  • public File[] listFiles() :获取指定目录下的所有文件或者文件目录的File数组
@Test
public void testFile2(){
    File file1 = new File("hello.txt");
    //获取绝对路径:E:\projects\ideacode\atguigu\javaseSenior\06io\hello.txt
    System.out.println(file1.getAbsoluteFile());
    //获取相对路径:hello.txt
    System.out.println(file1.getPath());
    //获取名字:hello.txt
    System.out.println(file1.getName());
    //获取上层文件目录:null
    System.out.println(file1.getParent());
    //获取长度:0  ==>11
    System.out.println(file1.length());
    //获取最后修改时间:0==>1584844277869
    System.out.println(file1.lastModified());
    System.out.println("***************");
    File file2 = new File("E:\\io\\hello.txt");
    //获取绝对路径:E:\io\hello.txt
    System.out.println(file2.getAbsoluteFile());
    //获取相对路径:E:\io\hello.txt
    System.out.println(file2.getPath());
    //获取名字:hello.txt
    System.out.println(file2.getName());
    //获取上层文件目录:E:\io
    System.out.println(file2.getParent());
    //获取长度:0
    System.out.println(file2.length());
    //获取最后修改时间:0
    System.out.println(file2.lastModified());
}

@Test
    public void testFile3(){
        File file = new File("E:\\projects\\ideacode\\atguigu\\javaseSenior\\06io");
        String[] list = file.list();
        for (String s:list) {
            //06io.iml	hello.txt	src
            System.out.print(s+"\t");
        }
        File[] files = file.listFiles();
        for (File fil:files
        ) {
            //E:\projects\ideacode\atguigu\javaseSenior\06io\06io.iml
            //E:\projects\ideacode\atguigu\javaseSenior\06io\hello.txt
            //E:\projects\ideacode\atguigu\javaseSenior\06io\src
            System.out.println(fil);
        }
    }

2 类的重命名功能

public boolean renameTo(File dest):把文件重命名为指定的文件路径

/**
 * 方法的重命名:要求file1在硬盘中存在,file2不存在,如果file2路径中存在目录,目录必须真实存在,否则会修   改失败。
 */
@Test
public void testFile4(){
    File file1 = new File("hello.txt");
    File file2 = new File("E:\\io.txt");
    boolean b = file1.renameTo(file2);
    boolean b1 = file2.renameTo(file1);
    System.out.println(b);
    System.out.println(b1);
}

3 File 类的判断功能(对硬盘上的文件进行判断操作)

  • public boolean isDirectory():判断是否是文件目录
  • public boolean isFile() :判断是否是文件
  • public boolean exists() :判断是否存在
  • public boolean canRead() :判断是否可读
  • public boolean canWrite() :判断是否可写
  • public boolean isHidden() :判断是否隐藏

4 File 类的创建功能(在硬盘中创建文件)

  • public boolean createNewFile() :创建文件。若文件存在,则不创建,返回false

  • public boolean mkdir() :创建文件目录。如果此文件目录存在,就不创建了。如果此文件目录的上层目录不存在,也不创建。

  • public boolean mkdirs() :创建文件目录。如果上层文件目录不存在,一并创建

    注意事项:如果你创建文件或者文件目录没有写盘符路径,那么默认在项目路径下 。

5 File 类的删除功能

  • public boolean delete():删除文件或者文件夹

注意事项:Java中的删除不走回收站,直接就删除。要删除一个文件目录,请注意该文件目录内不能包含文件或者文件目录

@Test
public void testFile5(){
    File file = new File("he.txt");
    boolean newFile=false;
    if (!file.exists()){
        try {
            newFile= file.createNewFile();
        } catch (IOException e) {
            System.out.println(e);
        }
    }
    if (file.exists()){
        boolean delete = file.delete();
        if (delete){
            System.out.println("删除成功!");
        }else{
            System.out.println("删除失败");
        }
    }
    if (newFile){
        System.out.println("创建成功!");
    }else {
        System.out.println("创建失败");
    }
    File file1 = new File("io//Hello");
    boolean mkdirs = file1.mkdirs();
    if (mkdirs){
        System.out.println("文件夹创建成功");
    }else {
        System.out.println("创建失败");
    }
    File io = new File("io");
    boolean delete = io.delete();
    if (delete){
        System.out.println("删除成功");
    }else {
        System.out.println("删除失败");
    }
}

2 IO流

2.1 java io原理

​ I/O是Input/output的缩写,I/O用于处理设备之间的数据传输,如读/写文件,网络通信等。在java程序中,对于数据的输入输出操作都是以“流”的方式进行的。java.io包下提供了各种流类和接口,用于获取不同种类的数据,并通过相应的方法输入或输出数据。

  • 输入:读取外部数据到程序的内存中。
  • 输出:将程序的内存中的数据输出到磁盘中。

注意:输入和输出都是相对的概念,我们都是站在内存的角度来看待输入和输出。

2.2 流的分类

  1. 按照操作的数据单位不同:字节流(8bit),字符流(16bit)。
  2. 按数据流的流向不同分为:输入流,输出流。
  3. 按流的角色的不同分为:
  • 节点流==>直接从数据源或目的地读写数据

image-20201028114140689

  • 处理流==>不直接连接到数据源或目的地,而是“连接”在已存在的流(节点流或处理流)之上,通过对数据的处理为程序提供更为强大的读写功能。

image-20201028114203381

2.3 流的体系

image-20201028114216887

2.3 流之字符流

2.3.1 字符输入流

作用:将文件中的内容读取到内存中去

/**
 * 将文件中的内容读取到内存中
 * 说明:
 *  1.read():当读到文件的末尾,则返回-1,否则返回读入的一个字符。
 *  2.异常处理:流资源必须关闭,所以将close()放到finally中去==>try-catch-finally。
 *  3.读入的文件一定要存在,否则会报异常FileNotFoundException
 */
@Test
public void testFileReader1(){
    //1.实例化File类的对象,
    File file = new File("hello.txt");
    FileReader reader=null;
    try {
        //2.提供具体的流
        reader = new FileReader(file);
        int read;
        //3.数据的读入
        while ((read=reader.read())!=-1){
            System.out.print((char) read);
        }
    } catch (FileNotFoundException e) {
        System.out.println(e.getMessage());
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }finally {
        try {
            //4.关闭流
            if(reader!=null){
                reader.close();
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}

	/**
     * reader.read(arr):读取arr长度的字符到数组中去,返回值为读取的字符的数量。
     */
    @Test
    public void testFileReader2(){
        //1.实例化File类的对象,
        File file = new File("hello.txt");
        FileReader reader=null;
        try {
            //2.提供具体的流
            reader = new FileReader(file);
            int read;
            //3.数据的读入
            char[] arr=new char[5];
            //
            while ((read=reader.read(arr))!=-1){
                //System.out.print(Arrays.toString(arr));==>错误的写法,读入数据可能出现错误
                /*for (int i = 0; i < read; i++) {
                    System.out.print(arr[i]);
                }
                System.out.println();*/
                String string=new String(arr,0,read);
                System.out.print(string);
            }
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }finally {
            try {
                //4.关闭流
                if(reader!=null){
                    reader.close();
                }
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }
2.3.2 字符输出流

作用:将内存中的数据以字符的形式输出指定的文件中去。

/**
 * 使用FileWriter类将内存中的文件写出到硬盘中。
 * 说明:>对应的file可以不存在,如果不存在会帮我们自动创建。
 *      >writer = new FileWriter(file,true);==>true是对文件的内容进行追加。
 *      >writer = new FileWriter(file,false);或者不写false==>是对原有文件的覆盖。
 */
@Test
public void testFileWriter(){
    //1.创建file对象
    File file = new File("helloworld1.txt");
    FileWriter writer=null;
    try {
        //2.创建FileWriter对象,如果文件不存在会自动创建
        writer = new FileWriter(file,true);
        //3.写出操作
        writer.write("张三李四王五赵六田七\n");
        writer.write("王八");
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }finally {
        //4.关闭流操作
        if (writer!=null){
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
2.3.3 文本文件的复制
/**
 * 复制文件文本的内容的操作
 * 注意:不能使用字符流来处理图片等字节数据。
 */
@Test
public void testFileReaderAndFileWriter(){
    //1.创建File类对象
    File file1 = new File("房屋.jpg");
    File file2 = new File("房屋1.jpg");
    //2.声明FileReader和FileWriter变量
    FileReader reader =null;
    FileWriter writer=null;
    try {
        //3.创建FileReader和FileWriter对象
        reader = new FileReader(file1);
        writer=new FileWriter(file2);
        //4.数据的读写操作
        char[] chars=new char[5];
        int len=0;
        while((len=reader.read(chars))!=-1){
            writer.write(chars,0,len);
            /*for (int i = 0; i < len; i++) {
                writer.write(chars[i]);
            }*/
        }
    } catch (FileNotFoundException e) {
        System.out.println(e.getMessage());
    } catch (IOException e) {
        System.out.println(e.getMessage());
    } finally{
        //5.流的关闭
        if (reader!=null){
            try {
                reader.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
        if (writer!=null){
            try {
                writer.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }
}

2.4 流之字节流

作用:用于处理非文本文件。例如jpg,MP3,mp4,avi,doc等

2.4.1 常见使用

1 jpg文件的复制

/**
 * 复制字节存储的文件的内容.
 * 说明:>当我们使用字节流读含有非汉字的文本文件读取到内存是可以的,但是有汉字的时候就会出现乱码。
 */
@Test
public void testFileOutputStreamAndFileInputStream(){
    //1.创建File类对象
    File file1 = new File("房屋.jpg");
    File file2 = new File("房屋2.jpg");
    //2.声明变量
    FileInputStream fis=null;
    FileOutputStream fos=null;
    try {
        //3.创建字节输入输出流
        fis=new FileInputStream(file1);
        fos=new FileOutputStream(file2);
        byte[] bytes=new byte[1024];
        int len=0;
        //4.进行数据的读写
        while((len=fis.read(bytes))!=-1){
            fos.write(bytes,0,len);
        }
    } catch (FileNotFoundException e){
        System.out.println(e.getMessage());
    }catch (IOException e) {
        System.out.println(e.getMessage());
    }finally {
        if (fis!=null){
            try {
                fis.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
        if (fos!=null){
            try {
                fos.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }
}

2 非文本文件的复制

@Test
public void testMethod(){
    long startTime=System.currentTimeMillis();
    copyFile("E:\\刷机包.zip","E:\\刷机包2.zip");
    long endTime=System.currentTimeMillis();
    //花费的时间为:19841==>该操作花费的时间和byte[]数组的长度有一定的关系。
    System.out.println("花费的时间为:"+(endTime-startTime));
}
/**
 * 指定路径下文件的复制
 */
public static void copyFile(String path,String destPath){
    //1.创建File类对象
    File file1 = new File(path);
    File file2 = new File(destPath);
    //2.声明变量
    FileInputStream fis=null;
    FileOutputStream fos=null;
    try {
        //3.创建字节输入输出流
        fis=new FileInputStream(file1);
        fos=new FileOutputStream(file2);
        byte[] bytes=new byte[1024];
        int len=0;
        //4.进行数据的读写
        while((len=fis.read(bytes))!=-1){
            fos.write(bytes,0,len);
        }
    } catch (FileNotFoundException e){
        System.out.println(e.getMessage());
    }catch (IOException e) {
        System.out.println(e.getMessage());
    }finally {
        if (fis!=null){
            try {
                fis.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
        if (fos!=null){
            try {
                fos.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }
}

2.5 流之缓冲流

作用:缓冲流主要是为了提高文件的读写效率,缓冲流主要包含了以下几个类。

  • BufferedInputStream

  • BufferedOutputStream

  • BufferedReader(多了一个readLine())

  • BufferedWriter

内部存在一个缓冲区数组,默认长度为8192,即当达到这个容量之后,就会进行flush操作将数据写出去。当我们手动调用的时候,他也会将缓冲区中的数据写出去。

image-20201028114235906

2.5.1 简单使用
@Test
public void testBufferedReader(){
    long startTime=System.currentTimeMillis();
    copyFile("E:\\刷机包.zip","E:\\刷机包2.zip");
    long endTime=System.currentTimeMillis();
    //花费的时间为:5856==>该操作花费的时间和byte[]数组的长度有一定的关系。
    System.out.println("花费的时间为:"+(endTime-startTime));
}
/**
 * 指定路径下文件的复制
 */
public static void copyFile(String path,String destPath){
    //1.创建File类对象
    File file1 = new File(path);
    File file2 = new File(destPath);
    //2.声明流变量
    FileInputStream fis=null;
    FileOutputStream fos=null;
    BufferedInputStream bis=null;
    BufferedOutputStream bos=null;
    try {
        //3.创建字节输入输出流
        fis=new FileInputStream(file1);
        fos=new FileOutputStream(file2);
        bis=new BufferedInputStream(fis);
        bos=new BufferedOutputStream(fos);
        //4.进行数据的读写
        byte[] bytes=new byte[1024];
        int len=0;
        while((len=bis.read(bytes))!=-1){
            bos.write(bytes,0,len);
        }
    } catch (FileNotFoundException e){
        System.out.println(e.getMessage());
    }catch (IOException e) {
        System.out.println(e.getMessage());
    }finally {
        //先关闭外层的流,后关闭内层的流;关闭外层流的同时,内层流也会自动的进行关闭。
        if (bis!=null){
            try {
                bis.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
        if (bos!=null){
            try {
                bos.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
        if (fis!=null){
            try {
                fis.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
        if (fos!=null){
            try {
                fos.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }
}

/**
 * 复制文件文本的内容的操作
 * 注意:不能使用字符流来处理图片等字节数据。
 */
@Test
public void testFileReaderAndFileWriter(){
    BufferedReader bReader=null;
    BufferedWriter bwriter=null;
    try {
        bReader=new BufferedReader(new FileReader(new File("dbcp.txt")));
        bwriter=new BufferedWriter(new FileWriter(new File("dbcp1.txt")));
        /*方式一:
        int len=0;
        char[] chars=new char[1024];
        while ((len=bReader.read(chars))!=-1){
             bwriter.write(chars,0,len);
             //bwriter.flush();
        }*/
        //方式二:使用string
        String data;
        //一次读取一行,如果到了末尾,则返回值为null
        while((data=bReader.readLine())!=null){
            //不包含换行符:手动添加,或者调用方法
            //bwriter.write(data+"\n");
            bwriter.write(data);
            bwriter.newLine();
        }


    } catch (FileNotFoundException e) {
        System.out.println(e.getMessage());
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }finally {
        if (bReader!=null){
            try {
                bReader.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
        if (bwriter!=null){
            try {
                bwriter.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }
}

2.6 流之字符转换流

1 作用:主要是为了进行字节流和字符流之间进行转换。包括以下几个类

  • InputStreamReader:字节输入流转换为字符输入流。
  • OutputStreamWriter:字符输出流转换为字节输出流

image-20201028114247772

2 简单实用

/**
 * 字节的输入转换为字符的输入
 */
@Test
public void testConvertedFlow1(){
    FileInputStream fis=null;
    InputStreamReader isr=null;
    try {
        fis=new FileInputStream("dbcp_gbk.txt");
        //isr=new InputStreamReader(fis);
        //参数2指明字符集,具体使用什么字符集,取决于文件保存的格式
        isr=new InputStreamReader(fis,"GBK");
        char[] buff=new char[20];
        int len=0;
        while((len=isr.read(buff))!=-1){
            String string=new String(buff,0,len);
            System.out.print(string);
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if (isr!=null){
            try {
                isr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
/**
 * 综合使用:InputStreamReader和OutputStreamWriter
 */
@Test
public void testConvertedFlow2(){
    FileInputStream fis=null;
    FileOutputStream fos=null;
    InputStreamReader isr=null;
    OutputStreamWriter osw=null;
    try {
        fis=new FileInputStream("dbcp.txt");
        fos=new FileOutputStream("dbcp_gbk1.txt");
        //isr=new InputStreamReader(fis);
        //参数2指明字符集,具体使用什么字符集,取决于文件保存的格式
        isr=new InputStreamReader(fis,"utf-8");
        osw=new OutputStreamWriter(fos,"GBK");
        char[] buff=new char[20];
        int len=0;
        while((len=isr.read(buff))!=-1){
            osw.write(buff,0,len);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if (isr!=null){
            try {
                isr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (osw!=null){
            try {
                osw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

3 字符转换流作用:提供字节流和字符流之间的转换。

  • 编码:字节,字节数组–>字符数组,字符串
  • 解码:字符数组,字符串–>字节,字节数组

4 字符集的问题

①编码表的由来

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

②常见的编码表

  • ASCII:美国标准信息交换码。用一个字节的7位可以表示。
  • ISO8859-1:拉丁码表。欧洲码表用一个字节的8位表示。
  • GB2312:中国的中文编码表。最多两个字节编码所有字符
  • GBK:中国的中文编码表升级,融合了更多的中文文字符号。最多两个字节编码
  • Unicode:国际标准码,融合了目前人类使用的所有字符。为每个字符分配唯一的字符码。所有的文字都用两个字节来表示。
  • UTF-8:变长的编码方式,可用1-4个字节来表示一个字符,中文是三个字符。

③编码表解决的一些问题

​ GBK和GB2312如何区别是一个字节表示还是两个字节表示一个字符,看第一位,如果是0表示一个字节就是一个字符,如果是1就代表两个字节代表一个字符。

​ Unicode存在问题:存储到底层文件,两个字节存储一个字符,出现了和GBK一样的问题,最高存储216个,如果和GBK一样的解决办法,只能存储215,又不够了。Unicode不完美,这里就有三个问题,一个是,我们已经知道,英文字母只用一个字节表示就够了,第二个问题是如何才能区别Unicode和ASCII?计算机怎么知道两个字节表示一个符号,而不是分别表示两个符号呢?第三个,如果和GBK等双字节编码方式一样,用最高位是1或0表示两个字节和一个字节,就少了很多值无法用于表示字符,不够表示所有字符。Unicode在很长一段时间内无法推广,直到互联网的出现。

④解决方式

​ 面向传输的众多 UTF(UCS Transfer Format)标准出现了,顾名思义,UTF-8就是每次8个位传输数据,而UTF-16就是每次16个位。这是为传输而设计的编码,并使编码无国界,这样就可以显示全世界上所有文化的字符了。

​ Unicode只是定义了一个庞大的、全球通用的字符集,并为每个字符规定了唯一确定的编号,具体存储成什么样的字节流,取决于字符编码方案。推荐的Unicode编码是UTF-8和UTF-16。

image-20201028114259478

2.7 流之标准输入输出流

public static void main(String[] args) {
    test1();
}
/**
 * 1.标准的输入输出流
 * 1)System.in:标准输入流,默认从键盘输入。
 * 2)System.out:标准输出流,默认从控制台输出。
 * 2.System类的setIn(InputStream is)/setOut(PrintStream ps)方式重新指定输入和输出的设备。
 * 3.练习1:当键盘输入字符串,要求将读取的字符串转换为大写,然后进行输入操作,直到输入‘e’或者“exit”退出
 *  方法一:Scanner来实现。
 *  方法二:System.in来实现
 *  注意:idea中单元测试不可以用来使用System.in
 */
public static void test1(){
    //转换流==byte==>字符
    InputStreamReader isr=new InputStreamReader(System.in);
    //字符缓冲流
    BufferedReader br=new BufferedReader(isr);
    while (true){
        System.out.println("请输入字符串:");
        String data = null;
        try {
            //一次读取一行
            data = br.readLine();
            if ("e".equalsIgnoreCase(data)||"exit".equalsIgnoreCase(data)){
                System.out.println("程序结束");
                break;
            }
            String string = data.toUpperCase();
            System.out.println(string);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if (br!=null){
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2.8 流之打印流

/**
 * 打印流:PrintStream和PrintWriter
 * 1.提供了一系列重载的print()和println()方法。
 * 2.练习:将前255个ascll码输出到文件中去
 */
@Test
public void testPrint(){
    PrintStream ps = null;
    try {
        //此时文件不会再控制台进行输出,会在文件中输出。
        FileOutputStream fos = new FileOutputStream(new File("text.txt"));
        // 创建打印输出流,设置为自动刷新模式(写入换行符或字节 '\n' 时都会刷新输出缓冲区)
        ps = new PrintStream(fos, true);
        if (ps != null) {
            // 把标准输出流(控制台输出)改成文件
            System.setOut(ps);
        }
        for (int i = 0; i <= 255; i++) { // 输出ASCII字符
            System.out.print((char) i);
            if (i % 50 == 0) { // 每50个数据一行
                System.out.println(); // 换行
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (ps != null) {
            ps.close();
        }
    }
}

2.9 流之数据流

/**
 * 数据流
 * 1.DataInputStream和DataOutputStream
 * 2.作用:用于读取或者写出基本数据类型的变量或字符串。
 * 3.注意:读取数据的顺序要和写入时的顺序保持一致。
 */
@Test
public void testDataInputStream(){
    //1.创建数据流
    DataOutputStream dops=null;
    DataInputStream dis=null;
    try {
        dops=new DataOutputStream(new FileOutputStream("dops.txt"));
        dops.writeUTF("张三丰");
        dops.flush();
        dops.writeInt(78);
        dops.flush();
        dops.writeBoolean(true);
        dops.flush();
        dis=new DataInputStream(new FileInputStream("dops.txt"));
        //写的时候先写什么,就需要先读什么,按照顺寻
        String name = dis.readUTF();
        int age = dis.readInt();
        boolean isMale = dis.readBoolean();
        System.out.println("名字:"+name+"\n年龄:"+age+"\n是不是男性:"+isMale);

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

}

2.10 流之对象流

1 对象流的作用

​ 用于存储和读取基本数据类型或者对象的处理流,他的强大之处是可以将Java中的对象写入到数据源中,也能把对象从数据源中还原回来。

2 涉及到的两个名词

  • 序列化:用ObjectOutputStream类保存基本类型数据或对象的机制。

  • 反序列化:用ObjectInputStream类读取基本类型数据或对象的机制。

    注意:不能序列化static和transient修饰的成员变量,序列化之后无法将值保存下来。

3 对象序列化的机制

​ 1)允许把内存中的java对象转换成与平台无关的二进制流,从而允许把这种二进制流持久的保存到磁盘中,或者通过网络的方式将这种二进制流传输到另一个网络节点,当其他程序获取这个二进制流,可以恢复成原来的Java对象。序列化是RMI(remote method invoke–远程方法调用)过程的参数和返回值必须实现的机制,而RMI是JavaEE的基础,因而序列化机制是JavaEE平台的基础。

​ 2)序列化的优点:可以将任何实现了Serializable接口的对象转化为字节数据,使其保存和传输时可被还原。

​ 3)如果需要某个类支持序列化机制,需要对象所属类和属性是可以序列化的,为了让某个类是可以序列化的,该类必须实现如下接口,否则会

抛出异常:NotSerializableException

  • ​ Serializable
  • ​ Externalizable

4 自定义类的序列化的要求

​ 1)实现Serializable接口。

​ 否则:NotSerializableException、

​ 2)提供序列化号:private static final long serialVersionUID = -6849794470754667710L

​ 否则:java.io.InvalidClassException。

​ 3)需要保证其内部属性也必须是可序列化的。默认情况下,基本数据类型是可以序列化的。

​ 序列化的类中不可以保存static和transient修饰的属性==>不会将值保存下来。

5 serialVersionUID的解释

​ 1)凡是实现Serializable接口的类都有一个表示序列化版本标识符的静态变量:private static final long serialVersionUID;serialVersionUID用来表明类的不同版本间的兼容性。

​ 简言之,其目的是以序列化对象进行版本控制,有关各版本反序列化时是否兼容。如果类没有显示定义这个静态常量,它的值是Java运行时环境根据类的内部细节自动生成的。若类的实例变量做了修改,serialVersionUID 可能发生变化。故建议显式声明。

​ 2)简单来说,Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的。在进行反序列化时,JVM会把传来的字节流中的serialVersionUID与本地相应实体类的serialVersionUID进行比较,如果相同就认为是一致的,可以进行反序列化,否则就会出现序列化版本不一致的异常。(InvalidCastException)

/**
 * 序列化的过程,将内存中的对象保存到磁盘中。
 */
@Test
public void testObjectStream01(){
    ObjectOutputStream oos =null;
    FileOutputStream fos =null;
    try {
        fos = new FileOutputStream("object.txt");
        oos = new ObjectOutputStream(fos);
        oos.writeObject(new String("我爱北京天安门"));
        //刷新操作
        oos.flush();
        oos.writeObject(new Person("张三",21,new Account(5000)));
        oos.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (oos!=null){
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
/**
 * 反序列化过程:将磁盘中的对象还原为内存中的对象
 */
@Test
public void testObjectStream02(){
    FileInputStream fis=null;
    ObjectInputStream ois=null;
    try {
        fis=new FileInputStream("object.txt");
        ois=new ObjectInputStream(fis);
        Object o = ois.readObject();
        System.out.println(o);
        Object o1 = ois.readObject();
        System.out.println(o1);

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

/**
 * 自定义类序列化
 * 要求:实现接口Serializable
 *
 */
class Person implements Serializable{
    private static final long serialVersionUID = -6849794470754667710L;
    private String name;
    private int age;
    private Account account;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", account=" + account +
                '}';
    }

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

    public Person() {
    }

    public Person(String name, int age, Account account) {
        this.name = name;
        this.age = age;
        this.account = account;
    }
}
class Account implements Serializable{
    private static final long serialVersionUID = -68497944707546677L;
    int money;

    public Account(int money) {
        this.money = money;
    }

    public Account() {
    }

    @Override
    public String toString() {
        return "Account{" +
                "money=" + money +
                '}';
    }
}

2.11 流之随机存取文件流

1 RandomAccessFile类的概述

​ 1)RandomAccessFile 声明在java.io包下,但直接继承于java.lang.Object类。并且它实现了DataInput、DataOutput这两个接口,也就意味着这个类既可以读也可以写。

​ 2)RandomAccessFile 类支持 “随机访问” 的方式,程序可以直接跳到文件的任意地方来 读、写文件

  • 支持只访问文件的部分内容
  • 可以向已存在的文件后追加内容

​ 3)RandomAccessFile 对象包含一个记录指针,用以标示当前读写处的位置。RandomAccessFile类对象可以自由移动记录指针:

  • long getFilePointer():获取文件记录指针的当前位置
  • void seek(long pos):将文件记录指针定位到 pos 位置

2 使用

​ 1)构造器

  • ​ public RandomAccessFile(File file, String mode)

  • ​ public RandomAccessFile(String name, String mode)

    2)创建 RandomAccessFile 类实例需要指定一个 mode 参数,该参数指定 RandomAccessFile 的访问模式:

  • r: 以只读方式打开

  • rw :打开以便读取和写入

  • rwd: 打开以便读取和写入;同步文件内容的更新 —> 多线程等可能用到

  • rws: 打开以便读取和写入; 同步文件内容和元数据的更新

    3) 如果模式为只读r。则不会创建文件,而是会去读取一个已经存在的文件,如果读取的文件不存在则会出现异常。 如果模式为rw读写。如果文件不存在则会去创建文件,如果存在则不会创建。如果当作输出流出现,源文件不存在则进行创建;如果存在,则会从头进行覆盖。

3 使用场景

​ 1)我们可以用RandomAccessFile这个类,来实现一个多线程断点下载的功能,用过下载工具的朋友们都知道,下载前都会建立两个临时文件,一个是与被下载文件大小相同的空文件,另一个是记录文件指针的位置文件,每次暂停的时候,都会保存上一次的指针,然后断点下载的时候,会继续从上一次的地方下载,从而实现断点下载或上传的功能,有兴趣的朋友们可以自己实现下。

@Test
public void testRandomAccessFile1(){
    RandomAccessFile raf1 =null;
    RandomAccessFile raf2 =null;
    try {
        raf1 = new RandomAccessFile(new File("房屋.jpg"),"r");
        raf2 = new RandomAccessFile(new File("房屋3.jpg"),"rw");
        byte[] bytes=new byte[1024];
        int len=0;
        while ((len=raf1.read(bytes))!=-1){
            raf2.write(bytes,0,len);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if (raf1!=null){
            try {
                raf1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (raf2!=null){
            try {
                raf2.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

/**
 * 从头开始如果不存在就是写数据,存在就是覆盖
 */
@Test
public void testRandomAccessFile02(){
    RandomAccessFile raf1 =null;
    try {
        raf1 = new RandomAccessFile(new File("hello.txt"),"rw");
        //hello,world==>xysad,world
        raf1.write("xysad".getBytes());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if (raf1!=null){
            try {
                raf1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

/**
 * 指定的位置开始进行覆盖
 */
@Test
public void testRandomAccessFile03(){
    RandomAccessFile raf1 =null;
    try {
        raf1 = new RandomAccessFile(new File("hello.txt"),"rw");
        raf1.seek(3);
        //xysad,world==>xysxysworld
        raf1.write("xys".getBytes());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if (raf1!=null){
            try {
                raf1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
/**
 * 指定位置插入数据
 */
@Test
public void testRandomAccessFile04(){
    RandomAccessFile raf1 =null;
    try {
        raf1 = new RandomAccessFile(new File("hello.txt"),"rw");
        raf1.seek(6);
        byte[] bytes=new byte[20];
        //保存指针6后面的所有数据到StringBuilder中
        StringBuilder sb=new StringBuilder((int) new File("hello.txt").length());
        int len=0;
        while((len=raf1.read(bytes))!=-1){
            sb.append(new String(bytes,0,len));
        }
        //调回指针
        raf1.seek(6);
        //写入---
        raf1.write("---".getBytes());
        //写入sb中存储的数据:xysxysworld==>xysxys---world
        raf1.write(sb.toString().getBytes());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if (raf1!=null){
            try {
                raf1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

流的小结

  • 流是用来处理数据的。
  • 处理数据时,一定要先明确数据源与数据目的地
  • 数据源可以是文件,可以是键盘。
  • 数据目的地可以是文件、显示器或者其他设备。
  • 而流只是在帮助数据进行传输,并对传输的数据进行处理,比如过滤处理、转换处理等。

拓展:NIO

​ 1 随着 JDK 7 的发布,Java对NIO进行了极大的扩展,增强了对文件处理和文件系统特性的支持,以至于我们称他们为 NIO.2。因为 NIO 提供的一些功能,NIO已经成为文件处理中越来越重要的部分。

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

//在以前IO操作都是这样写的:
import java.io.File;
File file = new File("index.html");

//但在Java7 中,我们可以这样写:
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) : 用于将多个字符串串连成路径
    ));
    //写入sb中存储的数据:xysxysworld==>xysxys—world
    raf1.write(sb.toString().getBytes());
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }finally {
    if (raf1!=null){
    try {
    raf1.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }

流的小结

  • 流是用来处理数据的。
  • 处理数据时,一定要先明确数据源与数据目的地
  • 数据源可以是文件,可以是键盘。
  • 数据目的地可以是文件、显示器或者其他设备。
  • 而流只是在帮助数据进行传输,并对传输的数据进行处理,比如过滤处理、转换处理等。

拓展:NIO

​ 1 随着 JDK 7 的发布,Java对NIO进行了极大的扩展,增强了对文件处理和文件系统特性的支持,以至于我们称他们为 NIO.2。因为 NIO 提供的一些功能,NIO已经成为文件处理中越来越重要的部分。

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

//在以前IO操作都是这样写的:
import java.io.File;
File file = new File("index.html");

//但在Java7 中,我们可以这样写:
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路径
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值