Java学习15-IO流-1

Java学习15-IO流-1

1. File类的使用

1.1 File类介绍

  • File类的一个对象,代表一个文件或一个文件目录(文件夹)
  • File类声明在java.io包下
  • File类中涉及到关于文件或文件目录的创建、删除、重命名、修改时间、文件大小等方法,但是并不涉及写入或读取文件内容的操作。如果需要读取或写入文件内容,必须使用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使用“/”来表示
  • Java程序支持跨平台运行,因此路径分隔符要慎用。
  • 为了解决这个隐患,File类提供了一个常量:
    public static final String separator 根据操作系统,动态的提供分隔符。
File file1 = new File("d:\\atguigu\\info.txt");
File file2 = new File("d:" + File.separator + "atguigu" + File.separator + "info.txt");
File file3 = new File("d:/atguigu");

1.3 File类常用方法

  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数组
  2. File类的重命名功能

    • public boolean renameTo(File dest):把文件重命名为指定的文件路径
  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() :创建文件目录。如果上层文件目录不存在,一并创建

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

  1. File类的删除功能
    1. public boolean delete():删除文件或者文件夹

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

2. IO流原理及流的分类

  • 输入input: :读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中。
  • 输出output: :将程序(内存)数据输出到磁盘、光盘等存储设备中。
  • 在这里插入图片描述

2.1 流的分类

  • 按操作数据单位不同分为:流 字节流(8 bit) ,字符流(16 bit)
  • 按数据流的流向不同分为:输入流、输出流
  • 按流的角色的不同分为:结点流、处理流
(抽象基类) 字节流 字符流
输入流 InputStream Reader
输出流 OutputStream Writer

在这里插入图片描述

  • 节点流:直接从数据源或目的地读写数据
    在这里插入图片描述
  • 处理流:不直接连接到数据源或目的地,而是“连接”在已存在的流(节点流或处理流)之上,通过对数据的处理为程序提供更为强大的读写功能。
    在这里插入图片描述

3. 节点流(或文件流)

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

3.1 FileReader

  • FileReader读入数据的基本操作
