Ceci est un mémo de l'exemple de programme d'entrée / sortie de fichier Java (jdk1.8 ou version ultérieure).
Il s'agit d'un exemple de programme qui lit C: / dk / input.xlsx
en une seule fois et le sort en sortie standard en hexadécimal.
Lors de la lecture en masse, vous devez faire attention à la taille afin qu'elle ne devienne pas saturée de mémoire.
BinaryFileInputNio.java
package test.nio;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
*Lire un fichier binaire
*/
public class BinaryFileInputNio {
/**
* @param args
*/
public static void main(String[] args) {
//Le nom du fichier lu
Path path = Paths.get("C:/dk", "input.xlsx");
try {
//Lecture en masse de fichiers binaires
byte[] data = Files.readAllBytes(path);
//Sortie des données binaires lues vers la sortie standard en hexadécimal
for (int i = 0; i < data.length; i++) {
System.out.printf("%02x ", data[i]);
}
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Il s'agit d'un exemple de programme qui lit C: / dk / input.xlsx
en unités de 1024 octets et le sort en sortie standard en hexadécimal.
BinaryFileInputNioPart.java
package test.nio;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
*Lire un fichier binaire
*/
public class BinaryFileInputNioPart {
/**
* @param args
*/
public static void main(String[] args) {
//Le nom du fichier lu
Path path = Paths.get("C:/dk", "input.xlsx");
//Génération de flux d'entrée
try (InputStream inst = Files.newInputStream(path)) {
//Tableau d'octets pour la lecture
byte[] buf = new byte[1024];
int len = 0;
//Lecture à partir du flux d'entrée (lecture d'un fichier)
while ((len = inst.read(buf)) != -1) {
//Sortie des données binaires lues vers la sortie standard en hexadécimal
for (int i = 0; i < len; i++) {
System.out.printf("%02x ", buf[i]);
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Ceci est un exemple de programme qui écrit des données binaires dans C: / dk / output.dat
à la fois.
BinaryFileOutputNio.java
package test.nio;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
*Ecrire un fichier binaire
*/
public class BinaryFileOutputNio {
/**
* @param args
*/
public static void main(String[] args) {
//Nom du fichier d'écriture
Path path = Paths.get("C:/dk", "output.dat");
//Données binaires
byte[] data = { (byte)0xe3, (byte)0x83, (byte)0x86, (byte)0xe3, (byte)0x82, (byte)0xad, (byte)0xe3, (byte)0x82, (byte)0xb9, (byte)0xe3, (byte)0x83, (byte)0x88, (byte)0xe3, (byte)0x83, (byte)0x95, (byte)0xe3, (byte)0x82, (byte)0xa1, (byte)0xe3, (byte)0x82, (byte)0xa4, (byte)0xe3, (byte)0x83, (byte)0xab, (byte)0xe5, (byte)0x87, (byte)0xba, (byte)0xe5, (byte)0x8a, (byte)0x9b, (byte)0x30, (byte)0x31, (byte)0x0d, (byte)0x0a, (byte)0xe3, (byte)0x83, (byte)0x86, (byte)0xe3, (byte)0x82, (byte)0xad, (byte)0xe3, (byte)0x82, (byte)0xb9, (byte)0xe3, (byte)0x83, (byte)0x88, (byte)0xe3, (byte)0x83, (byte)0x95, (byte)0xe3, (byte)0x82, (byte)0xa1, (byte)0xe3, (byte)0x82, (byte)0xa4, (byte)0xe3, (byte)0x83, (byte)0xab, (byte)0xe5, (byte)0x87, (byte)0xba, (byte)0xe5, (byte)0x8a, (byte)0x9b, (byte)0x30, (byte)0x32, (byte)0x0d, (byte)0x0a};
try {
//Écriture par lots de données binaires
Files.write(path, data);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Ceci est un exemple de programme qui écrit des données binaires dans C: / dk / output.dat
par parties.
BinaryFileOutputNioPart.java
package test.nio;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
*Ecrire un fichier binaire
*/
public class BinaryFileOutputNioPart {
/**
* @param args
*/
public static void main(String[] args) {
//Nom du fichier d'écriture
Path path = Paths.get("C:/dk", "output.dat");
//Données binaires
byte[] data1 = { (byte)0xe3, (byte)0x83, (byte)0x86, (byte)0xe3, (byte)0x82, (byte)0xad, (byte)0xe3, (byte)0x82, (byte)0xb9, (byte)0xe3, (byte)0x83, (byte)0x88, (byte)0xe3, (byte)0x83, (byte)0x95, (byte)0xe3, (byte)0x82, (byte)0xa1, (byte)0xe3, (byte)0x82, (byte)0xa4, (byte)0xe3, (byte)0x83, (byte)0xab, (byte)0xe5, (byte)0x87, (byte)0xba, (byte)0xe5, (byte)0x8a, (byte)0x9b, (byte)0x30, (byte)0x31, (byte)0x0d, (byte)0x0a};
byte[] data2 = { (byte)0xe3, (byte)0x83, (byte)0x86, (byte)0xe3, (byte)0x82, (byte)0xad, (byte)0xe3, (byte)0x82, (byte)0xb9, (byte)0xe3, (byte)0x83, (byte)0x88, (byte)0xe3, (byte)0x83, (byte)0x95, (byte)0xe3, (byte)0x82, (byte)0xa1, (byte)0xe3, (byte)0x82, (byte)0xa4, (byte)0xe3, (byte)0x83, (byte)0xab, (byte)0xe5, (byte)0x87, (byte)0xba, (byte)0xe5, (byte)0x8a, (byte)0x9b, (byte)0x30, (byte)0x32, (byte)0x0d, (byte)0x0a};
//Écriture de données binaires
try (OutputStream os = Files.newOutputStream(path)) {
os.write(data1);
os.write(data2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Il s'agit d'un exemple de programme qui lit C: / dk / input.txt
dans un lot et le sort sur la sortie standard.
Lors de la lecture en masse, vous devez faire attention à la taille afin qu'elle ne devienne pas saturée de mémoire.
TextFileInputNio.java
package test.nio;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
/**
*Lire un fichier texte
*/
public class TextFileInputNio {
/**
* @param args
*/
public static void main(String[] args) {
//Le nom du fichier lu
Path path = Paths.get("C:/dk", "input.txt");
try {
//Fichiers texte lus en masse
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
//Exporte le fichier texte lu vers la sortie standard
lines.forEach(line -> System.out.println(line));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Il s'agit d'un exemple de programme qui lit C: / dk / input.txt
avec BufferedReader et le sort en sortie standard.
TextFileInputNioPart1.java
package test.nio;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
*Lire un fichier texte
*/
public class TextFileInputNioPart1 {
/**
* @param args
*/
public static void main(String[] args) {
//Le nom du fichier lu
Path path = Paths.get("C:/dk", "input.txt");
//Générer BufferedReader
try (BufferedReader br = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
String msg;
//Lecture à partir d'un fichier texte
while ((msg = br.readLine()) != null) {
//Sortie vers sortie standard
System.out.println(msg);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Il s'agit d'un exemple de programme qui lit C: / dk / input.txt
en tant que Stream et le renvoie vers la sortie standard.
TextFileInputNioPart2.java
package test.nio;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
/**
*Lire un fichier texte
*/
public class TextFileInputNioPart2 {
/**
* @param args
*/
public static void main(String[] args) {
//Le nom du fichier lu
Path path = Paths.get("C:/dk", "input.txt");
//Génération de flux
try (Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8)) {
//Sortie vers sortie standard
lines.forEach(line -> System.out.println(line));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Il s'agit d'un exemple de programme qui écrit des données texte dans C: / dk / output.txt
à l'aide de BufferedWriter.
TextFileOutputNio1.java
package test.nio;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
*Ecrire dans un fichier texte
*/
public class TextFileOutputNio1 {
/**
* @param args
*/
public static void main(String[] args) {
//Nom du fichier d'écriture
Path path = Paths.get("C:/dk", "output.txt");
//Générer BufferedWriter
try (BufferedWriter bw = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
//Écrire dans un fichier
for (int i= 1; i <= 2; i++) {
bw.write("Sortie de fichier texte" + String.format("%02d", i));
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Il s'agit d'un exemple de programme qui écrit des données texte dans C: / dk / output.txt
à l'aide de PrintWriter.
TextFileOutputNio2.java
package test.nio;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
*Ecrire dans un fichier texte
*/
public class TextFileOutputNio2 {
/**
* @param args
*/
public static void main(String[] args) {
//Nom du fichier d'écriture
Path path = Paths.get("C:/dk", "output.txt");
try (
//Générer PrintWriter
BufferedWriter bw = Files.newBufferedWriter(path, StandardCharsets.UTF_8);
PrintWriter pw = new PrintWriter(bw);
) {
//Écrire dans un fichier
for (int i= 1; i <= 2; i++) {
pw.println("Sortie de fichier texte" + String.format("%02d", i));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
c'est tout
Recommended Posts