Skip to content

io microsphere nio file Files

github-actions[bot] edited this page Aug 10, 2026 · 1 revision

Files

Type: Class | Module: microsphere-java-core | Package: io.microsphere.nio.file | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/nio/file/Files.java

Overview

The utilties class of File based on NIO

Declaration

public abstract class Files implements Utils

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.3.18-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

Method Examples

readLines

File file = new File("/example.txt");
try {
    String[] lines = Files.readLines(file);
    for (String line : lines) {
        System.out.println(line);
    }
} catch(IOException e) {
    // Handle the exception
}

readLines

File file = new File("/example.txt");
Charset charset = java.nio.charset.StandardCharsets.UTF_8;
try {
    String[] lines = Files.readLines(file, charset);
    for (String line : lines) {
        System.out.println(line);
    }
} catch(IOException e) {
    // Handle the exception
}

readLines

Path filePath = Paths.get("/example.txt");
try {
    String[] lines = Files.readLines(filePath);
    for (String line : lines) {
        System.out.println(line);
    }
} catch(IOException e) {
    // Handle the exception
}

readLines

Path filePath = Paths.get("/example.txt");
Charset charset = java.nio.charset.StandardCharsets.UTF_8;
try {
    String[] lines = Files.readLines(filePath, charset);
    for (String line : lines) {
        System.out.println(line);
    }
} catch(IOException e) {
    // Handle the exception
}

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.nio.file.Files;

API Reference

Public Methods

Method Description
readLines Reads all lines from the given file and returns them as an array of strings,
readLines Reads all lines from the given file and returns them as an array of strings,
readLines Reads all lines from the given file path and returns them as an array of strings,
readLines Reads all lines from the given file path and returns them as an array of strings,

Method Details

readLines

public static String[] readLines(File file)

Reads all lines from the given file and returns them as an array of strings, using the default Charset for decoding.

This method reads the entire content of the file, converts it to a string using the default charset, and then splits the string into lines based on the system line separator. The file input stream is automatically closed after this operation.

Example Usage

`File file = new File("/example.txt");
try {
    String[] lines = Files.readLines(file);
    for (String line : lines) {
        System.out.println(line);
    `
} catch(IOException e) {
    // Handle the exception
}
}

readLines

public static String[] readLines(File file, Charset charset)

Reads all lines from the given file and returns them as an array of strings, using the specified Charset for decoding.

This method reads the entire content of the file, converts it to a string using the provided charset, and then splits the string into lines based on the system line separator. The file input stream is automatically closed after this operation.

Example Usage

`File file = new File("/example.txt");
Charset charset = java.nio.charset.StandardCharsets.UTF_8;
try {
    String[] lines = Files.readLines(file, charset);
    for (String line : lines) {
        System.out.println(line);
    `
} catch(IOException e) {
    // Handle the exception
}
}

readLines

public static String[] readLines(Path filePath)

Reads all lines from the given file path and returns them as an array of strings, using the default Charset for decoding.

This method reads the entire content of the file, converts it to a string using the default charset, and then splits the string into lines based on the system line separator. The file input stream is automatically closed after this operation.

Example Usage

`Path filePath = Paths.get("/example.txt");
try {
    String[] lines = Files.readLines(filePath);
    for (String line : lines) {
        System.out.println(line);
    `
} catch(IOException e) {
    // Handle the exception
}
}

readLines

public static String[] readLines(Path filePath, Charset charset)

Reads all lines from the given file path and returns them as an array of strings, using the specified Charset for decoding.

This method reads the entire content of the file, converts it to a string using the provided charset, and then splits the string into lines based on the system line separator. The file input stream is automatically closed after this operation.

Example Usage

`Path filePath = Paths.get("/example.txt");
Charset charset = java.nio.charset.StandardCharsets.UTF_8;
try {
    String[] lines = Files.readLines(filePath, charset);
    for (String line : lines) {
        System.out.println(line);
    `
} catch(IOException e) {
    // Handle the exception
}
}

See Also

  • Path

This documentation was auto-generated from the source code of microsphere-java.

Home

annotation-processor

annotation-test

java-annotations

java-core

java-test

jdk-tools

lang-model

Clone this wiki locally