27. Files 的常用方法都有哪些?

Java NIO(New IO)中的 Files 类提供了一系列静态方法来处理文件和目录。这些方法大大简化了对文件的操作,下面列举一些常用的方法:

  1. exists(Path path, LinkOption... options): 检查文件或目录是否存在。
  2. createFile(Path path, FileAttribute<?>... attrs): 创建一个新的空文件。
  3. createDirectory(Path path, FileAttribute<?>... attrs): 创建一个新的目录。
  4. delete(Path path): 删除文件或目录。
  5. copy(Path source, Path target, CopyOption... options): 将文件从一个路径复制到另一个路径。
  6. move(Path source, Path target, CopyOption... options): 将文件从一个路径移动到另一个路径。
  7. size(Path path): 返回文件的大小。
  8. readAllLines(Path path, Charset cs): 读取文件的所有行到一个列表中。
  9. write(Path path, byte[] bytes, OpenOption... options): 将字节序列写入文件。
  10. writeLines(Path path, Iterable<? extends CharSequence> lines, Charset cs, OpenOption... options): 将一系列行写入文件。
  11. setAttribute(Path path, String attribute, Object value, LinkOption... options): 设置文件的属性。
  12. readAttributes(Path path, Class<T> type, LinkOption... options): 读取文件的属性。
  13. walkFileTree(Path start, FileVisitor<? super Path> visitor): 遍历文件树。

以下是这些方法的一些简化的代码示例:

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
import java.util.List;

public class FilesExample {
    public static void main(String[] args) {
        Path path = Paths.get("example.txt");

        // 检查文件是否存在
        boolean exists = Files.exists(path);

        // 创建新文件
        try {
            Files.createFile(path);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 读取所有行
        try {
            List<String> lines = Files.readAllLines(path, java.nio.charset.StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 写入字节
        byte[] bytes = "Hello, world!".getBytes();
        try {
            Files.write(path, bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

注意:这些代码仅作为示例,不包括完整的错误处理和资源管理,实际使用时需要考虑异常处理和资源关闭等问题。

28. Java 容器都有哪些?

Java 容器分为两大类:集合框架(Collection Framework)和映射框架(Map Framework)。

集合框架主要包括以下接口和类:

  1. 接口:
    • Collection: 所有集合类型的根接口,提供了集合通用的方法。
    • List: 继承自 Collection,允许重复元素,维护元素插入顺序。
    • Set: 继承自 Collection,不允许重复元素。
    • Queue: 继承自 Collection,主要用于存储待处理的元素序列,按照特定的顺序处理它们。
    • Deque: 继承自 Queue,代表双端队列,可以同时从两端添加或移除元素。
  2. 实现类:
    • ArrayList: 可调整大小的数组实现,支持随机访问。
    • LinkedList: 双向链表实现,支持快速插入和删除。
    • Vector: 和 ArrayList 类似,但是是同步的。
    • Stack: 继承自 Vector,表示栈。
    • HashSet: 基于哈希表实现,不允许重复元素。
    • LinkedHashSet: 继承自 HashSet,维护了元素的插入顺序。
    • TreeSet: 基于红黑树实现,元素处于排序状态。
    • PriorityQueue: 基于优先级堆实现的无界优先级队列。
    • ArrayDeque: 基于动态数组的双端队列实现。

映射框架主要包括以下接口和类:

  1. 接口:
    • Map: 是所有映射类型的根接口,提供映射通用的方法。
    • SortedMap: 继承自 Map,元素按键排序。
  2. 实现类:
    • HashMap: 基于哈希表实现,键值对存储,允许使用 null 键和 null 值。
    • LinkedHashMap: 继承自 HashMap,维护了键值对的插入顺序。
    • TreeMap: 基于红黑树实现,按键排序。
    • WeakHashMap: 类似 HashMap,但是使用弱引用作为键。
    • IdentityHashMap: 使用 == 而不是 equals() 比较键的相等性。

以下是 ArrayListHashMap 的简单代码示例:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ContainerExample {
    public static void main(String[] args) {
        // ArrayList 示例
        List<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");
        System.out.println("List contains: " + list);

        // HashMap 示例
        Map<String, Integer> map = new HashMap<>();
        map.put("Apple", 1);
        map.put("Banana", 2);
        map.put("Cherry", 3);
        System.out.println("Map contains: " + map);
    }
}

这段代码分别演示了如何创建和向 ArrayListHashMap 中添加元素的基本操作。

【150 道精选 Java 高频面试题】请go公众号:码路向前 。

03-30 07:48