java基础之IO

1 文件的基本概念

1.1 路径

在IO中,常用到路径的概念,有以下几种路径:

  1. c:\temp\file.txt
  2. .\file.txt
  3. c:\temp\MyApp\bin…\file.txt

第一类,属于路径,绝对路径,规范路径 (CanonicalPath)
第二类,属于路径,相对路径(Path)
第三类,属于路径,绝对路径 (AbsolutePath)

下面的示例代码清楚的展示了java中这三类路径的区别

/* -------这是一个相对路径的代码------- */
File file = new File("..\\..\\..\\Test.txt");
System.out.println("file.getAbsolutePath()  -> " + file.getAbsolutePath());
System.out.println("file.getCanonicalPath() -> " + file.getCanonicalPath());
System.out.println("file.getPath()          -> " + file.getPath());

/* -------输出------- */
file.getAbsolutePath()  -> E:\commonWorkspace\IdeaPluginDevGuide\DevGuideVirtualFileSystem\..\..\..\Test.txt
file.getCanonicalPath() -> E:\Test.txt
file.getPath()          -> ..\..\..\Test.txt

可以看到Path就是创建File时使用的相对目录。AbsolutePath是绝对路径,其中可能包含…/ or ./这类切换目录的相对目录,而CanonicalPath就是规范化后的绝对路径。leetcode有一题就是规范化路径的,71-简化路径,链接如下

https://leetcode-cn.com/problems/simplify-path/

1.2 File类

该类主要用来抽象文件系统,并对其进行操作,类似于Linux的ls命令。

为了代码可以在多种操作系统上创建文件对象,目录中的分隔符建议采用File.separator。

String fileName = "test.txt";
        System.out.println("File.separator:" + File.separator);
        File testFile = new File("D:" + File.separator + "filepath" + File.separator + "test" + File.separator + fileName);
        File fileParent = testFile.getParentFile();//返回的是File类型,可以调用exsit()等方法
        String fileParentPath = testFile.getParent();//返回的是String类型
        System.out.println("fileParent:" + fileParent);
        System.out.println("fileParentPath:" + fileParentPath);
        if (!fileParent.exists()) {
            fileParent.mkdirs();// 能创建多级目录
        }
        if (!testFile.exists()){
            try {
                testFile.createNewFile();//有路径才能创建文件
            }catch (IOException e){
                e.printStackTrace();
            }
        }

        System.out.println(testFile);

        String path = testFile.getPath();
        String absolutePath = testFile.getAbsolutePath();//得到文件/文件夹的绝对路径
        String getFileName = testFile.getName();//得到文件/文件夹的名字
        System.out.println("path:"+path);
        System.out.println("absolutePath:"+absolutePath);

2 IO

2.1 IO流的分类

按处理数据单位分

字节流:每次读取(写出)一个字节,当传输的资源文件有中文时,就会出现乱码,

字符流:每次读取(写出)两个字节,有中文时,使用该流就可以正确传输显示中文。

常用IO流的树状图
在这里插入图片描述

2.2 基本字节流与字符流

字节流

FileInputStream和FileOutputStream

它们主要有两种常用的构造方法

FileInputStream(file: File)
FileInputStream(fileName: String)

下面是一个从文件中读取数据的例子

public static void readFileOneByOne() {

  try {
    final File file = new File("test.txt");
    final FileInputStream is = new FileInputStream(file);
    int data = -1;

    //逐个字节读取处理
    while((data = is.read()) != -1) {
      //处理它
      System.out.println((char) data);
    }

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

一次读取一个字节的效率太低了,FileInputStream提供了读取字节数组的方法,这就是字节流的批处理

public static void readFileOneByOne() {
  try {
    final File file = new File("test.txt");
    final FileInputStream is = new FileInputStream(file);
    int data = -1;
    //缓存容器
    byte[] datas = new byte[1024];
    //批量字节读取处理
    while((data = is.read(datas)) != -1) {
      //处理它
      System.out.println(new String(datas, 0, 1024));
    }
  } catch(IOException ioex) {
    ioex.printStackTrace();
  }
}

FileOutputStream

    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream("tmp.txt");
        FileOutputStream out = new FileOutputStream("output.txt");
        byte[] content = new byte[1024];
        while(in.read(content)!=-1){
            out.write(content);
        }
    }

字符流

OutputStreamWriter 和 InputStreamReader

字符流 OutputStreamWriter 和 InputStreamReader是字符流和字节流之间的桥梁,因为这两个字符IO类的构造方法参数是字节流对象。

public static void main(String[] args) throws IOException {
 OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("E:\\utf_8.txt"));          osw.write("你好");
  osw.flush();
  osw.close();
 }

