English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Contrôle de flux Java

Java tableau

Java orienté objet (I)

Java Orienté Objet (II)

Java Orienté Objet (III)

Java Exception Handling

Java Liste (List)

Java Queue (file d'attente)

Java Map Collections

Java Set Collections

Java Entrée Sortie (I/O)

Java Liseur/Écrivain

Autres sujets Java

Java programme pour créer une chaîne à partir du contenu du fichier

Java example summary

Dans ce programme, vous allez apprendre différentes techniques pour créer des chaînes à partir du contenu d'un fichier en utilisant Java.

Avant de créer une chaîne à partir de fichier, nous supposons que danssrcIl y a un dossier nommétest.txtle fichier.

C'esttest.txtdu contenu

This is a
Test file.

Exemple1Créer une chaîne à partir de fichier

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
public class FileString {
    public static void main(String[] args) throws IOException {
        String path = System.getProperty("user.dir") + "\\src\\test.txt";
        Charset encoding = Charset.defaultCharset();
        List<String> lines = Files.readAllLines(Paths.get(path), encoding);
        System.out.println(lines);
    }
}

When running this program, the output is:

[Ceci est un, fichier de test.]

Dans le programme ci-dessus, nous utilisons l'attribut user.dir de System pour obtenir le répertoire courant stocké dans la variable path. VérifiezJava programme pour obtenir le répertoire courant avecPlus d'informations.

Nous utilisons defaultCharset() comme encodage du fichier. Si vous connaissez l'encodage, utilisez-le, sinon utiliser l'encodage par défaut est sécurisé

Ensuite, nous utilisons la méthode readAllLines() pour lire toutes les lignes du fichier. Elle accepte le chemin du fichier et l'encodage, et retourne une liste de toutes les lignes, comme indiqué dans la sortie.

Puisque readAllLines peut également lever IOException, nous devons définir la méthode main de cette manière

public static void main(String[] args) throws IOException

Exemple2Créer une chaîne à partir de fichier

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
public class FileString {
    public static void main(String[] args) throws IOException {
        String path = System.getProperty("user.dir") + "\\src\\test.txt";
        Charset encoding = Charset.defaultCharset();
        byte[] encoded = Files.readAllBytes(Paths.get(path));
        String lines = new String(encoded, encoding);
        System.out.println(lines);
    }
}

When running this program, the output is:

This is a
Test file.

In the above program, we do not get a list of strings, but a string containing all the content.

Therefore, we use the readAllBytes() method to read all bytes from the given path. Then we convert these bytes to a string using the default encoding.

Java example summary