編寫一個文件加解密程序,經過命令行完成加解密工做;java
package Chengji;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Secret {
private static final int n=0x99;
private static int dt=0;
public static void main(String[] args) {
File srcFile=new File("a.txt");
File encFile=new File("b.txt");
File decFile=new File("c.txt");
try {
EncFile(srcFile,encFile);
DecFile(encFile,decFile);
}catch(Exception e) {
e.printStackTrace();
}
}
private static void EncFile(File srcFile,File encFile)throws Exception{
if(!srcFile.exists()) {
System.out.println("不存在");
}
if(!encFile.exists()) {
System.out.println("已被建立");
encFile.createNewFile();
}
InputStream is=new FileInputStream(srcFile);
OutputStream os=new FileOutputStream(encFile);
while((dt=is.read())>-1) {//當讀到文件內容時
os.write(dt^n);//將讀出的內容加密後寫入
}
is.close();
os.flush();
os.close();
}
private static void DecFile(File encFile,File decFile)throws Exception{
if(!encFile.exists()) {
System.out.println("不存在");
}
if(!decFile.exists()) {
System.out.println("已被建立");
decFile.createNewFile();
}
InputStream fis=new FileInputStream(encFile);
OutputStream fos=new FileOutputStream(decFile);
while((dt=fis.read())>-1) {
fos.write(dt^n);
}
fis.close();
fos.flush();
fos.close();
}
}

編寫一個文件分割工具,能把一個大文件分割成多個小的文件。而且能再次把它們合併起來獲得完整的文件。工具
package Chengji;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Fenge {
private static void cutFile(String src, String endsrc, int num) {
FileInputStream fis = null;
File f = null;
try {
fis = new FileInputStream(src);
f = new File(src);
byte[] b = new byte[num];
int len = 0;
int name1 = 1;
while ((len = fis.read(b)) != -1) {
String name2 = f.getName();
int lastIndexOf = name2.lastIndexOf(".");
String substring = name2.substring(0, lastIndexOf);
String substring2 = name2.substring(lastIndexOf, name2.length());
FileOutputStream fos = new FileOutputStream(endsrc + "\\\\"+ substring + "-" + name1 + substring2);
fos.write(b, 0, len);
fos.close();
name1++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void togetherFile(String src, String endsrc){
FileOutputStream fos = null;
File file1 = null;
File file2 = null;
try {
file1 = new File(endsrc);
file2 = new File(src);
fos = new FileOutputStream(endsrc);
for(File file : file2.listFiles()){
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
byte[] b = new byte[1024*1024];
int len = 0;
while((len = fis.read(b)) != -1){
fos.write(b, 0, len);
}
fis.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(fos != null){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
cutFile("C:\\Users\\北方\\Desktop\\1-信1807-5 郝子嘉 20183684\\HPATSS\\Harry Potter and the Sorcerer's Stone.txt", "C:\\Users\\北方\\Desktop\\1-信1807-5 郝子嘉 20183684\\HPATSS",10 * 10 * 20);
togetherFile("C:\\Users\\北方\\Desktop\\1-信1807-5 郝子嘉 20183684\\HPATSS","C:\\Users\\北方\\Desktop\\1-信1807-5 郝子嘉 20183684\\HPATSS\\Harry Potter and the Sorcerer's Stone.txt");
}
}
