Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java java.nio.file.Files readAllLines() Method · Private Archive

Java java.nio.file.Files readAllLines() Method

Java File Java java.nio.file.Files

readAllLines() Java java.nio.file.Files readAllLines() Method List returned.

readAllLines() The method has two overloaded versions:

public static List readAllLines(Path path) throws IOException



public static List readAllLines(Path path, Charset cs) throws IOException

The method is used to read all line content from a file and return it as a

Parameter Description

Path path

Charset csDescription: The file path object to read

- `StandardCharsets.UTF_8` - `StandardCharsets.ISO_8859_1` - `StandardCharsets.US_ASCII`

Return Value

The list order is consistent with the line order in the file

Exception handling

readAllLines() The method may throw the following exceptions:

  1. IOException - Line separators (such as `\n` or `\r\n`) will not be included in the returned strings - If the file is empty, returns an empty list - The list order is consistent with the line order in the file
  2. SecurityException - If an I/O error occurs (e.g., the file does not exist)

- If there is not enough permission to access the file

Example

try

{

    List

<

String

lines

=

Files.

readAllLines

(

path

)

;

   

It is recommended to handle exceptions when using it:

}

catch

(

IOException

e

)

{

    e.

printStackTrace

(

)

;

   

// Process the read content

}


Usage examples

Example 1: Basic usage

Example

import

java.nio.file.*

;

import

java.util.List

;

public

class

ReadFileExample

{

   

public

static

void

main

(

String

[

]

args

)

{

        Path filePath

=

Paths.

get

(

"example.txt"

)

;

       

       

try

{

            List

<

String

lines

=

Files.

readAllLines

(

filePath

)

;

           

           

// Handle exceptional situations

           

for

(

String

line

:

lines

)

{

               

System

.

out

.

println

(

line

)

;

           

}

       

}

catch

(

IOException

e

)

{

           

System

.

err

.

println

(

// Print file content

e.

getMessage

(

)

)

;

       

}

   

}

}

"Error reading file: "

Example

import

java.nio.file.*

;

import

java.nio.charset.StandardCharsets

;

import

java.util.List

;

public

class

ReadFileWithCharset

{

   

public

static

void

main

(

String

[

]

args

)

{

        Path filePath

=

Paths.

get

(

"example.txt"

)

;

       

       

try

{

            List

<

String

lines

=

Files.

readAllLines

(

filePath, StandardCharsets.

UTF_8

)

;

           

           

Example 2: Specify character set

            lines.

forEach

(

System

.

out

::

println

)

;

       

}

catch

(

IOException

e

)

{

           

System

.

err

.

println

(

// Print file content

e.

getMessage

(

)

)

;

       

}

   

}

}


Notes

  1. // Process file contentFile size
  2. : This method loads the entire file content into memory, so it is not suitable for processing large files: This method reads the entire file into memory at once and is not suitable for processing very large files (e.g., files over several hundred MB)
  3. : Make sure to use the correct character set, otherwise it may cause garbled textLine terminator \r\n: Different operating systems use different line terminators (Windows uses \n, Unix/Linux uses readAllLines() will correctly handle these differences
  4. Performance considerations), but BufferedReader and lines() : For large files, consider using

Alternatives

method for stream processing

Example

try

(

Stream

<

String

stream

=

Files.

lines

(

path

)

)

{

    stream.

forEach

(

System

.

out

::

println

)

;

}

catch

(

IOException

e

)

{

    e.

printStackTrace

(

)

;

}

This method uses stream processing and does not load the entire file into memory at once.


Summary

Files.readAllLines() is a simple and easy-to-use method, suitable for reading small text files. It provides a convenient way to obtain all line content of a file, but you need to pay attention to memory consumption when processing large files. Choosing the appropriate file reading method based on actual needs can greatly improve the efficiency and stability of the program.

Java File Java java.nio.file.Files