Java java.nio.file.Files readAllLines() Method
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
- Type:
java.nio.file.Path - The first version reads the file using the default character set (UTF-8), and the second version allows specifying the character set.
- Example:
Path filePath = Paths.get("example.txt");
Charset csDescription: The file path object to read
- Type:
java.nio.charset.Charset - (Optional parameter)
- Description: The character set used to decode the file content
StandardCharsets.UTF_8StandardCharsets.ISO_8859_1StandardCharsets.US_ASCII
- Example:
Charset charset = StandardCharsets.UTF_8;
Return Value
- Type:
List - Common character sets:
- Note:
- Description: A list containing all lines of the file, each element representing one line in the file
\nor\r\nLine separators (such as - ) will not be included in the returned strings
- If the file is empty, returns an empty list
Exception handling
readAllLines() The method may throw the following exceptions:
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 fileSecurityException- 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
- // Process file contentFile size
- : 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)
- : 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 usesreadAllLines()will correctly handle these differences - Performance considerations), but
BufferedReaderandlines(): 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 java.nio.file.Files