Java代码如何读取文件呢?

书欣 Java经验 发布时间:2023-01-28 10:59:13 阅读数:15958 1
下文笔者讲述如何使用java代码读取文件的方法分享,如下所示

java读取文件的方法

实现思路:
    方式1:
	   Java 8中Files.lines方法,将会读取文件返回一个stream 
	   如: 
	Stream<String> lines = Files.lines(Paths.get("test.log"));
	list<String> content = lines.collect(Collectors.toList());

    方式2:
	   Java 7中Files.readAllBytes或Files.readAllLines
    如:
	//返回字节数组
	byte[] bytes = Files.readAllBytes(Paths.get("test.log"));
	String content = new String(bytes);
	
	//返回字符串
	List<String> content = Files.readAllLines(Paths.get("test.log"));


    方式3:经典BufferedReader
    try (FileReader reader = new FileReader("test.log");
		BufferedReader br = new BufferedReader(reader)) {
 
		// read line by line
		String line;
		while ((line = br.readLine()) != null) {
			System.out.println(line);
		}
 
     } catch (IOException e) {
            //e
     }

Files.lines

在Java 8中
我们可以使用Files.lines将文件读入Stream
  它将自动关闭资源(打开的文件)
package com.java265;
 
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
public class FileExample1 {
    public static void main(String[] args) {
        try {
 
            Stream<String> lines = Files.lines(Paths.get("test.log"));
            List<String> content = lines.collect(Collectors.toList());
            content.forEach(x -> System.out.println(x));
 
        } catch (IOException e) {
            System.err.format("IOException: %s%n", e);
        }
    }
 
}

Files.readAllBytes

在Java 7中
   可使用Files.readAllBytes或Files.readAllLines读取文件
   它将自动关闭资源(打开的文件)
package com.java265;
 
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
 
public class FileExample2 {
    public static void main(String[] args) {
        try {
 
            byte[] bytes = Files.readAllBytes(Paths.get("test.log"));
            String content = new String(bytes);
 
            System.out.println(content);
 
        } catch (IOException e) {
            System.err.format("IOException: %s%n", e);
        }
    }
 
}

BufferedReader

BufferedReader
   带有try-with-resources
    可自动关闭资源
package com.java265;
 
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileExample3 {
 
    public static void main(String[] args) {
 
        try (FileReader reader = new FileReader("test.log");
             BufferedReader br = new BufferedReader(reader)) {
 
            // read line by line
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
 
        } catch (IOException e) {
            System.err.format("IOException: %s%n", e);
        }
    }
}

使用Scanner扫描文件示例

package com.java265;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
 
public class FileExample4 {
 
    public static void main(String[] args) {
 
        try (FileReader fr = new FileReader("test.log");
             Scanner scanner = new Scanner(fr)) {
 
            StringBuilder sb = new StringBuilder();
            while (scanner.hasNext()) {
                sb.append(scanner.nextLine()).append("\n");
            }
 
            System.out.println(sb.toString());
 
        } catch (IOException e) {
            System.err.format("IOException: %s%n", e);
        }
    }
} 
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202301/16748754045546.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者