文章目录:
1、认识文件
2、文件的分类
3、树型结构组织和目录
4、文件路径(Path)
5、Java 中文件系统相关的操作
6、java 中文件内容的读写 —— 数据流
为什么被称为流?
文件的内容(相关下面的读操作,写操作会覆盖原本的内容):
6.1、字节流—读:InputStream
代码优化改进(对异常处理和关闭资源处进行优化):
还可以进行优化(使用缓存数组):
6.2、字节流—写:OutputStream
还可以一次写入多个字节(利用缓存数组)
6.3、字符流—读:Read
6.4、字符流—写:Writer
7、针对文件操作的练习
7.1、扫描文件根据用户输入的文件名进行删除,删除需遵守用户的选择
①、代码解析:
②、详细代码:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-04-07
* Time: 12:29
*/
public class Demo10 {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入路径");
String root = scanner.next();
System.out.println("请输入需要删除的文件");
String deleteFile = scanner.next();
File file = new File(root);
if(!file.isDirectory()){
System.out.println("输入的为非法路径");
return;
}
scanner(file,deleteFile);
}
private static void scanner(File rootFile,String deleteFile) throws IOException {
if(rootFile == null){
return;
}
File[] files = rootFile.listFiles();
for (File file:files) {
if(file.isFile()){
if(file.getName().contains(deleteFile)){
delete(file);
}
}else if(file.isDirectory()){
scanner(file,deleteFile);
}
}
}
private static void delete(File f) throws IOException {
System.out.println("需要删除的路径为:"+f.getCanonicalPath());
Scanner scanner = new Scanner(System.in);
System.out.println("进行确认是否要删除:");
String choose = scanner.next();
if(choose.equals("√")){
f.delete();
System.out.println("删除成功");
}else {
System.out.println("退出删除");
}
}
}
③、执行效果:
7.2、进行普通文件的复制
①、代码解析:
②、详细代码:
import java.io.*;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-04-07
* Time: 12:40
*/
public class Demo11 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要拷贝的源路径");
String rootFile = scanner.next();
System.out.println("请输入要拷贝的目标路径");
String cpy = scanner.next();
File file = new File(rootFile);
if(!file.isFile()){
System.out.println("输入的路径非法");
return;
}
try (InputStream inputStream = new FileInputStream(rootFile)){
try (OutputStream outputStream = new FileOutputStream(cpy)){
while(true){
byte[] buffer = new byte[1024];
int len = inputStream.read(buffer);
if(len == -1){
break;
}
outputStream.write(buffer,0,len);
}
}
}catch (IOException e){
e.printStackTrace();
}
}
}
③、执行效果:
7.3、扫描指定目录,并找到名称或者内容中包含指定字符的所有普通文件(不包含目录)
①、代码解析:
②、详细代码:
import java.io.*;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-04-07
* Time: 12:50
*/
public class Demo12 {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入源文件路径");
String rootFile = scanner.next();
System.out.println("请输入需要查找的信息");
String str = scanner.next();
File file = new File(rootFile);
if(!file.isDirectory()){
System.out.println("路径非法");
return;
}
scanner(file,str);
}
private static void scanner(File file,String str) throws IOException {
if(file == null){
return;
}
File[] files = file.listFiles();
for (File file1:files) {
if(file1.isFile()){
if(containStr(file1,str)){
System.out.println("查找到该文件位于:"+file1.getCanonicalPath());
}
}else if(file1.isDirectory()){
scanner(file1,str);
}
}
}
private static boolean containStr(File file,String str){
StringBuffer stringBuffer = new StringBuffer();
try(Reader reader = new FileReader(file)){
char[] buffer = new char[1024];
while (true){
int len = reader.read(buffer);
if(len == -1){
break;
}
stringBuffer.append(buffer,0,len);
}
}catch (IOException e){
e.printStackTrace();
}
return stringBuffer.indexOf(str) != -1;
}
}