日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

图片查找

發布時間:2023/12/10 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 图片查找 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import java.io.*; import java.math.BigInteger; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.security.MessageDigest; import java.util.HashMap; import java.util.Properties;/*** 讀取一個圖片MD5,和文件夾里的所有圖片進行比較,返回相同的匹配信息* @author silence*/ public class MD5 {private static final HashMap<String, File> map = new HashMap<>();// 需要查找的圖片路徑,查找是否有HQ命名的圖片private static File oneFile = new File("operate\\HQ");//配置文件路徑private static File configFile = new File("operate\\HQ\\config.txt");//寫入錯誤信息的文件路徑private static File errorFile = new File("operate\\HQ\\run.txt");//被查找的圖片文件路徑private static File listFile = null;//統計查找次數的計數器private static int count = 1;/*** 傳入一個文件流對象,讀取MD5返回** @param file 文件流對象* @return 讀取MD5返回* @throws IOException io異常*/private static String getMd5ByFile(File file) throws IOException {String md5Value = null;if (!file.canRead()) {return null;}try (FileInputStream in = new FileInputStream(file)) {MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());MessageDigest md5 = MessageDigest.getInstance("MD5");md5.update(byteBuffer);BigInteger bi = new BigInteger(1, md5.digest());md5Value = bi.toString(16);} catch (Exception e) {e.printStackTrace();}return md5Value;}/*** 主方法=============================================*/public static void main(String[] args) throws IOException {//在這里讀取判斷單個文件圖片,是否有效String oneMd5 = getOneMd5(oneFile);//1.判斷兩個文件路徑是否有效,無效退出if (!ioFile() || oneMd5 == null) {System.out.println("程序退出!!!");System.exit(1);}//2.被查找的圖片文件路徑,讀取到mapgetFileMd5(listFile);System.out.println("文件讀完成共讀取" + count + "條信息....");//判斷=======================Properties properties = new Properties();FileWriter writer = new FileWriter(errorFile);if (map.containsKey(oneMd5)) {//如果有符合條件的值,想配置文件寫成功信息properties.setProperty("所匹配的文件名", map.get(oneMd5).getName());properties.setProperty("所匹配的文件路徑", map.get(oneMd5).toString());properties.setProperty("所在文件夾路徑", map.get(oneMd5).getParent());properties.setProperty("匹配成功,共匹配", count + "個文件信息");System.out.println("匹配成功,共匹配" + count + "個文件信息");} else {//如果沒有符合條件的值,向配置文件寫失敗信息properties.setProperty("匹配失敗,共匹配", count + "個文件信息");System.out.println("匹配失敗,共匹配" + count + "個文件信息");}properties.store(writer, null);writer.close();System.out.println("程序正常結束");}/*** 讀取需要查找文件的md5** @param oneFile 0* @return MD5*/private static String getOneMd5(File oneFile) throws IOException {//如果沒有這個文件夾,返回nullif (!oneFile.canRead()) {return null;}File[] files = oneFile.listFiles();for (File f : files) {//需要是文件的String name = f.getName().split("\\.")[0];if (f.isFile() && "HQ".equals(name)) {return getMd5ByFile(f);}}//如果沒有這個文件信息Properties p = new Properties();System.out.println("該文件沒有名為HQ的圖片信息,請把需要查找的圖片改名為HQ,放到當前文件下");p.setProperty("程序結束", "該文件沒有名為HQ的圖片信息,請把需要查找的圖片改名為HQ,放到當前文件下");FileWriter writer = new FileWriter(errorFile);p.store(writer, null);writer.close();return null;}/*** 判斷是否存在配置文件,存在返回true 不存在,創建,返回false** @return 存在返回true 不存在,創建,返回false*/private static boolean ioFile() throws IOException {//判斷文件配置路徑是否存在,并且可讀if (!configFile.canRead()) {System.out.println("配置文件不存在!!!即將創建一個空配置文件....");FileWriter writer = new FileWriter(configFile);Properties p = new Properties();p.setProperty("被查找的圖片文件路徑", "");p.store(writer, null);writer.close(); //關閉文件流return false;} else {String value1, value2;Properties p = new Properties();try (FileReader reader = new FileReader(configFile)) {System.out.println("配置文件存在,正在讀取配置信息......");//讀取配置文件p.load(reader);value1 = (String) p.get("被查找的圖片文件路徑");}System.out.println("正在判斷文件路徑是否為空....");if (value1 == null || "".equals(value1)) {System.out.println("文件路徑為空!!!");return false;}if (!(new File(value1).canRead())) {System.out.println("文件路徑無效!!!");return false;}listFile = new File(value1);System.out.println("圖片路徑有效進入下一步操作....");return true;}}/*** 讀取所有文件,存儲md5** @param file 文件* @throws IOException io異常*/private static void getFileMd5(File file) throws IOException {if (!file.canRead()) {//如果文件不可讀,或不是正確的文件路徑System.out.println("文件不可讀,或不是正確的文件路徑");return;}//返回此文件夾包含的所有文件try {File[] files = file.listFiles();assert files != null;for (File f : files) {//如果是一個文件,獲取MD5存入到mapif (f.isFile()) {String md5 = getMd5ByFile(f);System.out.println("成功讀取第" + count + "個");count++;map.put(md5, f);} else {getFileMd5(f);}}} catch (Exception e) {e.printStackTrace();}}}

================
1.在HQ文件夾內存入需要查找的HQ命名的圖片
2.在HQ文件夾中config.txt 中填入需要查找的文件夾名(文件夾)
列:C:\Users\Administrator\Desktop\Demo小案例\共享文件夾\IEDA背景圖片\img\新建文件夾 (2)
必須為雙斜杠
3.點擊啟動,運行完成后信息會存入HQ文件夾下run.txt文件中

#Fri Apr 16 18:31:12 CST 2021
所在文件夾路徑=D:\develop\jdk-11\conf\management
所匹配的文件路徑=D:\develop\jdk-11\conf\management\jmxremote.access
匹配成功,共匹配=1943個文件信息
所匹配的文件名=jmxremote.access

#Fri Apr 16 17:30:46 CST 2021
被查找的圖片文件路徑=D:\develop
C:\Users\Administrator\Desktop\Demo小案例\共享文件夾\IEDA背景圖片\img\新建文件夾 (2)

總結

以上是生活随笔為你收集整理的图片查找的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。