读、写、创建文件
打印信息
private static void info() throws IOException {
Path path = Paths.get("src\\com\\xq\\demo\\java8\\Test1.java").toAbsolutePath();
System.out.println(path);
System.out.println("文件是否存在: " + Files.exists(path));
System.out.println("是否是目录: " + Files.isDirectory(path));
System.out.println("是否是可执行文件: " + Files.isExecutable(path));
System.out.println("是否可读: " + Files.isReadable(path));
System.out.println("判断是否是一个文件: " + Files.isRegularFile(path));
System.out.println("是否可写: " + Files.isWritable(path));
System.out.println("文件是否不存在: " + Files.notExists(path));
System.out.println("文件是否隐藏: " + Files.isHidden(path));
System.out.println("文件大小: " + Files.size(path));
System.out.println("文件存储在SSD还是HDD: " + Files.getFileStore(path));
System.out.println("文件修改时间:" + Files.getLastModifiedTime(path));
System.out.println("文件拥有者: " + Files.getOwner(path));
System.out.println("文件类型: " + Files.probeContentType(path));
System.out.println("获取文件名: " + path.getFileName());
System.out.println("获取父目录: " + path.getParent());
System.out.println("获取根目录: " + path.getRoot());
}
Files.write + Files.readAllLines/Files.readAllBytes : 简单读写
private static void readWriteFile() throws IOException {
Path path = Paths.get("0.txt");
String content = String.valueOf(System.currentTimeMillis());
System.out.println("write=" + content);
Files.write(path, content.getBytes(StandardCharsets.UTF_8));
content = "-"+ Integer.MAX_VALUE;
System.out.println("write append=" + content);
Files.write(path, content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
Stream<String> stringStream = Files.lines(path, StandardCharsets.UTF_8);
stringStream.forEach(System.out::println);
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
for (int i = 0; i < lines.size(); i++) {
System.out.println("[" + i + "]" + lines.get(i));
}
byte[] allBytes = Files.readAllBytes(path);
System.out.println(new String(allBytes, StandardCharsets.UTF_8));
}
Files.newBufferedWriter + Files.newBufferedReader : 缓冲区读写
private static void writeFile1() {
String s = LocalTime.now().toString();
System.out.println("write=" + s);
Path path = Paths.get("1.txt");
try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
writer.write(s, 0, s.length());
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
}
try (BufferedReader bufferedReader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println("read=" + line);
}
} catch (IOException e) {
System.err.format("IOException: %s%n", e);
}
}
private static void writeFile2() {
String s = LocalDate.now().toString();
System.out.println("write=" + s);
Path path = Paths.get("2.txt");
BufferedWriter writer = null;
try {
writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8);
writer.write(s, 0, s.length());
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
try {
BufferedReader bufferedReader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println("read=" + line);
}
bufferedReader.close();
} catch (IOException e) {
System.err.format("IOException: %s%n", e);
}
}
Files.newOutputStream + Files.newInputStream : 输入输出流
private static void writeFile3() {
String s = LocalDateTime.now().toString();
System.out.println("write=" + s);
Path path = Paths.get("3.txt");
try (OutputStream outputStream = Files.newOutputStream(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream))) {
writer.write(s);
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
}
try (InputStream inputStream = Files.newInputStream(path, StandardOpenOption.READ);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println("read=" + line);
}
} catch (Exception e) {
System.err.format("IOException: %s%n", e);
}
}
demo
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.stream.Stream;
public class Test2 {
public static void main(String[] args) throws IOException {
info();
readWriteFile();
writeFile1();
System.out.println("====================1=====================");
writeFile2();
System.out.println("====================2=====================");
writeFile3();
System.out.println("====================3=====================");
}
private static void writeFile1() {
String s = LocalTime.now().toString();
System.out.println("write=" + s);
Path path = Paths.get("1.txt");
try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
writer.write(s, 0, s.length());
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
}
try (BufferedReader bufferedReader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println("read=" + line);
}
} catch (IOException e) {
System.err.format("IOException: %s%n", e);
}
}
private static void writeFile2() {
String s = LocalDate.now().toString();
System.out.println("write=" + s);
Path path = Paths.get("2.txt");
BufferedWriter writer = null;
try {
writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8);
writer.write(s, 0, s.length());
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
try {
BufferedReader bufferedReader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println("read=" + line);
}
bufferedReader.close();
} catch (IOException e) {
System.err.format("IOException: %s%n", e);
}
}
private static void writeFile3() {
String s = LocalDateTime.now().toString();
System.out.println("write=" + s);
Path path = Paths.get("3.txt");
try (OutputStream outputStream = Files.newOutputStream(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream))) {
writer.write(s);
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
}
try (InputStream inputStream = Files.newInputStream(path, StandardOpenOption.READ);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println("read=" + line);
}
} catch (Exception e) {
System.err.format("IOException: %s%n", e);
}
}
private static void readWriteFile() throws IOException {
Path path = Paths.get("0.txt");
String content = String.valueOf(System.currentTimeMillis());
System.out.println("write=" + content);
Files.write(path, content.getBytes(StandardCharsets.UTF_8));
content = "-"+ Integer.MAX_VALUE;
System.out.println("write append=" + content);
Files.write(path, content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
Stream<String> stringStream = Files.lines(path, StandardCharsets.UTF_8);
stringStream.forEach(System.out::println);
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
for (int i = 0; i < lines.size(); i++) {
System.out.println("[" + i + "]" + lines.get(i));
}
byte[] allBytes = Files.readAllBytes(path);
System.out.println(new String(allBytes, StandardCharsets.UTF_8));
}
private static void info() throws IOException {
Path path = Paths.get("src\\com\\xq\\demo\\java8\\Test1.java").toAbsolutePath();
System.out.println(path);
System.out.println("文件是否存在: " + Files.exists(path));
System.out.println("是否是目录: " + Files.isDirectory(path));
System.out.println("是否是可执行文件: " + Files.isExecutable(path));
System.out.println("是否可读: " + Files.isReadable(path));
System.out.println("判断是否是一个文件: " + Files.isRegularFile(path));
System.out.println("是否可写: " + Files.isWritable(path));
System.out.println("文件是否不存在: " + Files.notExists(path));
System.out.println("文件是否隐藏: " + Files.isHidden(path));
System.out.println("文件大小: " + Files.size(path));
System.out.println("文件存储在SSD还是HDD: " + Files.getFileStore(path));
System.out.println("文件修改时间:" + Files.getLastModifiedTime(path));
System.out.println("文件拥有者: " + Files.getOwner(path));
System.out.println("文件类型: " + Files.probeContentType(path));
System.out.println("获取文件名: " + path.getFileName());
System.out.println("获取父目录: " + path.getParent());
System.out.println("获取根目录: " + path.getRoot());
}
}