This article shares several codes with you, mainly about the encryption and decryption of Thread+IO files. Let’s take a look at the specific code:
Encrypted startup thread
package ; import ; public class enCodeFileThread extends Thread { public Files files; public File file; public File dst; public enCodeFileThread(String name,Files files, File file,File dst) { super(name); = dst; = files; = file; } public void run() { (file,dst); } }
Decrypt the startup thread
package ; import ; public class enCodeFileThread extends Thread { public Files files; public File file; public File dst; public enCodeFileThread(String name,Files files, File file,File dst) { super(name); = dst; = files; = file; } public void run() { (file,dst); } }
Decrypt the startup thread
package ; import ; public class deCodeFileThread extends Thread { public Files files; public File file; public File dst; public deCodeFileThread(String name,Files files, File file,File dst) { super(name); = dst; = files; = file; } public void run() { (dst); } }
File object serialization
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class Files implements Serializable { /** * Default sequence id */ private static final long serialVersionUID = 1L; private String filesNo; private String name; private byte[] content; private boolean flag = true; public Files() { } public Files(String filesNo){} public Files(String filesNo,String name, byte[] content) { super(); = name; = content; } public String getFilesNo() { return filesNo; } public void setFilesNo(String filesNo) { = filesNo; } public String getName() { return name; } public void setName(String name) { = name; } public byte[] getContent() { return content; } public void setContent(byte[] content) { = content; } //Encrypt the serialized file public synchronized void enCode(File file,File dst) { if(!flag){ try { wait(); } catch (InterruptedException e) { (); } }else{ //Get every file in the folder File[] chlidFiles = (); List<Files> list = new ArrayList(); for (int i = 0; i < ; i++) { File tmpFile = chlidFiles[i]; Files files = getFiled(tmpFile); (files); } saveFiles(dst,list); flag = true; notifyAll(); } } /** * Save information * @param dst * @param list */ private void saveFiles(File dst, List<Files> list) { FileOutputStream fos = null; ObjectOutputStream oos = null; try { fos = new FileOutputStream(dst); oos = new ObjectOutputStream(fos); (list); } catch (FileNotFoundException e) { (); } catch (IOException e) { (); }finally{ try { if(oos != null){ (); oos = null; } if(fos != null){ (); fos = null; } } catch (IOException e) { (); } } } public Files getFiled(File tmpFile) { Files files = new Files(); String name = (); (name); FileInputStream fis = null; ByteArrayOutputStream baos = null; try { fis = new FileInputStream(tmpFile); baos = new ByteArrayOutputStream(); byte[] buff = new byte[1024]; int hasRead = 0; while((hasRead = (buff)) > -1){ (buff, 0, hasRead); } (()); return files; } catch (FileNotFoundException e) { (); } catch (IOException e) { (); }finally{ try { if(baos != null){ (); baos = null; } } catch (IOException e) { (); } try { if(fis != null){ (); fis = null; } } catch (IOException e) { (); } } return null; } //Decrypt the serialized file public synchronized void deCode(File dst) { if(!flag){ try { wait(); } catch (InterruptedException e) { (); } }else{ List<Files> list = readFiles(dst); for (Files files : list) { String name = (); byte[] content = (); File file = new File(()+"//bbb",name); if(!()){ try { (); } catch (IOException e) { (); } } FileOutputStream fos = null; try { fos = new FileOutputStream(file); (content); } catch (FileNotFoundException e) { (); } catch (IOException e) { (); }finally{ try { if(fos != null){ (); fos = null; } flag = false; notifyAll(); } catch (IOException e) { (); } } } } } private List<Files> readFiles(File dst) { FileInputStream fis = null; ObjectInputStream ois = null; try { fis = new FileInputStream(dst); ois = new ObjectInputStream(fis); List<Files> list = (List<Files>) (); return list; } catch (FileNotFoundException e) { (); } catch (IOException e) { (); } catch (ClassNotFoundException e) { (); }finally{ try { if(ois != null){ (); ois = null; } } catch (IOException e) { (); } try { if(fis != null){ (); fis = null; } } catch (IOException e) { (); } } return null; } public String toString() { return "Files [name=" + name + ", content=" + (content != null ? arrayToString(content, ) : null) + "]"; } private String arrayToString(Object array, int len) { StringBuffer buffer = new StringBuffer(); ("["); for (int i = 0; i < len; i++) { if (i > 0) (", "); if (array instanceof byte[]) (((byte[]) array)[i]); } ("]"); return (); } public int hashCode() { return getFilesNo().hashCode(); } public boolean equals(Object obj) { if(obj!=null && getClass() == ){ Files target = (Files) obj; return ().equals(filesNo); } return false; } }
Test class
package ; import ; public class TestThread { public static void main(String[] args) { Files files = new Files("123"); File file = new File("E:\\20160928JavaBase\\Test\\aaa"); File file2 = new File("E:\\20160928JavaBase\\Test\\gg"); new enCodeFileThread("Encrypted File", files,file ,new File(file, "")).start(); new deCodeFileThread("Decrypted File", files, file, new File(file, "")).start(); } }
Summarize
The above is the entire content of this article about the encryption and decryption code example of Thread+IO file exploration in Java. I hope it will be helpful to everyone. Interested friends can continue to refer to this site:Java creation and ending thread code example、Detailed explanation of the code of thread communication producer consumer model and waiting wake-up mechanism of Java multi-threadedWait, if you have any questions, you can leave a message at any time, and the editor will reply to everyone in time. Thank you friends for your support for this site!