一.File类
二.扫描指定目录,并找到名称中包含指定字符的所有普通文件(不包含目录),并且后续询问用户是否要删除该文件
我的代码:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;public class Test1 {private static Scanner scanner = new Scanner(System.in);public static void main(String[] args) throws IOException {System.out.println("请输入目标目录:");File rootPath = new File(scanner.next());if (!rootPath.isDirectory()) {System.out.println("目标目录错误");return;}System.out.println("请输入关键字");String word = scanner.next();fingDir(rootPath, word);}private static void fingDir(File rootPath, String word) throws IOException {File[] files = rootPath.listFiles();if (files == null || files.length == 0) {return;}for (int i = 0; i < files.length; i++) {System.out.println(files[i].getCanonicalPath());if (files[i].isFile()) {delFile(files[i], word);} else {fingDir(files[i], word);}}}private static void delFile(File file, String word) {if (file.getName().contains(word)) {System.out.println("找到了,是否删除y/n");if (scanner.next().equals("y")) {file.delete();}}}
}
答案代码:
/*** 扫描指定目录,并找到名称中包含指定字符的所有普通文件(不包含目录),并且后续询问用户是否要删除该文件** @Author 比特就业课* @Date 2022-06-29*/
public class file_2445 {public static void main(String[] args) {// 接收用户输入的路径Scanner scanner = new Scanner(System.in);System.out.println("请输入要扫描的目录:");String rootPath = scanner.next();if (rootPath == null || rootPath.equals("")) {System.out.println("目录不能为空。");return;}// 根据目录创建File对象File rootDir = new File(rootPath);if (rootDir.isDirectory() == false) {System.out.println("输入的不是一个目录,请检查!");return;}// 接收查找条件System.out.println("请输入要找出文件名中含的字符串:");String token = scanner.next();// 用于存储符合条件的文件List<File> fileList = new ArrayList<>();// 开始查找scanDir(rootDir, token, fileList);// 处理查找结果if (fileList.size() == 0) {System.out.println("没有找到符合条件的文件。");return;}for (File file : fileList) {System.out.println("请问您要删除文件" + file.getAbsolutePath() + "吗?(y/n)");String order = scanner.next();if (order.equals("y")) {file.delete();}}}private static void scanDir(File rootDir, String token, List<File> fileList) {File[] files = rootDir.listFiles();if (files == null || files.length == 0) {return;}// 开始查找for (File file:files) {if (file.isDirectory()) {// 如果是一个目录就递归查找子目录scanDir(file, token, fileList);} else {// 如果是符合条件的文件就记录System.out.println(token);if (file.getName().contains(token)) {fileList.add(file.getAbsoluteFile());}}}}
}
三.进行普通文件的复制
我的代码:
import java.io.*;
import java.util.Scanner;public class Test2 {public static void main(String[] args) throws IOException {Scanner scanner = new Scanner(System.in);System.out.println("请输入目标文件的路径");File file1 = new File(scanner.next());if (!file1.isFile()) {System.out.println("目标文件错误");return;}System.out.println("请输入新文件的路径");File file2 = new File(scanner.next());if (!file2.getParentFile().isDirectory()) {System.out.println("新文件路径错误");return;}copyFile(file1, file2);}private static void copyFile(File file1, File file2) throws IOException {try(InputStream inputStream = new FileInputStream(file1);OutputStream outputStream = new FileOutputStream(file2)) {while (true) {byte[] buffer = new byte[2048];int n = inputStream.read(buffer);System.out.println(n);if (n == -1) {System.out.println("结束");break;} else {outputStream.write(buffer, 0, n);}}}}
}
/*** 进行普通文件的复制* * @Author 比特就业课* @Date 2022-06-29*/
public class File_2446 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);// 接收源文件目录System.out.println("请输入源文件的路径:");String sourcePath = scanner.next();if (sourcePath == null || sourcePath.equals("")) {System.out.println("源文路径不能为空。");return;}// 实例源文件File sourceFile = new File(sourcePath);// 校验合法性// 源文件不存在if (!sourceFile.exists()) {System.out.println("输入的源文件不存在,请检查。");return;}// 源路径对应的是一个目录if (sourceFile.isDirectory()) {System.out.println("输入的源文件是一个目录,请检查。");return;}// 输入目标路径System.out.println("请输入目标路径:");String destPath = scanner.next();if (destPath == null || destPath.equals("")) {System.out.println("目标路径不能为空。");return;}File destFile = new File(destPath);// 检查目标路径合法性// 已存在if (destFile.exists()) {// 是一个目录if (destFile.isDirectory()) {System.out.println("输入的目标路径是一个目录,请检查。");}// 是一个文件if (destFile.isFile()) {System.out.println("文件已存在,是否覆盖,y/n?");String input = scanner.next();if (input != null && input.toLowerCase().equals("")) {System.out.println("停止复制。");return;}}}// 复制过程InputStream inputStream = null;OutputStream outputStream = null;try {// 1. 读取源文件inputStream = new FileInputStream(sourceFile);// 2. 输出流outputStream = new FileOutputStream(destFile);// 定义一个缓冲区byte[] byes = new byte[1024];int length;while (true) {// 获取读取到的长度length = inputStream.read(byes);// 值为-1表示没有数据读出if (length == -1) {break;}// 把读到的length个字节写入到输出流outputStream.write(byes, 0, length);}// 将输出流中的数据写入文件outputStream.flush();System.out.println("复制成功。" + destFile.getAbsolutePath());} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {// 关闭输入流if (inputStream != null) {try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}// 关闭输出流if (outputStream != null) {try {outputStream.close();} catch (IOException e) {e.printStackTrace();}}}}
}
答案代码:
四. 扫描指定目录,并找到名称或者内容中包含指定字符的所有普通文件(不包含目录)
我的代码:
import java.io.*;
import java.util.Scanner;public class Test3 {private static Scanner scanner = new Scanner(System.in);public static void main(String[] args) throws IOException {System.out.println("请输入路径");File rootDir = new File(scanner.next());if (!rootDir.isDirectory()) {System.out.println("目录输入错误");return;}System.out.println("请输入名称");String word = scanner.next();findFile(rootDir, word);}private static void findFile(File rootDir, String word) throws IOException {File[] files = rootDir.listFiles();if (files == null || files.length == 0) {return;}for (File f : files) {if (f.isFile()) {isFind(f, word);} else {findFile(f, word);}}}private static void isFind(File f, String word) throws IOException {if (f.getName().contains(word)) {System.out.println("找到了" + f.getPath());} else {try (Reader reader = new FileReader(f)) {Scanner scanner1 = new Scanner(reader);String in = scanner1.nextLine();if (in.contains(word)) {System.out.println("找到了" + f.getPath());} else {return;}}}}
}
答案代码:
/*** 扫描指定目录,并找到名称或者内容中包含指定字符的所有普通文件(不包含目录)** @Author 比特就业课* @Date 2022-06-28*/
public class File_2447 {public static void main(String[] args) throws IOException {Scanner scanner = new Scanner(System.in);// 接收用户输入的路径System.out.println("请输入要扫描的路径:");String rootPath = scanner.next();// 校验路径合法性if (rootPath == null || rootPath.equals("")) {System.out.println("路径不能为空。");return;}// 根据输入的路径实例化文件对象File rootDir = new File(rootPath);if (rootDir.exists() == false) {System.out.println("指定的目录不存在,请检查。");return;}if (rootDir.isDirectory() == false) {System.out.println("指定的路径不是一个目录。请检查。");return;}// 接收要查找的关键字System.out.println("请输入要查找的关键字:");String token = scanner.next();if (token == null || token.equals("")) {System.out.println("查找的关键字不能为空,请检查。");return;}// 遍历目录查找符合条件的文件// 保存找到的文件List<File> fileList = new ArrayList<>();scanDir(rootDir, token, fileList);// 打印结果if (fileList.size() > 0) {System.out.println("共找到了 " + fileList.size() + "个文件:");for (File file: fileList) {System.out.println(file.getAbsoluteFile());}} else {System.out.println("没有找到相应的文件。");}}private static void scanDir(File rootDir, String token, List<File> fileList) throws IOException {// 获取目录下的所有文件File[] files = rootDir.listFiles();if (files == null || files.length == 0) {return;}// 遍历for (File file : files) {if (file.isDirectory()) {// 如果是文件就递归scanDir(file, token, fileList);} else {// 文件名是否包含if (file.getName().contains(token)) {fileList.add(file);} else {if (isContainContent(file, token)) {fileList.add(file.getAbsoluteFile());}}}}}private static boolean isContainContent(File file, String token) throws IOException {// 定义一个StringBuffer存储读取到的内容StringBuffer sb = new StringBuffer();// 输入流try (InputStream inputStream = new FileInputStream(file)) {try (Scanner scanner = new Scanner(inputStream, "UTF-8")) {// 读取每一行while (scanner.hasNext()) {// 一次读一行sb.append(scanner.nextLine());// 加入一行的结束符sb.append("\r\n");}}}return sb.indexOf(token) != -1;}
}