|
| 1 | +package com.wdbyte.io.file; |
| 2 | + |
| 3 | +import java.io.BufferedInputStream; |
| 4 | +import java.io.BufferedReader; |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileInputStream; |
| 7 | +import java.io.FileNotFoundException; |
| 8 | +import java.io.FileReader; |
| 9 | +import java.io.IOException; |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.nio.file.Paths; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Scanner; |
| 16 | +import java.util.stream.Stream; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author niulang |
| 22 | + * @date 2023/11/08 |
| 23 | + */ |
| 24 | +public class FileReadDemo { |
| 25 | + |
| 26 | + public static void main(String[] args) { |
| 27 | + Path path = Paths.get("/Users/darcy/wdbyte"); |
| 28 | + System.out.println(Files.isDirectory(path)); |
| 29 | + System.out.println(path.toFile().isFile()); |
| 30 | + System.out.println(path.toFile().isDirectory()); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * java 8 |
| 35 | + * |
| 36 | + * @throws IOException |
| 37 | + */ |
| 38 | + @Test |
| 39 | + public void test1() throws IOException { |
| 40 | + Path path = Paths.get("/Users/darcy/wdbyte/log.txt"); |
| 41 | + List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8); |
| 42 | + for (String line : lines) { |
| 43 | + System.out.println(line); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * java 8 |
| 49 | + * |
| 50 | + * @throws IOException |
| 51 | + */ |
| 52 | + @Test |
| 53 | + public void test2() throws IOException { |
| 54 | + Path path = Paths.get("/Users/darcy/wdbyte/log.txt"); |
| 55 | + byte[] bytes = Files.readAllBytes(path); |
| 56 | + String content = new String(bytes, StandardCharsets.UTF_8); |
| 57 | + System.out.println(content); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * java 8 |
| 62 | + * |
| 63 | + * @throws IOException |
| 64 | + */ |
| 65 | + @Test |
| 66 | + public void test3() throws IOException, InterruptedException { |
| 67 | + Path path = Paths.get("/Users/darcy/wdbyte/log.txt"); |
| 68 | + try (Stream<String> stream = Files.lines(path);) { |
| 69 | + stream.forEach(System.out::println); |
| 70 | + } |
| 71 | + try (Stream<String> stream = Files.lines(path);) { |
| 72 | + stream.parallel().forEachOrdered(System.out::println); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * java 11 |
| 78 | + * 要求:文件小于2G |
| 79 | + * |
| 80 | + * @throws IOException |
| 81 | + */ |
| 82 | + @Test |
| 83 | + public void test4() throws IOException { |
| 84 | + Path path = Paths.get("/Users/darcy/wdbyte/log.txt"); |
| 85 | + String content = Files.readString(path, StandardCharsets.UTF_8); |
| 86 | + System.out.println(content); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void test5() { |
| 91 | + File file = new File("/Users/darcy/wdbyte/log.txt"); |
| 92 | + String line; |
| 93 | + // 默认读取缓冲区大小 8K |
| 94 | + try (BufferedReader br = new BufferedReader(new FileReader(file))) { |
| 95 | + while ((line = br.readLine()) != null) { |
| 96 | + System.out.println(line); |
| 97 | + } |
| 98 | + } catch (FileNotFoundException e) { |
| 99 | + throw new RuntimeException(e); |
| 100 | + } catch (IOException e) { |
| 101 | + throw new RuntimeException(e); |
| 102 | + } |
| 103 | + |
| 104 | + // 定义缓冲区大小为 128 K |
| 105 | + int buffer = 128 * 1024; |
| 106 | + try (BufferedReader br = new BufferedReader(new FileReader(file), buffer)) { |
| 107 | + while ((line = br.readLine()) != null) { |
| 108 | + System.out.println(line); |
| 109 | + } |
| 110 | + } catch (FileNotFoundException e) { |
| 111 | + throw new RuntimeException(e); |
| 112 | + } catch (IOException e) { |
| 113 | + throw new RuntimeException(e); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * java 8 |
| 119 | + * |
| 120 | + * @throws IOException |
| 121 | + */ |
| 122 | + @Test |
| 123 | + public void test6() throws IOException { |
| 124 | + Path path = Paths.get("/Users/darcy/wdbyte/log.txt"); |
| 125 | + String line; |
| 126 | + try (BufferedReader br = Files.newBufferedReader(path, StandardCharsets.UTF_8);) { |
| 127 | + while ((line = br.readLine()) != null) { |
| 128 | + System.out.println(line); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * 自古以来 |
| 135 | + */ |
| 136 | + @Test |
| 137 | + public void test7() { |
| 138 | + File file = new File("/Users/darcy/wdbyte/log.txt"); |
| 139 | + try (Scanner sc = new Scanner(new FileReader(file))) { |
| 140 | + while (sc.hasNextLine()) { |
| 141 | + String line = sc.nextLine(); |
| 142 | + System.out.println(line); |
| 143 | + } |
| 144 | + } catch (FileNotFoundException e) { |
| 145 | + throw new RuntimeException(e); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * 自古以来 |
| 151 | + */ |
| 152 | + @Test |
| 153 | + public void test8() { |
| 154 | + File file = new File("/Users/darcy/wdbyte/log.txt"); |
| 155 | + try (FileInputStream fis = new FileInputStream(file); |
| 156 | + BufferedInputStream bis = new BufferedInputStream(fis);) { |
| 157 | + |
| 158 | + StringBuilder content = new StringBuilder(); |
| 159 | + int data; |
| 160 | + while ((data = bis.read()) != -1) { |
| 161 | + content.append((char)data); |
| 162 | + } |
| 163 | + System.out.println(content.toString()); |
| 164 | + } catch (IOException e) { |
| 165 | + e.printStackTrace(); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments