扫描指定⽬录,并找到名称中包含指定字符的所有普通⽂件(不包含⽬录),并且后续询问⽤⼾是否 要删除该⽂件
一个主要的操作就是需要扫描指定目录(递归)
files
数组中files
是空的,或者 files
数组长度为 0
,代表没有文件,则直接返回 files
数组 doDelete
public class Demo15 {
//递归目录的方法
private static void scan(File currentFile, String key) {
if(!currentFile.isDirectory()){
return;
}
File[] files = currentFile.listFiles();
if(files == null || files.length == 0){
return;
}
for(File f : files){
if(f.isFile()){
doDelete(f,key);
}else {
scan(f,key);
}
}
}
}
Y/N
进行选择Y
或y
,则将此文件删除private static void doDelete(File f, String key){
if(!f.getName().contains(key)){
return;
}
Scanner scanner = new Scanner(System.in);
System.out.println(f.getAbsolutePath()+"是否确定要删除 Y/N");
String choice = scanner.next();
if(choice.equals("Y") || choice.equals("y")) {
f.delete();
}
}
import java.io.File;
import java.util.Scanner;
public class Demo15 {
//递归目录的方法
private static void scan(File currentFile, String key) {
if(!currentFile.isDirectory()){
return;
}
File[] files = currentFile.listFiles();
if(files == null || files.length == 0){
return;
}
for(File f : files){
if(f.isFile()){
doDelete(f,key);
}else {
scan(f,key);
}
}
}
private static void doDelete(File f, String key){
if(!f.getName().contains(key)){
return;
}
Scanner scanner = new Scanner(System.in);
System.out.println(f.getAbsolutePath()+"是否确定要删除 Y/N");
String choice = scanner.next();
if(choice.equals("Y") || choice.equals("y")) {
f.delete();
}
}
public static void main(String[] args) {
System.out.println("请输入要搜索的路径:");
Scanner scanner = new Scanner(System.in);
String rootPath = scanner.next();
File rootFile = new File(rootPath);
if(!rootFile.isDirectory()){
System.out.println("输入的路径不存在");
return;
}
System.out.println("请输入要删除的文件名字的关键字:");
String key = scanner.next();
//进行递归查找
scan(rootFile,key);
}
}
进⾏普通⽂件的复制
把一个文件里面的每个字节都读出来,再写入另一个文件中
srcFile
对象dextFile
文件InputStream
进行读操作,OutputStream
进行写操作import java.io.*;
import java.util.Scanner;
public class Demo16 {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入源文件路径:");
String srcPath = scanner.next();
File srcFile = new File(srcPath);
if(!srcFile.isFile()){
System.out.println("源文件路径有误!");
return;
}
System.out.println("请输入目标文件路径");
String destPath = scanner.next();
File destFile = new File(destPath);
if(!destFile.getParentFile().isDirectory()) {
System.out.println("输入的目标文件路径有误!");
return;
}
try(InputStream inputStream = new FileInputStream(srcFile);
OutputStream outputStream = new FileOutputStream(destFile)){
while (true) {
byte[] buffer = new byte[1024];
int n = inputStream.read(buffer);
if(n == -1){
break;
}
outputStream.write(buffer,0,n);
}
}catch (IOException e){
e.printStackTrace();
}
}
}
try()
里面可以写多个对象,多个对象的构造过程使用 ; 分隔就可以了write(buffer)
,因为前面读操作不一定能把 buffer
填满,若直接写入 buffer
,就把没有用到的空间也写入了,不太合适 buffer
只填了 100
个空间,剩下 924
个空间都是 0
,写入就没有意义[0, n]
扫描指定⽬录,并找到名称或者内容中包含指定字符的所有普通⽂件(不包含⽬录) 注意:我们现在的⽅案性能较差,所以尽量不要在太复杂的⽬录下或者⼤⽂件下实验
import java.io.*;
import java.util.Scanner;
public class Demo17 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要搜索的路径:");
String rootPath = scanner.next();
File rootFile = new File(rootPath);
if(!rootFile.isDirectory()){
System.out.println("要搜索的路径有误!");
return;
}
System.out.println("请输入要搜索的查询词:");
String key = scanner.next();
//进行递归查找
scan(rootFile,key);
}
private static void scan(File rootFile, String key) {
if(!rootFile.isDirectory()) {
return;
}
File[] files = rootFile.listFiles();
if(files.length == 0 || files == null) {
return;
}
for(File f : files) {
if(f.isFile()){
//进行后续的查询操作
doSearch(f,key);
}else{
scan(f,key);
}
}
}
private static void doSearch(File f, String key) {
//打开文件,读取文件内容,并判定文件内容是否包含 key
StringBuilder stringBuilder = new StringBuilder();
//因为不考虑二进制,直接按照文本的方式来处理,就直接用 Reader
try(Reader reader = new FileReader(f)){
char[] buffer = new char[1024];
while(true) {
int n = reader.read(buffer);
if(n == -1){
break;
}
String s = new String(buffer,0,n);
stringBuilder.append(s);
}
}catch (IOException e){
e.printStackTrace();
}
if(stringBuilder.indexOf(key) == -1) {
//未找到
return;
}
//找到了
System.out.println("找到匹配的文件" + f.getAbsolutePath());
}
}
IO
操作,因为每次判定都要将硬盘里面的所有文件都读一遍。尤其是遇到硬盘上有些大的文件咱们搜索引擎中,进行搜索的过程,也就是在文件中查找内容是否被包含的过程
搜索出来的结果其实就是一些 HTML
文件,这些 HTML
文件里面一定是包含你的查询词(或者和你的查询词有关的)
搜索引擎每次搜索都是在数以十亿,数以百亿的 HTMl
中,找到几十万,几百万个结果
搜索引擎这样的场景,不能通过上述“遍历文件”方式实现
在未来实际工作中,也会用到一些“自定制的搜索引擎” 比如,我们自己的代码中,产生大量的日志,把这些日志导入到自己搭建的搜索引擎中,从而快速查找