实战演练
1. 练前热身:
- 概念:
File类:文件和目录路径名的抽象形式。
- 文件的创建方法:
public class FileController {
public static void fileTest() throws IOException {
// File f1 = new File("D://备份//test4//demo//mo");
File f = new File("D://备份//test4");
if (!f.exists()){
//以多重目录创建
// f1.mkdirs();
//创建单层目录
// f.mkdir();
//以文件形式创建
f.createNewFile();
}
File f2 = new File("D://备份//test");
//父路径
String parent = f2.getParent();
System.out.println(parent);
//打印文件绝对路径
System.out.println("绝对路径:"+f2.getAbsolutePath());
}
public static void main(String[] args) throws IOException {
fileTest();
}
}
2.练习题目
题目一:
文件复制,将文件复制到新的文件里,一边读一边写。
代码展示:
public static void fileCopy() {
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
try {
//读取文件内容
inputStream = new FileInputStream("C:\\Users\\Administrator\\Downloads\\Video\\f0.mp4");
outputStream = new FileOutputStream("D:\\备份\\f2.mp4", true);
byte[] bytes = new byte[1024];
int readCount = 0;
while ((readCount = inputStream.read(bytes)) != -1) {
//写入
outputStream.write(bytes, 0, readCount);
}
//刷新
outputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//不能同时try,会影响到下一个流的关闭
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream == null) {
//关闭流
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
题目二:
目录copy,将文件或者文件夹从当前目录复制到新的目录下。
代码展示:
/**
* 目录拷贝
*/
public static void copyMkdir(File targetPath, File resourcePath) {
if (resourcePath == null) {
return;
}
if (resourcePath.isFile()) {
String copyTarfPath = targetPath.getAbsolutePath().concat(resourcePath.getAbsolutePath().substring(3));
File file = new File(copyTarfPath);
File parentFile = file.getParentFile();
//当前文件下是文件,需要判断父目录是否存在,不存在新建
if (!parentFile.exists()) {
parentFile.mkdirs();
}
//如果是文件进行读写操作
fileContentInput(file, resourcePath);
return;
}
File[] reFiles = resourcePath.listFiles();
for (File reFile : reFiles) {
//如果是目录
if (reFile.isDirectory()) {
//给目标目录建立相应的文件夹
String tarPath = targetPath.getAbsolutePath();
//目标文本的目录
String copyTarfPath = tarPath.concat(reFile.getAbsolutePath().substring(3));
System.out.println(copyTarfPath);
//判断目标目录是否存在
File tarFileDir = new File(copyTarfPath);
if (!tarFileDir.exists()) {
tarFileDir.mkdirs();
}
}
//递归调用
copyMkdir(targetPath, reFile);
}
}
public static void fileContentInput(File tarFilePath, File reFilePath) {
//定义输出流
FileOutputStream out = null;
//定义输入流
FileInputStream ios = null;
byte[] bytes = new byte[1024 * 1024]; //每次读取1兆
int readCount = 0;
try {
ios = new FileInputStream(reFilePath);
out = new FileOutputStream(tarFilePath);
if ((readCount = ios.read(bytes)) != -1) {
//一边读一边写
out.write(bytes, 0, readCount);
}
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ios != null) {
try {
ios.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws IOException {
File targetPath = new File("C://");
File resourcePath = new File("D://备份");
copyMkdir(targetPath, resourcePath);
}