1. 程式人生 > >文件與流課後作業

文件與流課後作業

spl oid root println vat ray exception security leo

一、編寫一個程序,指定一個文件夾,能自動計算出其總容量。

package TEST1;
import java.io.File;
import java.util.ArrayList;
public class total {
static long Size=0;
private static ArrayList<String>filelist=new ArrayList<String>();
public static void main(String[] args)
{
total s =new total();
String filePath="D:\\QQ文件";
s.getFile(filePath);
}
void getFile(String filePath)
{
File root=new File(filePath);
File[]files=root.listFiles();
for(File file:files)
{
if(file.isDirectory())
{
getFile(file.getAbsolutePath());
filelist.add(file.getAbsolutePath());
}
else
{
Size+=file.getAbsolutePath().length();
}
}
System.out.println("容量為:"+Size);
}
}

二、編寫一個文件加解密程序,通過命令行完成加解密工作。

package TEST1;
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 code {
Key key;
public code(String str)
{
getKey(str);
}
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: " + e);
}
}
public void encrypt(String file, String destFile) throws Exception
{
Cipher cipher = Cipher.getInstance("DES");
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();
}
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){
code td = new code("aaa");
try {
td.encrypt("D:/r.txt", "D:/r加密後.txt");
} catch (Exception e) {
e.printStackTrace();
}
try {
td.decrypt("D:/r加密後.txt", "D:/r解密後.txt");
} catch (Exception e) {
e.printStackTrace();
}
}
}

三、編寫一個文件分割工具,能把一個大文件分割成多個小的文件。並且能再次把它們合並起來得到完整的文件。

package TEST3;
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 pew {
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("文件合並完成!");

}
}

package TEST3;

import java.io.File;

public class pew1 {
public void main(String[] args) throws Exception {

//分割文件

pew.splitFileDemo(new File("TEST"), 2);

//合並文件
pew.joinFileDemo(new String[]{"TEST2", "TEST3"});
}
}

實驗結果截圖傳不上去。

文件與流課後作業