欢迎光临
我们一直在努力

文件操作IO

1.扫描指定⽬录,并找到名称中包含指定字符的所有普通⽂件(不包含⽬录),并且后续询问⽤⼾是否 要删除该⽂件

package io;

import java.io.File;
import java.util.Scanner;

//实现简单的随文件的查找以及删除文件
public class Demo12 {
public static void main(String[] args) {
//1.让给用户通过控制台输入路径和指定的字符串
Scanner scanner = new Scanner(System.in);
System.out.println("强输入要扫描的路径:");
String basePath = scanner.next();
System.out.println("请输入要搜索的字符串:");
String word = scanner.next();

//2.对用户的输入进行检验,不能随意输入
//看一下basePath 是否合法
File baseFile = new File(basePath);
if(!baseFile.exists() || !baseFile.isDirectory()){
System.out.println("输入的路径有误!");
return;
}
if(word.isEmpty()){
System.out.println("输入的搜索字符串不能为空!");
return;
}

//3.单独创建一个方法,使用递归完成查找操作
scanDir(baseFile,word);
}
private static void scanDir(File baseFile, String word){
//1.列出baseFile下的所有文件
// 希望直接拿到File 对象 (判定文件是普通文件还是目录)
File[] files = baseFile.listFiles();
if(files == null || files.length == 0){
return;
}

//2.遍历数组
for(File f : files){
if(f.isFile()){
//针对文件名做判定
dealFile(f,word);
}else if(f.isDirectory()){
//进行递归操作
scanDir(f,word);
}
}
}
private static void dealFile(File f ,String word){
if(f.getName().contains(word)){
System.out.println("是否删除" + f.getAbsolutePath() + "?(Y/n");
Scanner scanner = new Scanner(System.in);
String choice = scanner.next();
if(choice.equals("Y") || choice.equals("y")){
f.delete();
}else{
//用户输入 或者 输入其他内容,都不触发删除操作
}
}
}
}

2.进⾏普通⽂件的复制

package io;

import java.io.*;
import java.util.Scanner;

public class Demo13 {
public static void main(String[] args) throws IOException {
//1.先让用户输入要复制的文件路径。
Scanner scanner = new Scanner(System.in);
System.out.println("前方输入要复制的源文件路径:");
String srcPath = scanner.next();
System.out.println("请输入要复制的目标文件路径:");
String destPath = scanner.next();

//2.对这两个路径做判定
File srcFile = new File(srcPath);
if(!srcFile.exists() || !srcFile.isFile()){
System.out.println("源文件不存在");
return;
}
File destFile = new File(destPath);
File destParentFile = destFile.getParentFile();
if(!destParentFile.exists() || !destParentFile.isDirectory()){
System.out.println("目标文件所在的目录不存在");
return;
}
//createNewFiLe()不写也行 因为下面的输出流会自动创建
if(!destFile.exists()){
destFile.createNewFile();
}

//3.进行真正的拷贝
//本质都需要打开两次文件
try(InputStream inputStream = new FileInputStream(srcFile);
OutputStream outputStream = new FileOutputStream(destFile)){
while(true){
//读取文件操作
byte[] bytes = new byte[1024];
int n = inputStream.read(bytes);
if(n == -1){
break;
}
//bytes 不一定是满的
outputStream.write(bytes,0,n);
}
}
}
}

3.扫描指定⽬录,并找到名称或者内容中包含指定字符的所有普通⽂件(不包含⽬录) 注意:我们现在的⽅案性能较差,所以尽量不要在太复杂的⽬录下或者⼤⽂件下实验

package io;

import java.io.*;
import java.util.Scanner;

public class Demo14 {
public static void main(String[] args) {
// 1. 输入要扫描的路径, 以及要搜索的关键词
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要扫描的路径:");
String basePath = scanner.next();
System.out.println("请输入要搜索的关键词:");
String word = scanner.next();

// 2. 对输入内容进行校验
File baseFile = new File(basePath);
if (!baseFile.exists() || !baseFile.isDirectory()) {
System.out.println("输入的路径不存在");
return;
}
if (word.isEmpty()) {
System.out.println("输入的搜索字符串不能为空");
return;
}

// 3. 递归的进行搜索了.
scanDir(baseFile, word);
}

private static void scanDir(File baseFile, String word) {
File[] files = baseFile.listFiles();
if (files == null || files.length == 0) {
return;
}
for (File file : files) {
System.out.println("当前搜索到文件: " + file.getAbsolutePath());
if (file.isFile()) {
dealFile(file, word);
} else if (file.isDirectory()) {
scanDir(file, word);
}
}
}

private static void dealFile(File file, String word) {
// 先判定文件名是否包含
if (file.getName().contains(word)) {
System.out.println("找到文件名匹配的结果: " + file.getAbsolutePath());
return;
}
// 判定文件内容是否包含.
StringBuilder content = new StringBuilder();
try (Reader reader = new FileReader(file)) {
while (true) {
char[] chars = new char[1024];
int n = reader.read(chars);
if (n == -1) {
break;
}
content.append(chars, 0, n);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
// 判定文件内容是否包含 word
if (content.indexOf(word) != -1) {
System.out.println("找到文件内容匹配的结果: " + file.getAbsolutePath());
}
}
}

赞(0)
未经允许不得转载:171主机测评 » 文件操作IO
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址