栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

IO流(笔记)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

IO流(笔记)

文章目录
  • 十三.IO流
    • (1)File类
      • 1.创建File实例
      • 2.路径分隔符:
    • (2)File常用方法
      • File的获取功能:
      • File的判断功能:
      • File创建方法(磁盘下)
      • File类的删除功能:
    • (3)流的分类
    • (4)流的体系结构
    • (5)FileReader基本操作
    • (6)read()方法的重载
    • (7)write()
    • (8)write和read的结合
    • (9)图片的复制操作
    • (10)缓冲流的使用
    • (11)转换流的使用
    • (12)对象流
    • (13)自定义类实现序列化和反序列化

十三.IO流 (1)File类

1.File类的一个对象,代表一个文件或一个文件目录(俗称:文件夹)

2.File类声明在java.io包下

1.创建File实例

相对路径:相较于某个路径下,指明的路径。
绝对路径:向含盘符在内的文件或文件目录的路径

//构造器一:
File file1 = new File("Hello.txt");//相对路径
File file2 = new File("E:\java\StudyProject\src\com\ccw\Exer30\hi.txt");//绝对路径
//构造器二:
File file3 = new File("E:\java\StudyProject\","Exer30");//文件目录,并非文件
//构造器三:
File file4 = new File(file3,"hi.txt");
2.路径分隔符:

Unix:/

Windows:

(2)File常用方法 File的获取功能:
  1. public String getAbsolutePath():获取绝对路径
  2. public String getPath():获取路径
  3. public String getName():获取名称
  4. public String getParent():获取上层文件目录路径。若无,返回null
  5. public long length():获取文件长度(即:字节数)。不能获取目录的长度。
  6. public long lastModified() :获取最后一次的修改时间,毫秒值
  7. public String[] list():获取指定目录下的所有文件或者文件目录的名称数组
  8. public File[] listFiles():获取指定目录下的所有文件或者文件目录的File数组

renameTo:把文件重命名为指定的文件路径

File file1 = new file("hello.txt")
File file2 = new file("D:\hi.txt")

比如file1.renameTo(file2)为例

要想保证重命名成功:1.需要file1在硬盘中是存在的;2.file2在硬盘中不存在

File的判断功能:
  1. public boolean isDirectory():判断是否是文件目录
  2. public boolean isFile():判断是否是文件
  3. public boolean exists():判断是否存在
  4. public boolean canRead():判断是否可读
  5. public boolean canWrite():判断是否可写
  6. public boolean isHidden() :判断是否隐藏
File创建方法(磁盘下)
  1. public boolean createNewFile():创建文件。若文件存在,则不创建,返回false

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

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

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

File类的删除功能:

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

删除注意事项:

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

(3)流的分类

操作数据单位不同:字节流(8bit),字符流(16bit)

数据流的流向不同:输入流,输出流

流的角色的不同:节点流,处理流

(抽象基类)字节流字符流
输入流InputStreamReader
输出流OutputStreamWriter

(4)流的体系结构

主要结构:

抽象基类节点流(或文件流)缓冲流(处理流的一种)
InputStreamFileInputStreamBufferedInputStream
OutputStreamFileOutputStreamBufferedOutputStream
ReaderFileReaderBufferedReader
WriterFileWriterBufferedWriter

(5)FileReader基本操作

异常处理:为了保证流资源一定可以执行关闭操作。需要使用try-catch-finally处理

public class FileReadWriteTest {
    @Test
    public void test1()  {
        FileReader fr = null;
        try {
            //1.实例化File类的对象,指明要操作的文件
            File file = new File("E:\java\StudyProject\src\com\ccw\Exer30\hello.txt");
            //2.提供具体的流
            fr = new FileReader(file);

            //3.数据的读入
            //read():返回读入的一个字符,如果到达文件末尾,返回-1
//        int data = fr.read();
//        while(data != -1){
//            System.out.print((char) data);
//            //继续往后读,相当于迭代器next
//            data = fr.read();
//        }
//        便捷写法
            int data;
            while((data = fr.read()) != -1){
                System.out.print((char)data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.流的关闭
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
(6)read()方法的重载
//对read()操作升级,使用read()重载方法
    @Test
    public void test2()  {
        FileReader fr = null;
        try {
            File file = new File("E:\java\StudyProject\src\com\ccw\Exer30\hello.txt");
            fr = new FileReader(file);
            //read(char[] cbuf):返回每次读入cbuf数组中的字符个数。如果达到文件末尾,返回-1
            char[] cbuf = new char[5];
            int len;
            while((len = fr.read(cbuf)) != -1){//len表示数组读进去几个
                //错误的遍历
//                for(char ch : cbuf){
//                    System.out.print(ch);
//                }
                for(int i = 0;i < len;i++){
                    System.out.print(cbuf[i]);
                }
//                或
                String str = new String(cbuf,0,len);//从cbuf数组下标0位置开始取,每次取len个
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
    }

}
(7)write()

从内存中写出数据到硬盘的文件里:

输出操作,对应的File可以不存在,如果不存在,在执行过程中,会自动创建;

  • 如果调用的构造器是FileWriter(file)或FileWriter(file,false)则会覆盖之前的内容

  • 如果调用的是FileWriter(file,true)则会接着追加内容

    以下是代码举例:

    public void test3() {
        FileWriter fw = null;//true表示接着写,false表示覆盖
        try {
            File file = new File("E:\java\StudyProject\src\com\ccw\Exer30\hello1.txt");
            fw = new FileWriter(file,true);
            //写出操作
            fw.write("I hava a lovely girlfriendn");
            fw.write("你是我的毕生梦想");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
    }
    
(8)write和read的结合
 
@Test
    public void test4()  {
    FileReader fr = null;
    FileWriter fw = null;
    try {
        File file1 = new File("E:\java\StudyProject\src\com\ccw\Exer30\hello.txt");
        File file2 = new File("E:\java\StudyProject\src\com\ccw\Exer30\hello1.txt");
        fr = new FileReader(file1);
        fw = new FileWriter(file2);

        int len;
        char[] cbuf = new char[5];
        while((len = fr.read(cbuf)) != -1){
            fw.write(cbuf,0,len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if(fr != null){
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(fw != null){
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

   }

对于文本文件,使用字符流来处理(.txt .java .cpp)
对于非文本文件,使用字节流来处理(.jpg .mp3 .avi .doc .ppt

(9)图片的复制操作
@Test
public void test2()  {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        File file1 = new File("E:\java\StudyProject\src\com\ccw\Exer31\QQ截图20210811113710.jpg");
        File file2 = new File("E:\java\StudyProject\src\com\ccw\Exer30\QQ截图20210811113710.jpg");

        fis = new FileInputStream(file1);
        fos = new FileOutputStream(file2);
        int len;
        byte[] buffer = new byte[5];
        while((len = fis.read(buffer)) != -1){
            fos.write(buffer,0,len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(fis != null){
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

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

        }
    }
}
(10)缓冲流的使用

作用:提高流的读取和写入速度

原因:内部提供了一个缓冲区

    public void BufferStreamTest() {
        BufferedInputStream bis = null;//将节点流作为参数传入
        BufferedOutputStream bos = null;
        try {
            //1.造文件
            File srcFile = new File("E:\java\StudyProject\src\com\ccw\Exer31\QQ截图20210811113710.jpg");
            File destFile = new File("E:\java\StudyProject\src\com\ccw\Exer31\QQ截图202108111137101.jpg");
            //2.造流
            //造节点流
            FileInputStream fis = new FileInputStream(srcFile);
            FileOutputStream fos = new FileOutputStream(destFile);

            //造缓冲流(加快存取速度)
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);

            //3.读取操作
            byte[] buffer  = new byte[10];
            int len;
            while ((len = bis.read(buffer)) != -1){
                bos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流
            if(bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

        //先关外部的流,再关内部(关闭外部,内存也自动关闭,可省略)
//        fis.close();
//        fos.close();

        
    }
(11)转换流的使用

1.转换流也属于字符流

InputStreamReader:将一个字节的输入流转换为字符的输入流

OutputStreamwriter:将一个字符的输出流转换为字节的输出流

2.作用:提供字节流与字符流之间的转换

图解如下:

public void test1() throws IOException {
//        File file = new File("E:\java\StudyProject\src\com\ccw\Exer31\hello.txt");
//        FileInputStream fis = new FileInputStream(file);

        FileInputStream fis = new FileInputStream("E:\java\StudyProject\src\com\ccw\Exer31\hello.txt");
        //参数2指明了字符集,具体使用哪个字符集,取决于文件保存时是使用的何种字符集
        InputStreamReader isr = new InputStreamReader(fis,"UTF-8");//已经转换为字符了

        //使用字符流的方式来输出到控制台
        int len;
        char[] cbuf = new char[4];
        while ((len = isr.read(cbuf)) != -1){
            String str = new String(cbuf,0,len);
            System.out.print(str);
        }

        isr.close();

    }

    
    @Test
    public void test2() throws IOException {

        FileInputStream fis = new FileInputStream("E:\java\StudyProject\src\com\ccw\Exer31\hello1.txt");
        FileOutputStream fos = new FileOutputStream("E:\java\StudyProject\src\com\ccw\Exer31\hello.txt");


       InputStreamReader isr = new InputStreamReader(fis,"utf-8");
       OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");

       char[] cbuf = new char[4];
       int len;
       while((len = isr.read(cbuf)) != -1){
           osw.write(cbuf,0,len);
       }

       isr.close();
       osw.close();


    }
}
(12)对象流

1.概述:用于存储和读取基本数据类型数据或对象的处理流。它的强大之处就是可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来。

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

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

ObjectOutputStream和1ObjectInputStream不能序列化static和transient修饰的成员变量

@Test
public void test1(){
    ObjectOutputStream oos = null;
    try {
        //1.造流造文件
        oos = new ObjectOutputStream(new FileOutputStream("E:\java\StudyProject\src\com\ccw\Exer32\hello.dat"));
        //2.写入数据
        oos.writeObject(new String("芜湖起飞"));
        oos.flush();//刷新操作
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(oos != null){
            //3.关闭流
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
  

  @Test
  public void test2()  {
      ObjectInputStream ois = null ;
      try {
          ois = new ObjectInputStream(new FileInputStream("E:\java\StudyProject\src\com\ccw\Exer32\hello.dat"));
          Object obj = ois.readObject();
          String str = (String) obj;
          System.out.println(str);
      } catch (IOException e) {
          e.printStackTrace();
      } catch (ClassNotFoundException e) {
          e.printStackTrace();
      } finally {
          if(ois != null){
              try {
                  ois.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }



  }

对象序列化机制允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。

当其它程序获取了这种二进制流,就可以恢复成原来的Java对象

(13)自定义类实现序列化和反序列化

如果需要让某个对象支持序列化机制,则必须让对象所属的类及其属性是可序列化的,为了让某个类是可序列化的,该类必须实现两个接口之一(serializable或Externalizable)。否则.会抛出NotSerializableExceotion异常

也需要当前类提供一个全局常量: serialVersionUTD

除了当前类需要实现Serializable接口之外,还必须保证其内部所有属性也必须是可序列化的。(默认情况下,基本数据类型可序列化)

=

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/856999.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号