Home> Java> javaTutorial> body text

How to read file content in java

小老鼠
Release: 2024-03-22 15:39:41
Original
440 people have browsed it

Java provides a variety of ways to read file contents, including: * **Files.readAllLines/Files.readAllBytes (Java 8 and above)**: Using the java.nio.file.Files class, you can easily read all lines or the entire content of a file. * **BufferedReader**: For older versions of Java, you can use the BufferedReader class to read a file line by line. * **Scanner**: The Scanner class provides another way to read files, which can read the content line by line or by delimiter.

How to read file content in java

In Java, you can read the contents of a file in a variety of ways. The following are several common methods:

1. Use the java.nio.file.Files class (Java 8 and above)

Java 8 introduces a new file I/O API to make file operations simpler. You can read all lines of a file into a list using the Files.readAllLines method, or read the entire contents of a file into a byte array using the Files.readAllBytes method.

java

##

import java.nio.file.Files; import java.nio.file.Paths; import java.io.IOException; import java.util.List; public class Main { public static void main(String[] args) { try { // 读取文件的所有行到一个列表中 List lines = Files.readAllLines(Paths.get("path_to_your_file.txt")); for (String line : lines) { System.out.println(line); } // 或者读取文件的全部内容到字节数组中 byte[] bytes = Files.readAllBytes(Paths.get("path_to_your_file.txt")); String content = new String(bytes); System.out.println(content); } catch (IOException e) { e.printStackTrace(); } } }
Copy after login

2. Use java.io.BufferedReader

For older versions of Java, you can use BufferedReader to read files.

java

import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Main { public static void main(String[] args) { BufferedReader br = null; try { br = new BufferedReader(new FileReader("path_to_your_file.txt")); String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
Copy after login

3. Use java.util.Scanner

You can also use the Scanner class to read files.

java

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = null; try { scanner = new Scanner(new File("path_to_your_file.txt")); while (scanner.hasNextLine()) { String line = scanner.nextLine(); System.out.println(line); } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (scanner != null) { scanner.close(); } } } }
Copy after login

In all three examples, you need to " path_to_your_file.txt" with your file path. If the file is in your project directory, you can use the filename directly. Otherwise, you need to provide the full file path.

Note that these methods may throw IOException, so you need to use a try-catch block to handle possible exceptions. In the BufferedReader and Scanner examples, we also added finally blocks to ensure that the file stream is closed properly to avoid resource leaks.

The above is the detailed content of How to read file content in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!