io_Nio_netty核心知识点和面试笔记

 二:File类

java.io.File类:文件和目录路径名的抽象表示形式,与平台无关。File 能新建、删除、重命名文件和目录,但 File 不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入/输出流。File对象可以作为参数传递给流的构造函数。

File类的常见构造方法:
public File(String pathname)
pathname为路径创建File对象,可以是绝对路径或者相对路径,如果pathname是相对路径,则默认的当前路径在系统属性user.dir中存储(可以通过system.getproperty("user.dir")查看)
public File(String parent,String child)
parent为父路径,child为子路径创建File对象

io_Nio_netty核心知识点和面试笔记

字符流以字符为单位,字节流以byte为单位

 io_Nio_netty核心知识点和面试笔记

 io_Nio_netty核心知识点和面试笔记

 io_Nio_netty核心知识点和面试笔记

 因此BufferedReader和BufferedWriter可以包装在FileReader和FileWriter上加快文件读写速度。

io_Nio_netty核心知识点和面试笔记

 3.1 输入输出流的基类InputStream和Reader,OutputStram和Writer

io_Nio_netty核心知识点和面试笔记

 io_Nio_netty核心知识点和面试笔记

package IO;

import org.junit.Test;

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

public class TestInputOutput {
    @Test
    public void test1() {//读取文件,并把结果输出到控制台上面
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(new File("src/IO/config.txt"));
            byte[] b = new byte[5];
            int len;
            while ((len = fis.read(b)) != -1) {
                System.out.print(new String(b, 0, len));
            }

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

    @Test
    public void test2() {
        // 1.创建一个File对象,表明要写入的文件位置。
        // 输出的物理文件可以不存在,当执行过程中,若不存在,会自动的创建。若存在,会将原有的文件覆盖
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(new File("src/IO/b.txt"));
            fos.write("你好,我在发消息".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Test
    public void test3(){// 从硬盘读取一个文件,并写入到另一个位置。(相当于文件的复制)
        FileInputStream fis = null;
        FileOutputStream fos = null;
        long start = System.currentTimeMillis();
        try {
            fis = new FileInputStream(new File("a.jpg"));
            fos = new FileOutputStream(new File("a1.jpg"));
            byte[] b = new byte[1000];//会发现数组的大小影响文件写出的效率,文件太小多次写入浪费时间,太大则浪费空间。
            int len;
            while ((len = fis.read(b))!=-1){
                fos.write(b,0,len);//注意使用len而不是b.length的原因是防止在后一次循环若数组为读满导致多余的数据被写入的错误
            }

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

        }
        long end = System.currentTimeMillis();
        System.out.println("时间共花了"+(end-start));
    }
}
View Code

相关文章:

  • 2021-12-10
  • 2021-08-08
  • 2021-09-11
  • 2021-11-04
  • 2022-01-11
  • 2021-12-27
  • 2022-01-14
  • 2021-10-09
猜你喜欢
  • 2021-08-31
  • 2021-09-21
  • 2021-10-08
  • 2021-11-25
  • 2021-09-29
  • 2022-02-08
  • 2022-02-12
相关资源
相似解决方案