2.3 过滤字节流

2.3.1 FilterInputStream与FilterOutputStream

过滤流是为某种目的过滤字节的数据流。基本字节流只能读取字节,如果想要读取整数值、双精度值或字符串,就需要一个过滤器类来包装字节输入流。

我觉得 FilterInputStream 的目的就是通过包装模式扩展基本字节流的功能。下面来介绍几种常用的包装IO类。

2.3.2 DataInputStream与DataOutputStream

DataInputStream 继承自 FilterInputStream。从数据流读取字节,并将它们转换为合适的基本数值类型或字符串。它的构造器需要FileInputStream作为参数。因为该类可读取数值,因此不能用-1判断文件末尾,可以捕获EOFException判断文件是否到末尾了。

public static void main(String[] args) throws IOException {
        DataOutputStream dataOutputStream = new DataOutputStream(new                                 FileOutputStream("dataIn.dat"));
        dataOutputStream.writeUTF("dandan");
        dataOutputStream.writeDouble(1.68);
        dataOutputStream.writeInt(100);

        //FileOutputStream fileOutputStream = new FileOutputStream("dataIn.txt");
        //fileOutputStream.write("wendy");//FileOutputStream不能写字符串。它是字节流,写字符串            //到文件建议用字符流。
        //fileOutputStream.write(1000);

        System.out.println("测试FileInputStream----------------------");
        FileInputStream in = new FileInputStream("dataIn.txt");
        while(in.read()!=-1){
            System.out.println(in.read());
        }

        System.out.println("测试DataInputStream-------------------------");
        try( DataInputStream dataInputStream = new DataInputStream(new FileInputStream("dataIn.dat"));){
            while(true){
                System.out.println(dataInputStream.readUTF());
                System.out.println(dataInputStream.readDouble());
                System.out.println(dataInputStream.readInt());
            }
        }catch (EOFException e){
            System.out.println("All data were read");
        }
    }

如果用FileInputStream,读取文件,输出结果如下,信息显然是错的。

测试FileInputStream----------------------
6
97
100
110
250
71
20
225
0
100

2.3.3 BufferedInputStream与BufferedOutputStream

BufferedInputStream 继承自 FilterInputStream。这两个类可以通过减少磁盘读写次数来提高输入和输出的速度(CPU访问速度远高于磁盘访问)。它们没有包含新的方法,所有方法都是从InputStream和OutputStream中继承的。

