一、实验目的
1.理解基于IO流文件操作的过程及意义
2.掌握基于字节数组缓冲区,实现IO操作方法
3.掌握基于NIO文件目录的创建方法
4.掌握基于NIO文件创建/删除的创建方法
二、实验内容
所有异常,全部在调用方法内捕获处理
将以下IOTest类的全部方法实现
package Test;
import java.util.*;
import java.math.*;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class Test {
public static void main(String[] args) throws Exception {
String fileName = "D:/example/from.txt";
System.out.println("----- 创建文件 ------");
createFile(fileName);
System.out.println("----- 将字符串写入文件 -------");
// \r\n在txt文本中换行
String str =
"白日依山尽\r\n" +
"黄河入海流\r\n" +
"欲穷千里目\r\n" +
"更上一层楼\r\n";
writeToFile(fileName,str);
System.out.println("--------- 基于基本IO流实现文件的复制 ----------");
String toFile = "D:/example/to.txt";
copyByIO(fileName, toFile);
System.out.println("--------- 基于NIO实现文件的复制 ----------");
String toFile2 = "D:/example/nio/to.txt";
copyByNIO(fileName, toFile2);
System.out.println("---------- 删除指定文件 -------------");
deleteFile(toFile);
System.out.println("---------- 遍历指定目录文件 -------------");
String dir = "D:/example";
walkDirectories(dir);
}
/*
* 基于指定文件名称创建目录及文件
* 如果文件已经存在,则忽略
*/
private static void createFile(String fileName) {
Path path=Paths.get(fileName);
try {
if(!Files.exists(path)) {
Files.createDirectories(path.getParent());
Files.createFile(path);
}
}
catch(Exception e){
e.printStackTrace();
}
}
/*
* 提示:文件以字节操作,因此可以
* 字符串,转字节数组,直接基于Files写入文件
*/
private static void writeToFile(String fileName, String content) throws Exception{
System.out.println(fileName);
FileOutputStream out=new FileOutputStream(fileName);
try {
byte[] bytes=content.getBytes();
out.write(bytes);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
out.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
/*
* 基于基本IO,以及字节数组缓冲区,复制文件
* 打印显示循环读写循环次数
*/
private static void copyByIO(String sourceFile, String targetFile)throws Exception {
createFile(targetFile);
int flag=0;
FileInputStream in=null;
FileOutputStream out=null;
try {
in=new FileInputStream(sourceFile);
out=new FileOutputStream(targetFile);
byte[] str=new byte[100];
in.read(str);
out.write(str);
flag++;
System.out.println("循环读写次数为:"+flag);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
in.close();
out.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
/*
* 基于NIO,实现文件的复制
*/
private static void copyByNIO(String sourceFile, String targetFile) throws Exception{
createFile(targetFile);
Path sourcePath=Paths.get(sourceFile);
Path targetPath=Paths.get(targetFile);
try {
Files.copy(sourcePath, targetPath,StandardCopyOption.REPLACE_EXISTING);
}
catch(Exception e){
e.printStackTrace();
}
}
/*
* 删除文件
*/
private static void deleteFile(String fileName) throws Exception{
Path path=Paths.get(fileName);
try {
Files.delete(path);
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 遍历打印指定目录下全部目录/文件名称
*/
private static void walkDirectories(String dir) throws Exception{
Path path=Paths.get(dir);
try {
Files.walk(path).forEach(s->System.out.println(s));
}
catch (Exception e) {
e.printStackTrace();
}
}
}