这是关于一个封装操作文件的一个类。
其中包括:
计算文件大小,拷贝文件夹以及图片,音频和视屏,合并文件,移动文件位置,删除文件等操作。(仅供参考)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.SequenceInputStream;
class HyjFileClass {
//1.获取文件大小,只需传一个文件路径的实参,返回值是文件大小(long类型),返回值只表示文件的字节大小。
/*
1.1.计算文件大小:long size = getFileSize(File file)
file:传入一个文件路径,计算文件夹中所有的文件大小,
返回值是一个long类型的字节数。
*/
public long getFileSize(File file){
long fileSize = 0;
File[] files = file.listFiles();
if (file.isFile()) {
return file.length();
}
if (file != null) {
for (File file2 : files) {
fileSize += getFileSize(file2);
}
}
return fileSize;
}
//2.把一个文件夹拷贝到另一个文件夹里---------------------
/*
2.1拷贝文件夹:getCopyFile(File filePath,File newFilePath)
filePath: 需要拷贝的文件路径,
newFilePath: 存放文件的路径
使用时只需要传入要拷贝的路径和一个存放的路径就可以了。
*/
public void getCopyFile(File filePath,File newFilePath) throws IOException{
File[] files = filePath.listFiles();
File tempFile = null;
if (!(newFilePath.isDirectory())) {
//创建目录
newFilePath.mkdir();
}
if (filePath != null) {
for (File file : files) {
if (file.isDirectory()) {
String string = file.getName();
tempFile = new File(newFilePath+"\\"+string);
tempFile.mkdir();
getCopyFile(file, tempFile);
}else if (file.isFile()) {
String string = file.getName();
tempFile = new File(newFilePath+"\\"+string);
tempFile.createNewFile();
getCopyFileContent(file,tempFile);
}
}
}
}
//3.拷贝图片,视频,音频等不需解码的文件----------------------------
/*
3.1字节流拷贝图片,视频,音频等文件:getCopyFileContent(File file,File file2)
file: 要拷贝的文件路径
file2: 存放路径
字节流适用于拷贝图片,视频,音频等一些不需要解码的文件,
使用时只需要传入要拷贝的路径和一个存放的路径就可以了。
*/
public void getCopyFileContent(File file,File file2) throws IOException{
FileInputStream inputStream = new FileInputStream(file);
FileOutputStream outputStream = new FileOutputStream(file2);
byte[] b = new byte[1024];
int count = 0;
while ((count = inputStream.read(b)) != -1) {
outputStream.write(b,0,count);
}
inputStream.close();
outputStream.close();
}
//4.用字符流来拷贝,当用此方法来拷贝图片,视频,音频等文件时,这些文件将不能被打开。---
/*
4.1.字符流拷贝文件:getFileRead(File file , File file2)
file: 要拷贝的文件路径
file2: 存放路径
字符流适用于拷贝一些需要解码操作的文件,但是不能实用字符流来拷贝图片,音频,
视频等。使用字符流可能会导致文件丢失。而不能正常打开。
*/
public void getFileRead(File file , File file2) throws IOException {
FileReader fileReader = new FileReader(file);
FileWriter fileWriter = new FileWriter(file2);
char[] b = new char[1024];
int count = 0;
while ((count = fileReader.read(b)) != -1) {
fileWriter.write(b,0,count);
}
fileWriter.close();
fileReader.close();
}
//5.合并文件---------------------------------------
/*
5.1.MergeFile(File file , File file2,File file3):合并两个文件
file: 需要合并的文件路径
file2: 需要合并的文件路径
file3: 合并之后的文件路径
该方法可以实现两个文件的拼接,并且返回一个新的文件。
*/
public void MergeFile(File file , File file2, File file3) throws IOException {
FileInputStream fileInputStream = new FileInputStream(file);
FileInputStream fileInputStream2 = new FileInputStream(file2);
FileOutputStream outputStream = new FileOutputStream(file3);
SequenceInputStream inputStream = new SequenceInputStream(fileInputStream, fileInputStream2);
byte[] b = new byte[1024];
while (inputStream.read(b) != -1) {
outputStream.write(b);
}
outputStream.close();
inputStream.close();
}
//6.把文件移动到新的路径下 file:要移动的路径 file2:接收移动的路径---------
/*
6.1.MoveFileToNewPath(File file, File file2):把目标文件移动到指定的路径下
file: 需要移动的文件路径
file2: 接收移动过来的文件路径
该方法可以把一个文件夹移动到指定的路径下,并且删除原来的文件。
*/
网友评论