代码高亮111111

本文介绍了一个使用Java实现的源代码检索系统,该系统能够从指定目录下的所有Java文件中提取类名和代码内容,存储在HashMap中,以便快速检索。系统支持按类名和全限定名检索,返回类的源代码路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package source;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Ini{
	private static final String JAVAEXTC = ".java";
	private static String pathname = "F:\\source";
	private static HashMap<String,HashMap<String ,String >> map = new HashMap<String, HashMap<String,String>>();
	private static ThreadPoolExecutor exec  = new ThreadPoolExecutor(5, 10, 0, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>());
	
	public static void main(String[] args) throws IOException, InterruptedException {
		init();
		System.out.println(getByFullName("Set"));
	}
	public static Result getByName(String name) {
		String key = name+JAVAEXTC;
		if (map.containsKey(key)){
			HashMap<String,String> hashMap = map.get(key);
			if (hashMap.size() != 1){
				Set<String> keySet = hashMap.keySet();
				return new Result(keySet);
			}
			
			return new Result(new ArrayList<String>(hashMap.values()).get(0));
		}
		return null;
	}
	public static Result getByFullName(String className) {
		String key = className.substring(className.lastIndexOf(".")+1);
		if (map.containsKey(key+JAVAEXTC)){
			HashMap<String,String> hashMap = map.get(key+JAVAEXTC);
			if (hashMap.containsKey(className+JAVAEXTC)){
				return new Result(hashMap.get(className+JAVAEXTC));
			}
		}
		return null;
	}

	public static void init() throws IOException{
		/**
		 * 加载
		 */
		File sourceDir = new File(pathname);
		if (sourceDir.exists() && sourceDir.isDirectory()){
			searchJava(sourceDir);
		}
		exec.shutdown();
	}
	
	private static void searchJava(File sourceDir) throws IOException {
		File[] files = sourceDir.listFiles();
		for (File file : files) {
			if (file.isFile()){
				String name = file.getName();
				if (name.endsWith(JAVAEXTC)){
					String className = file.getAbsolutePath().substring(pathname.length()+1).replaceAll("\\\\", ".");
					exec.execute(new LoadJava(file, name, className));
					
				}
			} else {
				searchJava(file);
			}
				
		}
	}
	
	static class Result{
		private boolean single ;
		
		private Set<String> set;
		
		private String content;

		public boolean isSingle() {
			return single;
		}

		public void setSingle(boolean single) {
			this.single = single;
		}

		public Set<String> getSet() {
			return set;
		}

		public void setSet(Set<String> set) {
			this.set = set;
		}

		public String getContent() {
			return content;
		}

		public void setContent(String content) {
			this.content = content;
		}

		public Result() {
			super();
			// TODO Auto-generated constructor stub
		}

		public Result(Set<String> set) {
			super();
			this.set = set;
		}

		public Result(String content) {
			super();
			this.content = content;
			this.single = true;
		}

		@Override
		public String toString() {
			return "Result [single=" + single + ", set=" + set + ", content=" + content + "]";
		}
		
		
		
	}
	
	static class LoadJava implements Runnable{
		private File file;
		
		private String name;
		
		private String className;
		
		
		public LoadJava(File file, String name, String className) {
			super();
			this.file = file;
			this.name = name;
			this.className = className;
		}

		@Override
		public void run() {
			String content = this.getJAVAContent();
			HashMap<String,String> hashMap = null;
			synchronized (name) {
				if(map.containsKey(name)){
					hashMap = map.get(name);
				} else {
					hashMap = new HashMap<String, String>();
				}
				hashMap.put(className, content);
				map.put(name, hashMap);
			}
			
		}

		private String getJAVAContent() {
			BufferedReader br;
			try {
				br = new BufferedReader(new FileReader(file));
				String line;
				StringBuffer sb = new StringBuffer();
				while((line = br.readLine()) != null){
					sb.append(line);
					sb.append("\r\n");
				}
				br.close();
				return sb.toString();
			} catch (FileNotFoundException e) {
				
				e.printStackTrace();
			} catch (IOException e) {
				
				e.printStackTrace();
			}
			return null;
		}
		
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值