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

java基础之IO

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

java基础之IO

1 文件的基本概念 1.1 路径

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

  1. c:tempfile.txt
  2. .file.txt
  3. c:tempMyAppbin…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:commonWorkspaceIdeaPluginDevGuideDevGuideVirtualFileSystem......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流的树状图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1DIDbUSi-1633521543054)(C:UserswendymaAppDataRoamingTyporatypora-user-imagesimage-20210925205522480.png)]

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();
  }
}

字符流

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

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

2.3.2 DataInputStream与DataOutputStream

DataInputStream从数据流读取字节,并将它们转换为合适的基本数值类型或字符串。它的构造器需要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.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

这两个类可以通过减少磁盘读写次数来提高输入和输出的速度(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.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);
        }

2.3.5 zip IO

zip文件中的一个个文件对象是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]);
                zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName()));
                int temp = 0;
                while ((temp = input.read()) != -1) {
                    zipOut.write(temp);
                }
                input.close();
            }
        }
        zipOut.close();
    }

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

2.3.6 如何复制一个文件?

2.3.7 如何下载一个文件?

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

@RequestMapping(value = "/oneFile", method = RequestMethod.GET)
public Object downloadFile() {
    ResponseEntity 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(body, headers, statusCode);
        in.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return responseEntity;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/301784.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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