服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java从文件中读取数据的六种方法

java从文件中读取数据的六种方法

2022-07-13 10:06???YANG Java教程

本文主要介绍了java从文件中读取数据的方法,详细的介绍了六种方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文主要介绍了java从文件中读取数据的六种方法,分享给大家,具体如下:

  • Scanner(Java 1.5) 按行读数据及String、Int类型等按分隔符读数据。
  • Files.lines, 返回Stream(Java 8) 流式数据处理,按行读取
  • Files.readAllLines, 返回List(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
29
30
31
@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

2.Files.lines (Java 8)

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

?
1
2
3
4
5
6
7
8
9
10
11
12
@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);

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

?
1
2
// 转换成List<String>, 要注java.lang.OutOfMemoryError: Java heap space
List<String> collect = lines.collect(Collectors.toList()

3.Files.readAllLines(java8)

?
1
2
3
4
5
6
7
8
9
10
@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
7
@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
9
10
@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
20
@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从文件中读取数据的六种方法的文章就介绍到这了,更多相关java从文件中读取数据的六种方法内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家! 

原文链接:https://blog.csdn.net/tianynnb/article/details/121185052

延伸 · 阅读

精彩推荐