读取/写入文本文件
// 1. 文本输入
// (1) 短小文本直接转入字符串
String string = new String(Files.readAllBytes(Paths.get("E:\\111.txt")), StandardCharsets.UTF_8);
System.out.println(string);
System.out.println();
// (2) 按行读取Files.readAllLines() -> List<String>
List<String> strings = Files.readAllLines(Paths.get("E:\\111.txt"), StandardCharsets.UTF_8);
System.out.println(strings);
System.out.println();
// (3) 文件太大可以转换为Files.lines() -> Stream<String>
Stream<String> lines = Files.lines(Paths.get("E:\\111.txt"), StandardCharsets.UTF_8);
lines.forEach(n -> System.out.println(n));
System.out.println();
// (4) 常规方法 BufferedReader.readLine()
String line = null;
String total = "";
try (BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(new FileInputStream("E:\\111.txt"), StandardCharsets.UTF_8))) {
while ((line = bufferedReader.readLine()) != null) {
total = total + line + File.separator;
}
}
System.out.println(total);
System.out.println();
// (5) 常规方法->流 BufferedReader.lines()
try (BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(new FileInputStream("E:\\111.txt"), StandardCharsets.UTF_8))) {
Stream<String> lines2 = bufferedReader.lines();
lines2.forEach(n -> System.out.println(n));
}
// 2. 文本输出
// (1)常规输出 BufferedWriter->OutputStreamWriter->FileOutputStream
try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("E:\\222.txt")))) {
bw.write("文本输出");
bw.write("文本输出");
bw.write("文本输出");
}
// (2) 使用 FileWriter
try (FileWriter fileWriter = new FileWriter("E:\\222.txt")) {
fileWriter.write("文本输出");
fileWriter.write("文本输出");
fileWriter.write("文本输出");
}
// (3) 使用 PrintWriter println()可以自动换行
try (PrintWriter printWriter = new PrintWriter("E:\\222.txt")) {
printWriter.write("文本输出");
printWriter.write("文本输出");
printWriter.write("文本输出");
printWriter.println("文本输出");
printWriter.println("文本输出");
printWriter.println("文本输出");
}
// 3. 从控制台读入
try(BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in))){
String tempLine = null;
while((tempLine=bufferedReader.readLine())!=null && !tempLine.contains("exit")){
System.out.println(tempLine);
}
}
// 4. 通过System.out传入文本
PrintStream defaultOut = System.out;
System.setOut(new PrintStream("E:\\333.txt"));
System.out.println("文本输出");
System.setOut(defaultOut);
// 5. 字符集
// (1) 默认字符集
System.out.println(Charset.defaultCharset());
// (2) 可用字符集
// SortedMap<String, Charset> availableCharsets =
// Charset.availableCharsets();
// availableCharsets.entrySet().forEach(n->System.out.println(n.getKey()+" "+n.getValue()));
// (3) 指定字符集
Charset utf_8 = StandardCharsets.UTF_8;
utf_8 = Charset.forName("UTF-8");
读取/写入二进制文件
// 6.写入二进制文件 try (DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream("E:\\111.data"))) { dataOutputStream.writeChars("二进制文本"); } System.out.println(); // 7.读取二进制文件 try (DataInputStream dataInputStream = new DataInputStream(new FileInputStream("E:\\111.data"))) { //available() 在不阻塞的情况下,可以获取的字节数 while (0 != dataInputStream.available()) { System.out.println(dataInputStream.readChar()); } }
随机文件访问
// 8. 随机文件访问
// (1) 二进制写入文件
try (DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream("E:\\222.data"))) {
writeFixedString("空白处", 20, dataOutputStream);
writeFixedString("随机文件访问1", 30, dataOutputStream);
writeFixedString("随机文件访问2", 40, dataOutputStream);
dataOutputStream.writeDouble(88.88);
dataOutputStream.writeInt(88);
}
// (2) 二进制读取文件
try (RandomAccessFile randomAccessFile = new RandomAccessFile("E:\\222.data", "r")) {
System.out.println(randomAccessFile.length());
// 中文占两个字节
//seek() 离开始跳出的字节数
randomAccessFile.seek(20 * 2);
//getFilePointer() 当前位置
System.out.println(randomAccessFile.getFilePointer());
System.out.println("readFixedString:" + readFixedString(30, randomAccessFile));
System.out.println("readFixedString:" + readFixedString(40, randomAccessFile));
System.out.println(randomAccessFile.readDouble());
System.out.println(randomAccessFile.readInt());
}
// (3) byte写入文件
try (RandomAccessFile randomAccessFile = new RandomAccessFile("E:\\333.data", "rw")) {
// 不能使用writeBytes(), writeBytes(b, 0, len) len是字符串的length;
// 而write()是bytes的length
// randomAccessFile.writeBytes(s);
randomAccessFile.write("随机文件访问2".getBytes());
randomAccessFile.write("随机文件访问sss3".getBytes());
}
// (4) byte读取文件
try (RandomAccessFile randomAccessFile = new RandomAccessFile("E:\\333.data", "rw")) {
int x = "随机文件访问2".getBytes().length;
byte[] b = new byte[x];
System.out.println(new String(b));
int y = "随机文件访问sss3".getBytes().length;
b = new byte[y];
randomAccessFile.read(b);
System.out.println(new String(b));
// 从指定位置读取文件的功能
b = new byte[1024];
int hasRead = 0;
while ((hasRead = randomAccessFile.read(b)) > 0) { //只要能够读出来,就一直读
System.out.print(new String(b, 0, hasRead));
}
}
// (5)在文档末尾加字符
try (RandomAccessFile randomAccessFile = new RandomAccessFile("E:\\111.txt", "rw")) {
//跳至文件末尾
randomAccessFile.seek(randomAccessFile.length());
randomAccessFile.writeBytes("The appended string");
}
System.out.println();
操作Zip文档
// 9. 操作Zip文档
//(1) 读取zip文档 ZipInputStream-->getNextEntry() ZipFile-->getInputStream()
try(InputStream in = new FileInputStream("E:\\111.zip");
ZipInputStream zipInputStream = new ZipInputStream(in);
ZipFile zipFile = new ZipFile("E:\\111.zip");){
ZipEntry entry = null;
while((entry = zipInputStream.getNextEntry()) != null){
try(InputStream inputStream = zipFile.getInputStream(entry);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader br = new BufferedReader(inputStreamReader);){
System.out.println("ZipEntry "+entry.getName());
br.lines().forEach(n->System.out.println(n));
}
zipInputStream.closeEntry();
}
}
//(2) 写入zip文档 ZipOutputStream-->putNextEntry
try(OutputStream outputStream= new FileOutputStream("E:\\222.zip");
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);){
zipOutputStream.putNextEntry(new ZipEntry("writeToZipFile1.txt"));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream (zipOutputStream) ;
bufferedOutputStream.write("writeToZipFile1.txt test中文".getBytes());
bufferedOutputStream.flush();
zipOutputStream.putNextEntry(new ZipEntry("writeToZipFile2.txt"));
bufferedOutputStream.write("writeToZipFile2.txt test中文".getBytes());
bufferedOutputStream.flush();
zipOutputStream.putNextEntry(new ZipEntry("writeToZipFile3.txt"));
bufferedOutputStream.write("writeToZipFile3.txt test中文".getBytes());
bufferedOutputStream.flush();
bufferedOutputStream.close();
}