public void testFileReader() {
        FileReader fr = null;

        try {
            //1.实例化File类的对象,指明要操作的文件
            File file = new File("hello.txt");//单元测试:相较于当前Module下; main方法测试:相较于当前工程
            //2.提供具体的流
            fr = new FileReader(file);
            //3.数据的读入
            int data = fr.read();//read():返回读入的一个字符。如果达到文件末尾,返回-1
            while (data != -1) {
                System.out.print((char) data);
                data = fr.read();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭流
            try {
                if (fr != null)
                    fr.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
  • FileReader中使用read(char[] cbuf)读入数据
  1. 建立一个流对象,将已存在的一个文件加载进流。
    FileReader fr = new FileReader(new File(“Test.txt”));
  2. 创建一个临时存放数据的数组。
    char[] ch = new char[1024];
  3. 调用流对象的读取方法将流中的数据读入到数组中。
    fr.read(ch);
  4. 关闭资源。
    fr.close();
 /**
     * 对read()操作升级:使用read的重载方法
     */
    @Test
    public void testFileReader1() throws IOException {
        FileReader fileReader = null;
        try {
            //1.File类的实例化
            File file = new File("hello.txt");

            //2.FileReader流的实例化
            fileReader = new FileReader(file);

            //3.读入的操作
            char[] cbuf = new char[1024];
            int len;
            while ((len = fileReader.read(cbuf)) != -1) {//read( char[] cbuf):返回每次读入cbuf数组中字符的个数。如果达到文件末尾,返回-1

                /**
                 * 另一种遍历写法:
                 * Strig str = new String(cbuf, 0, len);
                 * System.out.println(str);
                 */
                for (int i = 0; i < len; i++) {
                    System.out.print(cbuf[i]);
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileReader != null) ;
            //4.资源的关闭
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

3.2 FileWriter

  • FileWriter:从内存中写出数据到硬盘的文件里
  • File对应的磁盘中的文件如果不存在,在输出的过程中,会自动创建此文件
  • File对应的磁盘中的文件如果存在:
    • 如果流使用的构造器是FileWriter(file, false) / FileWriter(file):对原有文件的覆盖
    • 如果流使用的构造器是FileWriter(file, true):不会对原有文件覆盖,而是在原有文件基础上追加内容
  1. 创建流对象,建立数据存放文件
    FileWriter fw = new FileWriter(new File(“Test.txt”));
  2. 调用流对象的写入方法,将数据写入流
    fw.write(“atguigu-songhongkang”);
  3. 关闭流资源,并将流中的数据清空到文件中。
    fw.close();
 /**
     * 从内存中写出数据到硬盘的文件里
     */
    @Test
    public void testFileWriter() {
        FileWriter fileWriter = null;
        try {
            //1.提供File类的对象,指明写出到的文件
            File file = new File("hello1.txt");

            //2.提供FileWriter的对象,用于数据的写出
            fileWriter = new FileWriter(file);

            //3.写出的操作
            fileWriter.write("helloworld");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.流的关闭
            if (fileWriter!=null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

       
    }

3.3使用FileReader和FileWriter实现文本文件的复制

 @Test
    public void testFileReaderFileWriter() {
        FileReader fileReader = null;
        FileWriter fileWriter = null;
        try {
            //1.创建File类的对象,指明读入和写出的文件
            File srcFile = new File("hello.txt");
            File destFile = new File("hello2.txt");

            //2.创建输入流和输出流的对象
            fileReader = new FileReader(srcFile);
            fileWriter = new FileWriter(destFile);

            //3.数据的读入和写出操作
            char[] cbuf = new char[1024];
            int len;
            while ((len = fileReader.read(cbuf)) != -1) {
                fileWriter.write(cbuf, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //4.关闭流资源
                if (fileWriter != null) ;
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

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


        }

    }

3.4 FileInputStream和FileoutputStream

/**
 * 实现对图片的复制操作
 */

public class FileInputOutputStreamTest {

    @Test
    public void testFileInputStream() throws FileNotFoundException {

        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            File srcFile = new File("紫霞.jpg");
            File destFile = new File("紫霞2.jpg");

            fileInputStream = new FileInputStream(srcFile);
            fileOutputStream = new FileOutputStream(destFile);

            byte[] buffer = new byte[1024];
            int len;
            while ((len = fileInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileInputStream != null) ;
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fileOutputStream != null) ;
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

4. 缓冲流(简便写法看4.2)

  • 提高流的读取、写入速度
  • 缓冲流要“套接”在相应的节点流之上

4.1 BufferedInputStream 和 BufferedOutputStream

 /*
     * 实现非文本文件的复制
     * */
    @Test
    public void BufferedStreamTest() throws FileNotFoundException {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //1.造文件
            File srcFile = new File("紫霞.jpg");
            File destFile = new File("紫霞3.jpg");

            //2.造流
            //2.1造结点流
            FileInputStream fis = new FileInputStream(srcFile);
            FileOutputStream fos = new FileOutputStream(destFile);
            //2.2造缓冲流
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);

            //3.复制的细节:读取、写入
            byte[] buffer = new byte[1024];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.资源关闭
            //要求:先关闭外层的流,再关闭内层的流
            try {
                if (bos != null) ;
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (bis != null) ;
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //说明:关闭外层流的同时,内层流也会自动的进行关闭,关于内层流的关闭我们可以省略
//        fos.close();
//        fis.close();

        }


    }

4.2 BufferedReader 和 BufferedWriter

多了一个readLine()方法

/**
     * 实现文本文件的复制
     */
    @Test
    public void testBufferedReaderWriter() {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            //1.造文件、造流简便写法
            br = new BufferedReader(new FileReader(new File("hello.txt")));
            bw = new BufferedWriter(new FileWriter(new File("hello3.txt")));

            //2.读写操作方式一
//            char[] cbuf = new char[1024];
//            int len;
//            while ((len = br.read(cbuf)) != -1) {
//                bw.write(cbuf, 0, len);
//
//            }

            //2.读写操作方式二
            String data;
            while ((data = br.readLine()) != null) {
                //换行方法一:
                bw.write(data + "\n");//data中不包含换行符
                //换行方法二:
                bw.write(data);//data中不包含换行符
                bw.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            //3.关闭流资源
            try {
                if (br != null)
                    br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (bw != null)
                    bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

4.3 小练习-图片的加密解密

public class PicTest {
    //图片的加密
    @Test
    public void test1() {

        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("紫霞.jpg");
            fos = new FileOutputStream("紫霞secret.jpg");

            byte[] buffer = new byte[20];
            int len;
            while ((len = fis.read(buffer)) != -1) {
                //字节数组修改
                for (int i = 0; i < len; i++) {
                    buffer[i] = (byte) (buffer[i] ^ 5);
                }
                fos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }


    //图片的解密
    @Test
    public void test2() {

        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("紫霞secret.jpg");
            fos = new FileOutputStream("紫霞4.jpg");

            byte[] buffer = new byte[20];
            int len;
            while ((len = fis.read(buffer)) != -1) {
                //字节数组修改
                for (int i = 0; i < len; i++) {
                    buffer[i] = (byte) (buffer[i] ^ 5);
                }
                fos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}

5. 转换流 InputStreamReader和 OutoutStreamWriter

提供字节流与字符流之间的转换

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

在这里插入图片描述

/**
 * 转换流的使用
 */

public class InputStreamReaderTest {

    @Test
    public void test1() {
        InputStreamReader isr = null;
        OutputStreamWriter osw = null;
        try {
            isr = new InputStreamReader(new FileInputStream(new File("hello.txt")), "utf-8");
            osw = new OutputStreamWriter(new FileOutputStream(new File("hello4.txt")), "gbk");

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

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

    }

6. 标准输入、输出流

  • System.in和System.out分别代表了系统标准的输入和输出设备
  • System.in的类型是InputStream
  • System.out的类型是PrintStream,其是OutputStream的子类FilterOutputStream 的子类
  • 通过System类的setIn(InputStream in),setOut(PrintStream out)方法重新指定输入和输出的流
 /**
     * 从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续
     * 进行输入操作,直至当输入“e”或者“exit”时,退出程序。
     * <p>
     * 方法一:使用Scanner实现,调用next()返回一个字符串
     * 方法二:使用System.in实现,System.in -> 转换流 ->BufferedReader的readerline()
     */
    @Test
    public static void test3() {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(System.in));

            while (true) {
                System.out.println("请输入字符串:");
                String data = br.readLine();
                if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)) {
                    System.out.println("程序结束");
                    break;
                }
                String uperCase = data.toUpperCase();
                System.out.println(uperCase);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)
                    br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

使用单元测试无法在控制台输入,所以要想从控制台输入数据就要写main函数

猜你喜欢

转载自blog.csdn.net/qq_42145862/article/details/89367084