使用缓冲流时需要指定一个缓冲区。

 public static void main(String[] args) {
        try {
            File file = new File("buffered.txt");
            if (!file.exists()) {
                file.createNewFile();
            }

            FileInputStream fis = new FileInputStream("buffered.txt");
            BufferedInputStream bis = new BufferedInputStream(fis);
            String content = "";
            //自己定义一个缓冲区
            byte[] buffer = new byte[1024];
            int flag = 0;
            while ((flag = bis.read(buffer)) != -1) {
                content += new String(buffer, 0, flag); // byte数组构造字符串
            }
            System.out.println(content);
            //关闭的时候只需要关闭最外层的流就行了
            bis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

2.3.5 ZipOutputStream

zipIO类也是继承自 FilterInputStream或FilterOutputStream。

zip文件中的一个个文件对象是ZipEntry。压缩文件夹时,对于文件夹中的每个文件,要新建一个与其对应的ZipEntry放入ZipOutputStream,并把文件内容写入到对应的 ZipEntry中。

    static public void zipMultiFile() throws IOException {
        // 要被压缩的文件夹
        File file = new File("d:" + File.separator + "router");
        File zipFile = new File("d:" + File.separator + "router.zip");
        InputStream input = null;
        ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
        zipOut.setComment("hello");
        if (file.isDirectory()) {
            File[] files = file.listFiles(); // 获取被压缩文件夹中的文件数组
            for (int i = 0; i < files.length; ++i) {
                input = new FileInputStream(files[i]);// 注意文件夹中不能再有文件夹,除非递归
                // 创建ZipEntry对象,加入到ZipOutputStream中
                zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator +                           files[i].getName()));

                // 把文件内容写入到该ZipEntry中
                int temp = 0;
                while ((temp = input.read()) != -1) {
                    zipOut.write(temp);
                }
                input.close();
            }
        }
        zipOut.close();
    }

    static public void unzipMultiFile() throws IOException {
        File file = new File("d:" + File.separator + "zipFile.zip");
        File outFile = null;
        ZipFile zipFile = new ZipFile(file);
        ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
        ZipEntry entry = null;
        InputStream input = null;
        OutputStream output = null;
        while ((entry = zipInput.getNextEntry()) != null) {
            //entry.getName()获得压缩包内的文件及文件路径(aaaa/bb.txt)
            System.out.println("解压缩" + entry.getName() + "文件");
            outFile = new File("d:" + File.separator + entry.getName());
            if (!outFile.getParentFile().exists()) {
                outFile.getParentFile().mkdir();
            }
            if (!outFile.exists()) {
                outFile.createNewFile();
            }
            input = zipFile.getInputStream(entry);
            output = new FileOutputStream(outFile);
            int temp = 0;
            while ((temp = input.read()) != -1) {
                output.write(temp);
            }
            input.close();
            output.close();
        }
    }

大家自然想到,既然能压缩,自然能解压缩,在谈解压缩之前,我们会用到一个ZipFile类,先给一个这个例子吧。java中的每一个压缩文件都是可以使用ZipFile来进行表示的

2.4 对象IO

ObjectInputStream与ObjectInputStream类可以实现基本数据类型和String类型的输入与输出,所以完全可以用ObjectInputStream与ObjectInputStream代替DataInputStream与DataOutputStream。

除此之外,它们还可以实现对象的输入和输出。注意并非每一个对象都可以写入到输出流,可以写入到输出流的对象成为可序列化的,可序列化对象的类必须实现Serializble接口。

        try (ObjectOutputStream objectOutputStream =
                        new ObjectOutputStream(new FileOutputStream("objectIn.txt"))
        ) {
            objectOutputStream.writeUTF("john");
            objectOutputStream.writeDouble(85.5);
            //不序列化对象,无法写入文件
            objectOutputStream.writeObject(new Student("wendy", 25));
        }

        try (ObjectInputStream objectInputStream =
                        new ObjectInputStream(new FileInputStream("objectIn.txt"))
        ) {
            String name = objectInputStream.readUTF();
            double age = objectInputStream.readDouble();
            Student student = (Student) (objectInputStream.readObject());//必须抛出                          ClassNotFoundException
            System.out.println(name + " " + age + " " + student);
        }

3 IO的使用

3.1 如何复制一个文件?

public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream("tmp.txt");
        FileOutputStream out = new FileOutputStream("output.txt");
        byte[] content = new byte[1024];
        while(in.read(content)!=-1){
            out.write(content);
        }
    }

3.2 如何下载一个文件?

通过浏览器下载服务器上的文件是web程序常见的功能,下面是springboot框架下利用IO下载文件的方法。

@RequestMapping(value = "/oneFile", method = RequestMethod.GET)
public Object downloadFile() {
    ResponseEntity<byte[]> responseEntity = null;
    try {
        File file = new File("tmp.txt");
        InputStream in = new FileInputStream(file);
        byte[] body = new byte[in.available()]; // body不能为空,不然空指针异常
        in.read(body);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment;filename=11.txt");
        HttpStatus statusCode = HttpStatus.OK;
        responseEntity = new ResponseEntity<byte[]>(body, headers, statusCode);
        in.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return responseEntity;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值