目录
File类
File类的重命名
方法名 | 描述 |
---|---|
public boolean renameTo(File dest) | 把文件重命名为指定的文件路径 |
注意事项:
- 如果路径名相同,那么就是重命名;
- 如果路径名不同,那么就是剪切。
package org.westos.demo;
import java.io.File;
import java.io.IOException;
/**
* @author lwj
* @date 2020/5/24 8:55
*/
public class MyTest {
public static void main(String[] args) throws IOException {
File file = new File("src\\org\\westos\\demo\\a.txt");
boolean newFile = file.createNewFile();
System.out.println(newFile);
//true
File file1 = new File("src\\org\\westos\\demo\\b.txt");
boolean b = file.renameTo(file1);
System.out.println(b);
//true
//a.txt被重命名为b.txt
}
}
File类的判断
方法名 | 描述 |
---|---|
public boolean isDirectory() | 判断是否是文件夹 |
public boolean isFile() | 判断是否是文件 |
public boolean exists() | 判断是否存在 |
public boolean canRead() | 判断是否可读 |
public boolean canWrite() | 判断是否可写 |
public boolean isHidden() | 判断是否隐藏 |
package org.westos.demo;
import java.io.File;
/**
* 判断
* @author lwj
* @date 2020/5/24 9:11
*/
public class MyTest2 {
public static void main(String[] args) {
File file = new File("src\\org\\westos\\demo\\b.txt");
System.out.println(file.isFile());
//true
File src = new File("src");
System.out.println(src.isDirectory());
//true
System.out.println(new File("src\\org\\westos\\demo\\a.txt").exists());
//false
}
}
File类的获取
方法名 | 描述 |
---|---|
public String getAbsolutePath() | 获取绝对路径,返回字符串类型 |
public File getAbsoluteFile() | 获取绝对路径,返回File类型 |
public String getParent() | 获取父路径,返回字符串类型 |
public File getParentFile() | 获取父路径,返回File类型 |
public String getPath() | 获取相对路径,返回字符串类型 |
public long getTotalSpace() | 获取磁盘总容量 |
public long getFreeSpace() | 获取磁盘剩余容量 |
public String name() | 获取文件名 |
public long length() | 获取文件大小 |
public long lastModified() | 获取最后一次修改时间 |
package org.westos.demo;
import java.io.File;
/**
* 获取
* @author lwj
* @date 2020/5/24 9:31
*/
public class MyTest3 {
public static void main(String[] args) {
File file = new File("src\\org\\westos\\demo\\b.txt");
System.out.println(file.getAbsolutePath());
//获取绝对路径,返回字符串类型
//E:\XiBuKaiYuan\JavaSEDirectory\0524\File类\src\org\westos\demo\b.txt
System.out.println(file.getAbsoluteFile());
//获取绝对路径,返回File类型
//E:\XiBuKaiYuan\JavaSEDirectory\0524\File类\src\org\westos\demo\b.txt
System.out.println(file.getParent());
//获取父目录,返回字符串,在封装File时,如果不存在父目录,返回null
//src\org\westos\demo
System.out.println(file.getParentFile());
//获取父目录,返回File类型,在封装File时,如果不存在父目录,返回null
//src\org\westos\demo
System.out.println(file.getPath());
//获取相对路径
//src\org\westos\demo\b.txt
System.out.println(file.getName());
//b.txt
}
}
package org.westos.demo2;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author lwj
* @date 2020/5/24 10:18
*/
public class MyTest3 {
public static void main(String[] args) {
File file = new File("E:");
System.out.println((double) file.getTotalSpace() / 1024 / 1024 / 1024);
//E盘总容量:99.99GB
System.out.println((double) file.getFreeSpace() / 1024 / 1024 / 1024);
//E盘剩余容量:98.22GB
File file1 = new File("src\\org\\westos\\demo\\b.txt");
System.out.println(file1.getName());
//获取文件名
//b.txt
System.out.println(file1.length());
//获取文件的大小
//18
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM--dd HH:mm:ss");
System.out.println(sdf.format(new Date(file1.lastModified())));
//获取文件的最后一次修改时间
//2020-05--24 10:24:21
}
}
方法名 | 描述 |
---|---|
public String[] list() | 获取指定目录下的所有文件或者文件夹的名称数组 |
public File[] listFiles() | 获取指定目录下的所有文件或者文件夹的File数组 |
package org.westos.demo2;
import java.io.File;
/**
* @author lwj
* @date 2020/5/24 10:32
*/
public class MyTest4 {
public static void main(String[] args) {
File file = new File("src\\org\\westos\\demo");
for (String s : file.list()) {
System.out.println(s);
}
/*
b.txt
MyTest.java
MyTest2.java
MyTest3.java
*/
for (File listFile : file.listFiles()) {
System.out.println(listFile);
}
/*
src\org\westos\demo\b.txt
src\org\westos\demo\MyTest.java
src\org\westos\demo\MyTest2.java
src\org\westos\demo\MyTest3.java
*/
}
}
输出指定目录下指定后缀的文件名
package org.westos.demo2;
import java.io.File;
/**
* 输出指定目录下指定后缀的文件名
* @author lwj
* @date 2020/5/24 9:47
*/
public class MyTest {
public static void main(String[] args) {
File file = new File("src\\org\\westos");
show(file);
}
/*
E:\XiBuKaiYuan\JavaSEDirectory\0524\File类\src\org\westos\demo\MyTest.java
E:\XiBuKaiYuan\JavaSEDirectory\0524\File类\src\org\westos\demo\MyTest2.java
E:\XiBuKaiYuan\JavaSEDirectory\0524\File类\src\org\westos\demo\MyTest3.java
E:\XiBuKaiYuan\JavaSEDirectory\0524\File类\src\org\westos\demo2\MyTest.java
E:\XiBuKaiYuan\JavaSEDirectory\0524\File类\src\org\westos\test\MyTest.java
Process finished with exit code 0
*/
public static void show(File file) {
if (file.isFile() && file.getName().endsWith(".java")) {
System.out.println(file.getAbsolutePath());
} else if (file.isDirectory()) {
for (File listFile : file.listFiles()) {
show(listFile);
}
}
}
}
文件名称过滤器
方法名 | 描述 |
---|---|
public File[] listFiles(FileFilter filter) | |
public File[] listFiles(FilenameFilter filter) |
返回指定目录下符合条件的文件
FilenameFilter
package org.westos.demo2;
import java.io.File;
import java.io.FilenameFilter;
/**
* @author lwj
* @date 2020/5/24 9:56
*/
public class MyTest2 {
public static void main(String[] args) {
File file = new File("src\\org\\westos\\demo");
show(file);
//src\org\westos\demo\MyTest.java
//src\org\westos\demo\MyTest2.java
//src\org\westos\demo\MyTest3.java
}
public static void show(File file) {
File[] files = file.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return new File(dir, name).isFile() && name.endsWith(".java");
}
});
for (File file1 : files) {
System.out.println(file1);
}
}
}
FileFilter
package org.westos.demo2;
import java.io.File;
import java.io.FileFilter;
/**
* @author lwj
* @date 2020/5/24 11:21
*/
public class MyTest5 {
public static void main(String[] args) {
File file = new File("src\\org\\westos\\demo");
show(file);
/*
src\org\westos\demo\MyTest.java
src\org\westos\demo\MyTest2.java
src\org\westos\demo\MyTest3.java
*/
}
public static void show(File file) {
File[] files = file.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
//子文件或者子文件夹对象
return pathname.isFile() && pathname.getName().endsWith(".java");
}
});
for (File file1 : files) {
System.out.println(file1);
}
}
}
删除非空文件夹
public File[] listFiles()
package org.westos.test;
import java.io.File;
/**
* 删除非空目录
* @author lwj
* @date 2020/5/24 9:17
*/
public class MyTest {
public static void main(String[] args) {
File file = new File("E:\\test");
delete(file);
}
public static void delete(File file) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File file1 : files) {
delete(file1);
}
}
file.delete();
//1、文件删除;2、空文件夹删除
}
}
public String[] list()
package org.westos.test;
import java.io.File;
/**
* @author lwj
* @date 2020/5/24 10:54
*/
public class MyTest2 {
public static void main(String[] args) {
File file = new File("E:\\test");
delete(file);
}
public static void delete(File file) {
if (file.isDirectory()) {
String[] list = file.list();
for (String s : list) {
File file1 = new File(file, s);
delete(file1);
}
}
file.delete();
}
}
IO流
IO概述
IO流用于处理设备之间的数据传输。
IO流分类:
1、按照数据流向
- 输入流:读入数据
- 输出流:写出数据
2、按照数据类型
- 字节流:可以读写任何类型的文件,比如视频、音频、文本文件
- 字符流:只能读写文本文件
IO流的基类:
- 字节流的抽象基类:InputStream、OutputStream
- 字符流的抽象基类:Reader、Writer
FileOutputStream
构造方法
FileOutputStream(File file)
FileOutputStream(File file, boolean append)
FileOutputStream(String name)
FileOutputStream(String name, boolean append)
public FileOutputStream(File file) throws FileNotFoundException {
this(file, false);
}
//默认不追加,即重新覆盖
一次写入一个字节
public void write(int b)
package org.westos.demo;
import java.io.*;
/**
* @author lwj
* @date 2020/5/24 13:32
*/
public class MyTest {
public static void main(String[] args) {
File file = new File("src\\org\\westos\\demo\\a.txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
//创建一个文件输出流关联一个文件,如果关联文件不存在,会自动帮你创建这个文件
fos.write('a');
//一次写入一个字节
fos.write('b');
fos.write('c');
fos.write('d');
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
一次写入一个字节数组
public void write(byte[] b)
package org.westos.demo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author lwj
* @date 2020/5/24 13:58
*/
public class MyTest2 {
public static void main(String[] args) {
File file = new File("src\\org\\westos\\demo\\a.txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
byte[] bytes = {'A', 'B', 'C', 'D'};
fos.write(bytes);
//一次写入一个字节数组
byte[] bytes1 = new String("哈哈哈").getBytes();
fos.write(bytes1);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
写入字节数组后,a.txt中的"abcd"被"ABCD哈哈哈"覆盖,因为FileOutputStream的构造方法不允许追加。
一次写入字节数组的一部分
public void write(byte[] b, int off, int len)
package org.westos.demo;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author lwj
* @date 2020/5/24 14:04
*/
public class MyTest3 {
public static void main(String[] args) {
File file = new File("src\\org\\westos\\demo\\a.txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
byte[] bytes = {'A', 'B', 'C', 'D'};
fos.write(bytes, 1, 3);
//一次写入一个字节数组
byte[] bytes1 = new String("哈哈哈").getBytes();
fos.write(bytes1, 3, 3);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
写入字节数组的一部分后,a.txt中的"ABCD哈哈哈"被"BCD哈"覆盖,因为FileOutputStream的构造方法不允许追加。
构造方法的append参数
package org.westos.demo;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author lwj
* @date 2020/5/24 14:31
*/
public class MyTest4 {
public static void main(String[] args) {
File file = new File("src\\org\\westos\\demo\\a.txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file, true);
//允许追加在文件的后面
byte[] bytes = {'A', 'B', 'C', 'D'};
fos.write(bytes, 1, 3);
//一次写入一个字节数组
byte[] bytes1 = new String("哈哈哈").getBytes();
fos.write(bytes1, 3, 3);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
写入字节数组的一部分后,a.txt中的"BCD哈"被"BCD哈"追加在后面,因为FileOutputStream的构造方法允许追加了。
a.txt
BCD哈BCD哈
FileOutputStream写出数据实现换行
package org.westos.demo;
import java.io.*;
/**
* @author lwj
* @date 2020/5/24 14:34
*/
public class MyTest5 {
public static void main(String[] args) {
File file = new File("src\\org\\westos\\demo\\b.txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file, true);
fos.write("IO流".getBytes());
fos.write("\r\n".getBytes());
fos.write("FileOutputStream文件输出流".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
b.txt
IO流
FileOutputStream文件输出流
FileInputStream
构造方法
FileInputStream(File file)
FileInputStream(String name)
一次读取一个字节
public int read()
//一次读取一个字节
demo2\a.txt
abcd
package org.westos.demo2;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* @author lwj
* @date 2020/5/24 14:48
*/
public class MyTest {
public static void main(String[] args) {
File file = new File("src\\org\\westos\\demo2\\a.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
int read = fis.read();
System.out.println((char) read);
//a
//如果读取不到有效的字节,那么返回-1
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
一次读取一个字节数组
int read(byte[] b)
//一次读取一个字节数组
package org.westos.demo2;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/**
* @author lwj
* @date 2020/5/24 14:55
*/
public class MyTest2 {
public static void main(String[] args) {
File file = new File("src\\org\\westos\\demo2\\a.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
byte[] bytes = new byte[5];
int len = fis.read(bytes);
//返回一次实际读取到的有效字节个数
System.out.println(len);
//4
String s = new String(bytes, 0, len);
/*
public String(byte bytes[], int offset, int length) {
*/
System.out.println(s);
//abcd
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
复制文件
package org.westos.demo3;
import java.io.*;
/**
* @author lwj
* @date 2020/5/24 15:11
*/
public class MyTest {
public static void main(String[] args) {
File source = new File("C:\\Users\\shawn\\Music\\MV\\G.E.M. 邓紫棋-差不多姑娘 (中国新说唱2019 第6期)(蓝光).mp4");
File dest = new File("邓紫棋-中国新说唱-差不多姑娘.mp4");
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(source);
fos = new FileOutputStream(dest);
byte[] bytes = new byte[1024];
int len = 0;
long start = System.currentTimeMillis();
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
fos.flush();
}
long end = System.currentTimeMillis();
System.out.println("共耗时:" + (end - start) / 1000 + "s");
//共耗时:2s
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
BufferedOutputStream 和 BufferedInputStream
BufferedOutputStream(OutputStream out)
BufferedOutputStream(OutputStream out, int size)
带缓冲区的字节输出流
package org.westos.demo4;
import java.io.*;
/**
* @author lwj
* @date 2020/5/24 16:05
*/
public class MyTest {
public static void main(String[] args) {
//BufferedOutputStream 带缓冲区的字节输出流
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(new File("C:\\Users\\shawn\\Music\\VipSongsDownload\\周杰伦 - 简单爱.qmcflac")));
bos = new BufferedOutputStream(new FileOutputStream(new File("周杰伦-简单爱.mp3")));
byte[] bytes = new byte[1024 * 8];
int len = 0;
long start = System.currentTimeMillis();
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
}
long end = System.currentTimeMillis();
System.out.println("共消耗:" + (end - start) / 1000 + "s");
//共消耗:0s
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
复制多级文件夹
package org.westos.test;
import java.io.*;
/**
* @author lwj
* @date 2020/5/24 16:39
*/
public class MyTest {
public static void main(String[] args) {
File source = new File("C:\\Users\\shawn\\Music");
File dest = new File("E:\\音乐");
show(source, dest);
}
public static void show(File source, File dest) {
if (source.isDirectory()) {
for (File file : source.listFiles()) {
File newDest = new File(dest, file.getName());;
if (file.isDirectory()) {
newDest.mkdir();
}
show(file, newDest);
}
} else {
copyFile(source, dest);
}
}
public static void copyFile(File source, File dest) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(source));
bos = new BufferedOutputStream(new FileOutputStream(dest));
byte[] bytes = new byte[1024 * 8];
int len = 0;
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}