1.文本文件I/O
1.1输出
常用文本文件输出类和构造器
类名/构造器 | 解释 |
PrintWriter(File file) | 创建一个指定文件的具有文本输出功能的对象 |
PrintWriter(Writer writer) | 创建一个指定具有文本输出功能的对象 |
PrintWriter(Writer writer, boolean flush) | 创建一个指定具有冲刷选项且具有文本输出功能的对象 |
BufferedWriter(Writer writer) | 创建一个文本缓冲输出封装对象 |
FileWriter(File file) | 创建一个指定文件的文本输出对象 |
FileWriter(File file, boolean append) | 创建一个指定文件,可以将文本数据添加到文末的对象 |
FileWriter(String fileName, boolean append) | 创建一个指定文件名,可以将文本数据添加到文末的对象 |
PrintWriter类常用方法
方法名 | 解释 |
print(argument) | 将指定参数写到文本文件 |
println(argument) | 将指定参数写到文本文件,换行 |
flush() | 冲刷缓冲器 |
close() | 关闭文件 |
具体实例
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
public class PrintWriterTest {
public static void main(String[] args) throws IOException {
File file = new File("E:\\test.txt");
PrintWriter out = new PrintWriter(file);
out.println("This line will be written to the file");
out.print("hello world");
out.flush();
out.close();
}
}
将上例改为缓冲文本文件输出
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class PrintWriterTest {
public static void main(String[] args) throws IOException {
File file = new File("E:\\test.txt");
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter(file)));
out.println("This line will be written to the file");
out.print("hello world");
out.flush();
out.close();
}
}
运行发现, 第一次结果被覆盖,我们可以将文本输出设置为结尾添加形式
File file = new File("E:\\test.txt");
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter(file, true)));
2.Scanner扫描器
Scanner构造器和常用方法
构造器/方法 | 解释 |
Scanner(File file) | 按指定文件创建一个扫描器 |
Scanner(InputStream source) | 按指定输入流创建一个扫描器 |
close() | 关闭扫描器 |
nexLine()、nexBoolean()、nexByte()、nexDouble()、nexInt()、nexFloat()、nexLong()、 | 返回一个指定的读入数据 |
hasNexLine()、hasNexBoolean()、hasNexByte()、hasNexDouble()、hasNexInt()、hasNexFloat()、hasNexLong()、 | 如果扫描器的缓冲中有指定类型数据则返回真,否则返回假 |
具体实例
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ScannerTest {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("E:\\test.txt");
Scanner sc = new Scanner(file);
while(sc.hasNextLine()) {
String line = sc.nextLine();
System.out.println(line);
}
sc.close();
}
}
3.Files和Path/Paths
Path具体实例
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathTest {
public static void main(String[] args) {
Path path = Paths.get("E:\\java");
System.out.println(path.toString());
System.out.println(path.getNameCount());
System.out.println(path.getFileName());
System.out.println(path.getFileSystem());
System.out.println(path.toFile());
System.out.println(path.endsWith(".txt"));
System.out.println(path.isAbsolute());
System.out.println(path.getRoot());
System.out.println(path.toUri());
System.out.println(path.getParent());
}
}
Files和Path的应用-获取文件夹下文件、文件树
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class FilesTest {
public static void main(String[] args) throws IOException {
//获取文件树下的所有文件
Path pathT = Paths.get("E:\\");
Stream<Path> paths = Files.walk(pathT);//tree
paths.forEach(p -> {
System.out.println(p.getFileName());
System.out.println(p.getNameCount());
System.out.println(p.getParent());
System.out.println(p.getRoot());
});
//获取当前路径下的所有文件
Path pathL = Paths.get("E:\\Java\\jdk-10");
Stream<Path> list = Files.list(pathL);
list.forEach(p -> {
System.out.println(p.getFileName());
System.out.println(p.getParent());
System.out.println(p.getRoot());
});
paths.close();
list.close();
}
}
查询指定格式文件
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class FilesTest {
public static void main(String[] args) throws IOException {
Path pathT = Paths.get("E:\\Eclipse\\Java练习");
Stream<Path> paths = Files.walk(pathT);//tree
paths.forEach(p -> {
if(p.toString().endsWith(".java")) {
System.out.println(p.getFileName());
}
});
paths.close();
}
}