最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java探索之Thread+IO文件的加密解密代碼實(shí)例

 更新時(shí)間:2017年10月26日 17:05:51   作者:書思BookReflect  
這篇文章主要介紹了Java探索之Thread+IO文件的加密解密代碼實(shí)例,具有一定參考價(jià)值,需要的朋友可以了解下。

這篇文章向大家分享了幾段代碼,主要是關(guān)于Thread+IO文件的加密解密,下面看看具體代碼:

加密啟動線程

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);
  }
}

解密啟動線程

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);
  }
}

解密啟動線程

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);
  }
}

文件對象序列化

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()+"http://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;
  }
}

測試類

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();
  }
}

總結(jié)

以上就是本文關(guān)于Java探索之Thread+IO文件的加密解密代碼實(shí)例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:Java創(chuàng)建與結(jié)束線程代碼示例Java多線程之線程通信生產(chǎn)者消費(fèi)者模式及等待喚醒機(jī)制代碼詳解等,有什么問題可以隨時(shí)留言,小編會及時(shí)回復(fù)大家的。感謝朋友們對本站的支持!

相關(guān)文章

  • 8個(gè)簡單部分開啟Java語言學(xué)習(xí)之路 附j(luò)ava學(xué)習(xí)書單

    8個(gè)簡單部分開啟Java語言學(xué)習(xí)之路 附j(luò)ava學(xué)習(xí)書單

    8個(gè)簡單部分開啟Java語言學(xué)習(xí)之路,附j(luò)ava學(xué)習(xí)書單,這篇文章主要向大家介紹了學(xué)習(xí)java語言的方向,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Java Web使用POI導(dǎo)出Excel的方法詳解

    Java Web使用POI導(dǎo)出Excel的方法詳解

    這篇文章主要介紹了Java Web使用POI導(dǎo)出Excel的方法,結(jié)合實(shí)例形式詳細(xì)分析了Java Web使用POI導(dǎo)出Excel的具體操作步驟、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-06-06
  • Java spring的三種注入方式詳解流程

    Java spring的三種注入方式詳解流程

    Spring框架由Rod Johnson開發(fā),2004年發(fā)布了Spring框架的第一版。Spring是一個(gè)從實(shí)際開發(fā)中抽取出來的框架,因此它完成了大量開發(fā)中的通用步驟,留給開發(fā)者的僅僅是與特定應(yīng)用相關(guān)的部分,從而大大提高了企業(yè)應(yīng)用的開發(fā)效率
    2021-10-10
  • Java Swing JList列表框的實(shí)現(xiàn)

    Java Swing JList列表框的實(shí)現(xiàn)

    這篇文章主要介紹了Java Swing JList列表框的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • 淺談mybatis中SQL語句給boolean類型賦值問題

    淺談mybatis中SQL語句給boolean類型賦值問題

    這篇文章主要介紹了淺談mybatis中SQL語句給boolean類型賦值問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • 淺談JDK14性能管理工具之jmap和jhat

    淺談JDK14性能管理工具之jmap和jhat

    我們在寫代碼的過程中,經(jīng)常會遇到內(nèi)存泄露的問題,比如某個(gè)集合中的對象沒有被回收,或者內(nèi)存出現(xiàn)不明原因的增長。這些都是需要我們來定位的問題,我們可以使用jmap和jhat來對java程序中的內(nèi)存對象進(jìn)行分析。
    2021-06-06
  • Java實(shí)現(xiàn)前端jsencrypt.js加密后端解密的示例代碼

    Java實(shí)現(xiàn)前端jsencrypt.js加密后端解密的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用jsencrypt.js實(shí)現(xiàn)前端加密,利用Java實(shí)現(xiàn)后端解密的功能,文中的示例代碼講解詳細(xì),需要的可以參考一下
    2022-09-09
  • java怎么設(shè)置代理ip實(shí)現(xiàn)高效網(wǎng)絡(luò)請求

    java怎么設(shè)置代理ip實(shí)現(xiàn)高效網(wǎng)絡(luò)請求

    無論是在爬蟲、API調(diào)用還是網(wǎng)絡(luò)測試中,代理IP的使用都變得愈發(fā)重要,本文我們主要來介紹一下如何在Java中設(shè)置代理IP實(shí)現(xiàn)高效網(wǎng)絡(luò)請求吧
    2024-11-11
  • SSM框架通過mybatis-generator自動生成代碼(推薦)

    SSM框架通過mybatis-generator自動生成代碼(推薦)

    這篇文章主要介紹了SSM框架通過mybatis-generator自動生成代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2017-11-11
  • Object類toString()和equals()方法使用解析

    Object類toString()和equals()方法使用解析

    這篇文章主要介紹了Object類toString()和equals()方法使用解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02

最新評論

桂东县| 杭锦后旗| 桃源县| 南平市| 宣恩县| 新疆| 孝义市| 涟水县| 江西省| 石阡县| 新宁县| 宜良县| 马龙县| 樟树市| 嘉兴市| 平度市| 泗阳县| 沂南县| 集安市| 龙陵县| 滨海县| 都昌县| 泰顺县| 剑川县| 鹤峰县| 北安市| 仁寿县| 翁牛特旗| 达尔| 上思县| 利津县| 琼结县| 南阳市| 定边县| 望江县| 横山县| 出国| 常宁市| 疏附县| 怀集县| 略阳县|