目录
一、File
1.1 观察get系列特点差异
1.2 创建文件
1.3.1 delete()删除文件
1.3.2 deleteOnExit()删除文件
1.4 mkdir 与 mkdirs的区别
1.5 文件重命名
二、文件内容的读写----数据流
1.1 InputStream
1.1.1 使用 read() 读取文件
1.2 OutputStream
1.3 代码演示
1.3.1 扫描指定⽬录,并找到名称中包含指定字符的所有普通⽂件(不包含⽬录),并且后续询问⽤⼾是否 要删除该⽂件
1.3.2 进⾏普通⽂件的复制
1.3.3 扫描指定⽬录,并找到名称或者内容中包含指定字符的所有普通⽂件(不包含⽬录)
一、File
(1)属性
(2)构造方法
在Windows操作平台上,通常使用第二种构造方法
(3)方法
1.1 观察get系列特点差异
import java.io.File;
import java.io.IOException;
public class Demo4 {
public static void main(String[] args) throws IOException {
File file = new File("C:./test");
System.out.println(file.getName());//文件名字
System.out.println(file.getParent());//父目录文件路径
System.out.println(file.getPath());//文件路径
System.out.println(file.getAbsolutePath());//绝对路径
System.out.println(file.getCanonicalPath());//修饰过的绝对路径
}
}
由于在 Java 中 “\” 有转义字符 的功能,所以我们输入路径的时候需要多加一个“\”。上面演示了五种方法。
1.2 创建文件
public class Demo5 {
public static void main(String[] args) throws IOException {
File file = new File("./test");
boolean ok = file.isFile();
System.out.println(ok);
file.createNewFile();
boolean ok1 = file.isFile();
System.out.println(ok1);
}
}
1.3.1 delete()删除文件
public class Demo5 {
public static void main(String[] args) throws IOException {
File file = new File("./test");
file.createNewFile();
boolean ok = file.isFile();
System.out.println(ok);
file.delete();
boolean ok2 = file.isFile();
System.out.println(ok2);
}
}
可以见到,基础的操作并不难,我们可以通过file.createNewFile()来创建新文件,也可以通过file.delete()来删除文件。
1.3.2 deleteOnExit()删除文件
public class Demo5 {
public static void main(String[] args) throws IOException {
File file = new File("./test");
file.createNewFile();
boolean ok = file.isFile();
System.out.println(ok);
file.deleteOnExit();
System.out.println("删除完成");
System.out.println(file.exists());
Scanner scanner = new Scanner(System.in);
scanner.next();
}
}
可以看到我们掉用户 scanner 函数阻塞进程,掉用 deleteOnExit() 之后,文件并没有立即删除,只有当我们结束进程的之后才能删除。
1.4 mkdir 与 mkdirs的区别
import java.io.File;
import java.io.IOException;
public class Demo6 {
public static void main(String[] args) throws IOException {
File dir = new File("some-parent\\some-dir"); // some-parent 和 so
System.out.println(dir.isDirectory());
System.out.println(dir.isFile());
System.out.println(dir.mkdir());
System.out.println(dir.isDirectory());
System.out.println(dir.isFile());
}
}
import java.io.File;
import java.io.IOException;
public class Demo7 {
public static void main(String[] args) throws IOException {
File dir = new File("some-parent\\some-dir"); // some-parent 和 so
System.out.println(dir.isDirectory());
System.out.println(dir.isFile());
System.out.println(dir.mkdirs());
System.out.println(dir.isDirectory());
System.out.println(dir.isFile());
System.out.println(dir.getAbsolutePath());
}
}
由此可见,mkdirs对比与mkdir来说,mkdirs可以创建中间不存在的目录。
1.5 文件重命名
import java.io.File;
import java.io.IOException;
public class Demo8 {
public static void main(String[] args) throws IOException {
File file = new File("./some-parent");
File dest = new File("./some-dir");
System.out.println(file.getCanonicalPath());
System.out.println(dest.exists());
System.out.println(file.renameTo(dest));
System.out.println(dest.getCanonicalPath());
System.out.println(dest.exists());
}
}
import java.io.File;
import java.io.IOException;
public class Demo8 {
public static void main(String[] args) throws IOException {
File file = new File("./some-parent/some-dir");
File dest = new File("./some-dir");
System.out.println(file.getCanonicalPath());
System.out.println(dest.exists());
System.out.println(file.renameTo(dest));
System.out.println(dest.getCanonicalPath());
System.out.println(dest.exists());
}
}
可以看到,文件重命名也可以浅显的理解为,文件路径移动
二、文件内容的读写----数据流
上述我们只学会了 操作文件 ,对于操作文件内容,我们则需要用到数据流相关知识。Reader读取出来的是char数组或者String ,InputStream读取出来的是byte数组。
1.1 InputStream
InputStream 只是⼀个抽象类,要使⽤还需要具体的实现类。关于 InputStream 的实现类有很多,对于文件的读写 ,我们只需要关心 FIleInputStream,FileInputStream()里可以是 文件路径 或者 文件,下面是两种输出方式的演示。
1.1.1 使用 read() 读取文件
public class Demo9 {
public static void main(String[] args) {
try(InputStream inputStream = new FileInputStream("./test.txt")){
while(true){
int n = inputStream.read();
if(n == -1){
break;
}
System.out.printf("%c",n);
}
}catch (IOException e){
e.printStackTrace();
}
}
}
public class Demo10 {
public static void main(String[] args) {
try(InputStream inputStream = new FileInputStream("./test.txt")){
byte[] bytes = new byte[21024];
while (true){
int n = inputStream.read(bytes);
if(n == -1){
break;
}
for (int i = 0; i <n ; i++) {
System.out.printf("%c",bytes[i]);
}
}
}catch(IOException e){
e.printStackTrace();
}
}
}
将⽂件完全读完的两种⽅式。相⽐较⽽⾔,后⼀种的 IO 次数更少,性能更好
1.2 OutputStream
OutputStream 同样只是⼀个抽象类,要使⽤还需要具体的实现类。对于文件的读写,我们只需要关注FileOutputStream,下面是两种输入方式的演示。
public class Demo11 {
public static void main(String[] args) {
try(OutputStream os = new FileOutputStream("./output.txt",true)){
os.write('h');
os.write('e');
os.write('l');
os.write('l');
os.write('o');
os.flush();
}catch(IOException e){
e.printStackTrace();
}
}
}
public class Demo12 {
public static void main(String[] args) {
try(OutputStream os = new FileOutputStream("./output.txt")){
byte[] buf = new byte[]{(byte) 'h',(byte) 'e',(byte) 'l',(byte) 'l',(byte) 'o'};
os.write(buf);
os.flush();
}catch(IOException e){
e.printStackTrace();
}
}
}
1.3 代码演示
1.3.1 扫描指定⽬录,并找到名称中包含指定字符的所有普通⽂件(不包含⽬录),并且后续询问⽤⼾是否 要删除该⽂件
mport java.io.File;
import java.util.Scanner;
public class Demo14 {
public static void main(String[] args) {
System.out.println("请输入想扫描的文件");
System.out.println("->");
Scanner scanner = new Scanner(System.in);
String rootPath = scanner.next();
File file = new File(rootPath);
System.out.println("请输入想删除的文件名字");;
System.out.println("->");
String key = scanner.next();
scan(file,key);
}
private static void scan(File file, String key) {
if(!file.isDirectory()){
System.out.println("输入的路径有误!");
return;
}
File[] files = file.listFiles();
for (File f : files){
if(f.isFile()){
if(f.getName().contains(key)){
doDelete(f,key);
}
}else {
scan(f,key);
}
}
}
private static void doDelete(File f,String k) {
System.out.println(f.getAbsolutePath()+"是否删除 Y/N");
Scanner scanner = new Scanner(System.in);
String key = scanner.next();
if(key.equals("Y")||key.equals("y")){
f.delete();
}
}
}
1.3.2 进⾏普通⽂件的复制
import java.io.*;
import java.util.Scanner;
public class Demo13 {
public static void main(String[] args) throws IOException {
System.out.println("请输入想复制的路径");
System.out.print("->");
Scanner scanner = new Scanner(System.in);
String rootPath = scanner.next();
System.out.println("请输入复制后的路径");
System.out.print("->");
String scrPath = scanner.next();
try(InputStream inputStream = new FileInputStream(rootPath);
OutputStream outputStream = new FileOutputStream(scrPath)){
byte[] buf = new byte[1024];
while(true){
int n = inputStream.read(buf);
if(n == -1){
break;
}
outputStream.write(buf);
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
1.3.3 扫描指定⽬录,并找到名称或者内容中包含指定字符的所有普通⽂件(不包含⽬录)
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Scanner;
public class Demo3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入想查询的路径");
System.out.print("->");
String rootPath = scanner.next();
File rootFile = new File(rootPath);
if(!rootFile.isDirectory()){
System.out.println("输入的文件路径有误!");
return;
}
System.out.println("请输入想查询的 关键字");
System.out.print("->");
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 == null|| files.length == 0){
return;
}
for (File f : files){
if(f.isFile()){
doSearch(f,key);
}else {
scan(f,key);
}
}
}
private static void doSearch(File f, String key) {
StringBuilder sb = new StringBuilder();
try(Reader reader = new FileReader(f)){
char[] chars = new char[1024];
while(true){
int n = reader.read(chars);
if(n == -1){
break;
}
String s = new String(chars,0,n);
sb.append(s);
}
}catch(IOException e){
e.printStackTrace();
}
if(sb.indexOf(key) == -1){
return;
}
System.out.println("找到目标文件" + f.getAbsolutePath());
}
}
=========================================================================
最后如果感觉对你有帮助的话,不如给博主来个三连,博主会继续加油的ヾ(◍°∇°◍)ノ゙