This is a memo of Java (jdk1.8 or later) file input / output sample program.
This is a sample program that reads C: /dk/input.xlsx
all at once and outputs it to standard output in hexadecimal.
When reading in bulk, you need to be careful about the size so that it does not become Out of memory.
BinaryFileInputNio.java
package test.nio;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
*Read a binary file
*/
public class BinaryFileInputNio {
/**
* @param args
*/
public static void main(String[] args) {
//The name of the read file
Path path = Paths.get("C:/dk", "input.xlsx");
try {
//Bulk reading of binary files
byte[] data = Files.readAllBytes(path);
//Output the read binary data to standard output in hexadecimal
for (int i = 0; i < data.length; i++) {
System.out.printf("%02x ", data[i]);
}
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
}
}
This is a sample program that reads C: /dk/input.xlsx
in 1024-byte units and outputs it to standard output in hexadecimal.
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;
/**
*Read a binary file
*/
public class BinaryFileInputNioPart {
/**
* @param args
*/
public static void main(String[] args) {
//The name of the read file
Path path = Paths.get("C:/dk", "input.xlsx");
//Input stream generation
try (InputStream inst = Files.newInputStream(path)) {
//Byte array for reading
byte[] buf = new byte[1024];
int len = 0;
//Reading from the input stream (reading a file)
while ((len = inst.read(buf)) != -1) {
//Output the read binary data to standard output in hexadecimal
for (int i = 0; i < len; i++) {
System.out.printf("%02x ", buf[i]);
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
This is a sample program that writes binary data to C: /dk/output.dat
at once.
BinaryFileOutputNio.java
package test.nio;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
*Write a binary file
*/
public class BinaryFileOutputNio {
/**
* @param args
*/
public static void main(String[] args) {
//The name of the write file
Path path = Paths.get("C:/dk", "output.dat");
//Binary data
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 {
//Binary data batch write
Files.write(path, data);
} catch (IOException e) {
e.printStackTrace();
}
}
}
This is a sample program that writes binary data to C: /dk/output.dat
in parts.
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;
/**
*Write a binary file
*/
public class BinaryFileOutputNioPart {
/**
* @param args
*/
public static void main(String[] args) {
//The name of the write file
Path path = Paths.get("C:/dk", "output.dat");
//Binary data
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};
//Binary data writing
try (OutputStream os = Files.newOutputStream(path)) {
os.write(data1);
os.write(data2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
This is a sample program that reads C: /dk/input.txt
in a batch and outputs it to standard output.
When reading in bulk, you need to be careful about the size so that it does not become Out of memory.
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;
/**
*Reading a text file
*/
public class TextFileInputNio {
/**
* @param args
*/
public static void main(String[] args) {
//The name of the read file
Path path = Paths.get("C:/dk", "input.txt");
try {
//Bulk read text files
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
//Output the read text file to standard output
lines.forEach(line -> System.out.println(line));
} catch (IOException e) {
e.printStackTrace();
}
}
}
This is a sample program that reads C: /dk/input.txt
with BufferedReader and outputs it to standard output.
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;
/**
*Reading a text file
*/
public class TextFileInputNioPart1 {
/**
* @param args
*/
public static void main(String[] args) {
//The name of the read file
Path path = Paths.get("C:/dk", "input.txt");
//Generate BufferedReader
try (BufferedReader br = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
String msg;
//Reading from a text file
while ((msg = br.readLine()) != null) {
//Output to standard output
System.out.println(msg);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
This is a sample program that reads C: /dk/input.txt
as Stream and outputs it to standard output.
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;
/**
*Reading a text file
*/
public class TextFileInputNioPart2 {
/**
* @param args
*/
public static void main(String[] args) {
//The name of the read file
Path path = Paths.get("C:/dk", "input.txt");
//Stream generation
try (Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8)) {
//Output to standard output
lines.forEach(line -> System.out.println(line));
} catch (IOException e) {
e.printStackTrace();
}
}
}
This is a sample program that writes text data to C: /dk/output.txt
using 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;
/**
*Writing to a text file
*/
public class TextFileOutputNio1 {
/**
* @param args
*/
public static void main(String[] args) {
//The name of the write file
Path path = Paths.get("C:/dk", "output.txt");
//Generate BufferedWriter
try (BufferedWriter bw = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
//Write to file
for (int i= 1; i <= 2; i++) {
bw.write("Text file output" + String.format("%02d", i));
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
This is a sample program that writes text data to C: /dk/output.txt
using 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;
/**
*Writing to a text file
*/
public class TextFileOutputNio2 {
/**
* @param args
*/
public static void main(String[] args) {
//The name of the write file
Path path = Paths.get("C:/dk", "output.txt");
try (
//Generate PrintWriter
BufferedWriter bw = Files.newBufferedWriter(path, StandardCharsets.UTF_8);
PrintWriter pw = new PrintWriter(bw);
) {
//Write to file
for (int i= 1; i <= 2; i++) {
pw.println("Text file output" + String.format("%02d", i));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
that's all
Recommended Posts