文件与流——课后作业
1、编写一个程序,指定一个文件夹,能自动计算出其总容量
源程序:
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:\\2345Explorer";
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);
}
}
运行测试结果:
大小是431
大小是868
大小是964
大小是1054
大小是1254
大小是1322
大小是2013
大小是2259
大小是2338
大小是2338
大小是3655
大小是3655
大小是3655
大小是3655
大小是3749
2、编写一个文件加解密程序,通过命令行完成加解密工作
源代码:
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 TestDES {
Key key;
public TestDES(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){
TestDES td = new TestDES("aaa");
try {
td.encrypt("e:/r.txt", "e:/r加密后.txt");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //加密
try {
td.decrypt("e:/r加密后.txt", "e:/r解密后.txt");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //解密
}
}
运行测试结果:
我在指定的位置创建了一个文本文件,并录入了一段文字:
需要被加密的文本。
通过加密之后得到了一个新的文件,文本内容被加密为了:
謱??o沑?儩g櫉7鮄\x
通过程序进行解密,得到了一个解密之后的文件,文本内容为
需要被加密的文本。
以此便达到了对文本文件的加密操作,并且可以对之进行解密操作。
3、编写一个文件分割工具,能把一个大文件分割成多个小的文件。并且能再次把它们合并起来得到完整的文件。
源代码:
package 文件的分割合并;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileSliptUtil {
public static void splitFileDemo(File src, int m) throws IOException {
if(src.isFile()) {
//获取文件的总长度
long l = src.length();
//获取文件名
String fileName = src.getName().substring(0, src.getName().indexOf("."));
//获取文件后缀
String endName = src.getName().substring(src.getName().lastIndexOf("."));
System.out.println(endName);
InputStream in = null;
try {
in = new FileInputStream(src);
for(int i = 1; i <= m; i++) {
StringBuffer sb = new StringBuffer();
sb.append(src.getParent());
sb.append("\\");
sb.append(fileName);
sb.append("_data");
sb.append(i);
sb.append(endName);
System.out.println(sb.toString());
File file2 = new File(sb.toString());
//创建写文件的输出流
OutputStream out = new FileOutputStream(file2);
int len = -1;
byte[] bytes = new byte[10*1024*1024];
while((len = in.read(bytes))!=-1) {
out.write(bytes, 0, len);
if(file2.length() > (l / m)) {
break;
}
}
out.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(in != null)
in.close();
}
System.out.println("--- 文件分割完成 ---");
}
}
public static void joinFileDemo(String[] src) {
// 获取合并文件
File newFile = new File(src[0].toString());
// 获取文件名 后缀
String fileName = newFile.getName().substring(0, newFile.getName().indexOf("_"));
String endName = newFile.getName().substring(newFile.getName().lastIndexOf("."));
// 得到新的文件名
StringBuffer sb = new StringBuffer();
sb.append(newFile.getParent());
sb.append("\\");
sb.append(fileName);
sb.append(endName);
newFile = new File(sb.toString());
for(int i = 0; i < src.length; i++) {
File file = new File(src[i]);
try {
//读取小文件的输入流
InputStream in = new FileInputStream(file);
OutputStream out = new FileOutputStream(newFile, true);
int len = -1;
byte[] bytes = new byte[10*1024*1024];
while((len = in.read(bytes))!=-1) {
out.write(bytes, 0, len);
}
out.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("文件合并完成!");
}
public class TestFileSlipt {
public void main(String[] args) throws Exception {
//分割文件
FileSliptUtil.splitFileDemo(new File("文件路径"), 2);
//合并文件
FileSliptUtil.joinFileDemo(new String[]{"文件路径", "文件路径"});
}
}
}