好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java io文件操作从文件读取数据的六种方法

在上一篇文章中,我为大家介绍了《5种创建文件并写入文件数据的方法》,本节我们为大家来介绍6种从文件中读取数据的方法.

另外为了方便大家理解,我为这一篇文章录制了对应的视频: 总结java从文件中读取数据的6种方法-JAVA IO基础总结第二篇

Scanner (Java 1.5) 按行读数据及String、Int类型等按分隔符读数据。

Files.lines , 返回Stream(Java 8) 流式数据处理,按行读取

Files.readAllLines , 返回List<String>(Java 8)

Files.readString , 读取String(Java 11), 文件最大 2G.

Files.readAllBytes , 读取byte[](Java 7), 文件最大 2G.

BufferedReader , 经典方式 (Java 1.1 -> forever)

可以说,每一种方法都有自己的适用场景,下文中为大家来一一介绍。

1.Scanner

第一种方式是Scanner,从JDK1.5开始提供的API,特点是可以按行读取、按分割符去读取文件数据,既可以读取String类型,也可以读取Int类型、Long类型等基础数据类型的数据。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

@Test

void testReadFile1() throws IOException {

    //文件内容:Hello World|Hello Zimug

    String fileName = "D:\\data\\test\\newFile4.txt" ;

    try (Scanner sc = new Scanner( new FileReader(fileName))) {

       while (sc.hasNextLine()) {  //按行读取字符串

          String line = sc.nextLine();

          System.out.println(line);

       }

    }

    try (Scanner sc = new Scanner( new FileReader(fileName))) {

       sc.useDelimiter( "\\|" );  //分隔符

       while (sc.hasNext()) {   //按分隔符读取字符串

          String str = sc.next();

          System.out.println(str);

       }

    }

    //sc.hasNextInt() 、hasNextFloat() 、基础数据类型等等等等。

    //文件内容:1|2

    fileName = "D:\\data\\test\\newFile5.txt" ;

    try (Scanner sc = new Scanner( new FileReader(fileName))) {

       sc.useDelimiter( "\\|" );  //分隔符

       while (sc.hasNextInt()) {   //按分隔符读取Int

           int intValue = sc.nextInt();

          System.out.println(intValue);

       }

    }

}

上面的方法输出结果如下:

Hello World|Hello Zimug
Hello World
Hello Zimug
1
2

2.Files.lines (Java 8)

如果你是需要按行去处理数据文件的内容,这种方式是我推荐大家去使用的一种方式,代码简洁,使用java 8的Stream流将文件读取与文件处理有机融合。

?

1

2

3

4

5

6

7

8

9

10

@Test

void testReadFile2() throws IOException {

    String fileName = "D:\\data\\test\\newFile.txt" ;

    // 读取文件内容到Stream流中,按行读取

    Stream<String> lines = Files.lines(Paths.get(fileName));

    // 随机行顺序进行数据处理

    lines.forEach(ele -> {

       System.out.println(ele);

    });

}

forEach获取Stream流中的行数据不能保证顺序,但速度快。如果你想按顺序去处理文件中的行数据,可以使用forEachOrdered,但处理效率会下降。

?

1

2

// 按文件行顺序进行处理

lines.forEachOrdered(System.out::println);

或者利用CPU多和的能力,进行数据的并行处理parallel(),适合比较大的文件。

?

1

2

// 按文件行顺序进行处理

lines.parallel().forEachOrdered(System.out::println);

也可以把 Stream<String> 转换成 List<String> ,但是要注意这意味着你要将所有的数据一次性加载到内存,要注意java.lang.OutOfMemoryError

?

1

2

// 转换成List<String>, 要注意java.lang.OutOfMemoryError: Java heap space

List<String> collect = lines.collect(Collectors.toList());

3.Files.readAllLines

这种方法仍然是java8 为我们提供的,如果我们不需要 Stream<String> ,我们想直接按行读取文件获取到一个 List<String> ,就采用下面的方法。同样的问题:这意味着你要将所有的数据一次性加载到内存,要注意java.lang.OutOfMemoryError

?

1

2

3

4

5

6

7

8

9

@Test

void testReadFile3() throws IOException {

    String fileName = "D:\\data\\test\\newFile3.txt" ;

    // 转换成List<String>, 要注意java.lang.OutOfMemoryError: Java heap space

    List<String> lines = Files.readAllLines(Paths.get(fileName),

                StandardCharsets.UTF_8);

    lines.forEach(System.out::println);

 

}

4.Files.readString(JDK 11)

从 java11开始,为我们提供了一次性读取一个文件的方法。文件不能超过2G,同时要注意你的服务器及JVM内存。这种方法适合快速读取小文本文件。

?

1

2

3

4

5

6

@Test

void testReadFile4() throws IOException {

    String fileName = "D:\\data\\test\\newFile3.txt" ;

    // java 11 开始提供的方法,读取文件不能超过2G,与你的内存息息相关

    //String s = Files.readString(Paths.get(fileName));

}

5.Files.readAllBytes()

如果你没有JDK11(readAllBytes()始于JDK7),仍然想一次性的快速读取一个文件的内容转为String,该怎么办?先将数据读取为二进制数组,然后转换成String内容。这种方法适合在没有JDK11的请开给你下,快速读取小文本文件。

?

1

2

3

4

5

6

7

8

@Test

void testReadFile5() throws IOException {

    String fileName = "D:\\data\\test\\newFile3.txt" ;

    //如果是JDK11用上面的方法,如果不是用这个方法也很容易

    byte [] bytes = Files.readAllBytes(Paths.get(fileName));

    String content = new String(bytes, StandardCharsets.UTF_8);

    System.out.println(content);

}

6.经典管道流的方式

最后一种就是经典的管道流的方式

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

@Test

void testReadFile6() throws IOException {

    String fileName = "D:\\data\\test\\newFile3.txt" ;

    // 带缓冲的流读取,默认缓冲区8k

    try (BufferedReader br = new BufferedReader( new FileReader(fileName))){

       String line;

       while ((line = br.readLine()) != null ) {

          System.out.println(line);

       }

    }

    //java 8中这样写也可以

    try (BufferedReader br = Files.newBufferedReader(Paths.get(fileName))){

       String line;

       while ((line = br.readLine()) != null ) {

          System.out.println(line);

       }

    }

 

}

这种方式可以通过管道流嵌套的方式,组合使用,比较灵活。比如我们
想从文件中读取java Object就可以使用下面的代码,前提是文件中的数据是ObjectOutputStream写入的数据,才可以用ObjectInputStream来读取。

?

1

2

3

4

try (FileInputStream fis = new FileInputStream(fileName);

      ObjectInputStream ois = new ObjectInputStream(fis)){

    ois.readObject();

}

以上就是java io操作总结从文件读取数据的六种方法的详细内容,更多关于java io从文件读取数据的资料请关注其它相关文章!

原文链接:https://zimug.blog.csdn.net/article/details/108314370

查看更多关于java io文件操作从文件读取数据的六种方法的详细内容...

  阅读:21次