溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java探索之Thread+IO文件的加密解密的示例分析

發(fā)布時(shí)間:2021-08-05 14:38:44 來(lái)源:億速云 閱讀:96 作者:小新 欄目:編程語(yǔ)言

這篇文章主要為大家展示了“Java探索之Thread+IO文件的加密解密的示例分析”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Java探索之Thread+IO文件的加密解密的示例分析”這篇文章吧。

加密啟動(dòng)線程

package com.hz.subsection;
import java.io.File;
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);
    this.dst = dst;
    this.files = files;
    this.file = file;
  }
  public void run() {
    files.enCode(file,dst);
  }
}

解密啟動(dòng)線程

package com.hz.subsection;
import java.io.File;
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);
    this.dst = dst;
    this.files = files;
    this.file = file;
  }
  public void run() {
    files.enCode(file,dst);
  }
}

解密啟動(dòng)線程

package com.hz.subsection;
import java.io.File;
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);
    this.dst = dst;
    this.files = files;
    this.file = file;
  }
  public void run() {
    files.deCode(dst);
  }
}

文件對(duì)象序列化

package com.hz.subsection;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class Files implements Serializable {
  /**
   * 默認(rèn)序列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();
    this.name = name;
    this.content = content;
  }
  public String getFilesNo() {
    return filesNo;
  }
  public void setFilesNo(String filesNo) {
    this.filesNo = filesNo;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public byte[] getContent() {
    return content;
  }
  public void setContent(byte[] content) {
    this.content = content;
  }
  //加密序列化文件
  public synchronized void enCode(File file,File dst) {
    if(!flag){
      try {
        wait();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }else{
      //獲取文件夾下的每一個(gè)文件
      File[] chlidFiles = file.listFiles();
      List<Files> list = new ArrayList();
      for (int i = 0; i < chlidFiles.length; i++) {
        File tmpFile = chlidFiles[i];
        Files files = getFiled(tmpFile);
        list.add(files);
      }
      saveFiles(dst,list);
      flag = true;
      notifyAll();
    }  
  }
  /**
   * 保存信息
   * @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);
      oos.writeObject(list);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }finally{
      try {
        if(oos != null){
          oos.close();
          oos = null;
        }
        if(fos != null){
          fos.close();
          fos = null;
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  public Files getFiled(File tmpFile) {
    Files files = new Files();
    String name = tmpFile.getName();
    files.setName(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 = fis.read(buff)) > -1){
        baos.write(buff, 0, hasRead);
      }
      files.setContent(baos.toByteArray());
      return files;
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }finally{
      try {
        if(baos != null){
          baos.close();
          baos = null;
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
      try {
        if(fis != null){
          fis.close();
          fis = null;
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return null;
  }
  //解密序列化文件
  public synchronized void deCode(File dst) {
    if(!flag){
      try {
        wait();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }else{
      List<Files> list = readFiles(dst);
    for (Files files : list) {
      String name = files.getName();
      byte[] content = files.getContent();
      File file = new File(dst.getParent()+"//bbb",name);
      if(!file.exists()){
        try {
          file.createNewFile();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      FileOutputStream fos = null;
      try {
        fos = new FileOutputStream(file);
        fos.write(content);
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }finally{
        try {
          if(fos != null){
            fos.close();
            fos = null;
          }
          flag = false;
          notifyAll();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    }
  }
  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>) ois.readObject();
      return list;
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }finally{
      try {
        if(ois != null){
          ois.close();
          ois = null;
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
      try {
        if(fis != null){
          fis.close();
          fis = null;
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return null;
  }
  public String toString() {
    return "Files [name="
        + name
        + ", content="
        + (content != null ? arrayToString(content, content.length)
            : null) + "]";
  }
  private String arrayToString(Object array, int len) {
    StringBuffer buffer = new StringBuffer();
    buffer.append("[");
    for (int i = 0; i < len; i++) {
      if (i > 0)
        buffer.append(", ");
      if (array instanceof byte[])
        buffer.append(((byte[]) array)[i]);
    }
    buffer.append("]");
    return buffer.toString();
  }
  public int hashCode() {
    return getFilesNo().hashCode();
  }
  public boolean equals(Object obj) {
    if(obj!=null && getClass() == Files.class){
      Files target = (Files) obj;
      return target.getFilesNo().equals(filesNo);
    }
    return false;
  }
}

測(cè)試類

package com.hz.subsection;
import java.io.File;
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("加密文件", files,file ,new File(file, "dst.hz")).start();
    new deCodeFileThread("解密文件", files, file, new File(file, "dst.hz")).start();
  }
}

以上是“Java探索之Thread+IO文件的加密解密的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI