一:自动计算出其总容量
package 计算文件夹容量;
import java.io.File;
import java.util.ArrayList;
public class Size {
static long size=0;
private static ArrayList<String> filelist=new ArrayList<String>();
public static void main(String[] args) {
Size s=new Size();
String filePath="F:\\JAVA项目代码";
s.getFiles(filePath);
}
//通过递归得到某一路径下所有的目录及文件
void getFiles(String filePath) {
File root=new File(filePath);
File[] files=root.listFiles();
for(File file:files) {
if(file.isDirectory()) {
getFiles(file.getAbsolutePath());
filelist.add(file.getAbsolutePath());
}
else {
size+=file.getAbsolutePath().length();
}
}
System.out.println("大小是"+size);
}
}
二:完成加解密工作
package 文件解密和加密操作;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
public class FileCode {
Key key;
public FileCode(String str) {
getKey(str);//生成密匙
}
/**
* 根据参数生成KEY
*/
public void getKey(String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator = null;
} catch (Exception e) {
throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
}
}
/**
* 文件file进行加密并保存目标文件destFile中
*
* @param file 要加密的文件 如c:/test/srcFile.txt
* @param destFile 加密后存放的文件名 如c:/加密后文件.txt
*/
public void encrypt(String file, String destFile) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
// cipher.init(Cipher.ENCRYPT_MODE, getKey());
cipher.init(Cipher.ENCRYPT_MODE, this.key);
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(destFile);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = cis.read(buffer)) > 0) {
out.write(buffer, 0, r);
}
cis.close();
is.close();
out.close();
}
/**
* 文件采用DES算法解密文件
*
* @param file 已加密的文件 如c:/加密后文件.txt
* * @param destFile
* 解密后存放的文件名 如c:/ test/解密后文件.txt
*/
public void decrypt(String file, String dest) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, this.key);
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherOutputStream cos = new CipherOutputStream(out, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = is.read(buffer)) >= 0) {
System.out.println();
cos.write(buffer, 0, r);
}
cos.close();
out.close();
is.close();
}
public static void main(String[] args){
FileCode td = new FileCode("aaa");
/* try {
td.encrypt("F:\\测试加密.txt", "F:\\测试加密1.txt");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //加密 */
try {
td.decrypt("F:\\测试加密1.txt", "F:\\测试加密2.txt");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //解密
}
}
三:文件分割工具
package 文件分割工具;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CutFile {
public static void main(String[] args) {
//调用cutFile()函数 传人参数分别为 (原大文件,切割后存放的小文件的路径,切割规定的内存大小)
cutFile("F:\\测试加密.txt", "F:",10 * 10 * 20);
}
private static void cutFile(String src, String endsrc, int num) {
FileInputStream fis = null;
File file = null;
try {
fis = new FileInputStream(src);
file = new File(src);
//创建规定大小的byte数组
byte[] b = new byte[num];
int len = 0;
//name为以后的小文件命名做准备
int name = 1;
//遍历将大文件读入byte数组中,当byte数组读满后写入对应的小文件中
while ((len = fis.read(b)) != -1) {
//分别找到原大文件的文件名和文件类型,为下面的小文件命名做准备
String name2 = file.getName();
int lastIndexOf = name2.lastIndexOf(".");
String substring = name2.substring(0, lastIndexOf);
String substring2 = name2.substring(lastIndexOf, name2.length());
FileOutputStream fos = new FileOutputStream(endsrc + "\\\\"+ substring + "-" + name + substring2);
//将byte数组写入对应的小文件中
fos.write(b, 0, len);
//结束资源
fos.close();
name++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
//结束资源
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}