封裝關(guān)于操作文件的一個(gè)類(計(jì)算,拷貝,合并,移動,刪除等)

這是關(guān)于一個(gè)封裝操作文件的一個(gè)類。

其中包括:

計(jì)算文件大小,拷貝文件夾以及圖片,音頻和視屏,合并文件,移動文件位置,刪除文件等操作。(僅供參考)


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.獲取文件大小,只需傳一個(gè)文件路徑的實(shí)參,返回值是文件大小(long類型),返回值只表示文件的字節(jié)大小。

/* ??

? ? ? ? ? ? ? ? ? ?1.1.計(jì)算文件大小:long size = getFileSize(File file)

? ? ? ? ? ? ? ? ? ?file:傳入一個(gè)文件路徑,計(jì)算文件夾中所有的文件大小,

? ? ? ? ? ? ? ? ? ?返回值是一個(gè)long類型的字節(jié)數(shù)。

*/

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.把一個(gè)文件夾拷貝到另一個(gè)文件夾里---------------------

/*

? ? ? ? ? ? ? 2.1拷貝文件夾:getCopyFile(File filePath,File newFilePath)

? ? ? ? ? ? ? ? ? filePath: 需要拷貝的文件路徑,

? ? ? ? ? ? ? ? ? newFilePath: 存放文件的路徑

? ? ? ? ? ? ? ? ?使用時(shí)只需要傳入要拷貝的路徑和一個(gè)存放的路徑就可以了。

*/

public void getCopyFile(File filePath,File newFilePath) throws IOException{

? ? ? ? ? ? ? ?File[] files = filePath.listFiles();

? ? ? ? ? ? ? ?File tempFile = null;

? ? ? ? ? ? ? if (!(newFilePath.isDirectory())) {

? ? ? ? ? ? ? ? ? ? ? ? ? ?//創(chuàng)建目錄

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?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字節(jié)流拷貝圖片,視頻,音頻等文件:getCopyFileContent(File file,File file2)

? ? ? ? ? ? ? ? ? ?file: 要拷貝的文件路徑

? ? ? ? ? ? ? ? ? file2: 存放路徑

? ? ? ? ? ? ? ? ?字節(jié)流適用于拷貝圖片,視頻,音頻等一些不需要解碼的文件,

? ? ? ? ? ? ? ? ?使用時(shí)只需要傳入要拷貝的路徑和一個(gè)存放的路徑就可以了。

*/

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.用字符流來拷貝,當(dāng)用此方法來拷貝圖片,視頻,音頻等文件時(shí),這些文件將不能被打開。---

/*

? ? ? ? ? ? ?4.1.字符流拷貝文件:getFileRead(File file , File file2)

? ? ? ? ? ? ? ? ? file: 要拷貝的文件路徑

? ? ? ? ? ? ? ? ?file2: 存放路徑

? ? ? ? ? ? ? ? 字符流適用于拷貝一些需要解碼操作的文件,但是不能實(shí)用字符流來拷貝圖片,音頻,

? ? ? ? ? ? ? 視頻等。使用字符流可能會導(dǎo)致文件丟失。而不能正常打開。

*/

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):合并兩個(gè)文件

? ? ? ? ? ? ? ? ? ? ? ? ? file: 需要合并的文件路徑

? ? ? ? ? ? ? ? ? ? ? ? ?file2: 需要合并的文件路徑

? ? ? ? ? ? ? ? ? ? ? ? ?file3: 合并之后的文件路徑

? ? ? ? ? ? ? ? ? ? ? ? 該方法可以實(shí)現(xiàn)兩個(gè)文件的拼接,并且返回一個(gè)新的文件。

*/

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):把目標(biāo)文件移動到指定的路徑下

? ? ? ? ? ? ? ? ? ? ? ? ?file: 需要移動的文件路徑

? ? ? ? ? ? ? ? ? ? ? ? file2: 接收移動過來的文件路徑

? ? ? ? ? ? ? ? ? ? ? ? 該方法可以把一個(gè)文件夾移動到指定的路徑下,并且刪除原來的文件。

*/

public void MoveFileToNewPath(File file, File file2) throws IOException{

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?File file3 = new File(file2.getPath() + "\\" + file.getName());

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //把文件復(fù)制到指定的路徑上

? ? ? ? ? ? ? ? ? ? ? ? ? ? ?getCopyFile(file, file3);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //清空原來的文件夾并且刪除

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? getDeleteFile(file);

}

? ? ? ? ? ? ? ? ? ? ? ? ? ? //遞歸刪除整個(gè)文件夾

? ? ? ? ? ? ? ? ? ? ? ? ? public void getDeleteFile(File file){

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? File[] files = file.listFiles();

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (file != null) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for (File file2 : files) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?if (file2.isDirectory()) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? getDeleteFile(file2);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }else {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?file2.delete();

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? file.delete();

? ? ? ? ? ? ? ? ? ? ? ? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容