public class GetRandomObj { public static void main(String[] args){ //默认把资源文件创建在C盘下,命名为SourceFile.txt //一行填一个数据 比如:12,33两个数据,就填2行 List<String> strings = new ArrayList<String>(); List<String> selected = new ArrayList<String>(); List<Integer> ints = new ArrayList<Integer>(); List<String> getStrs = new ArrayList<String>(); Boolean isFisrtStart = true; Boolean isGo = true; BufferedReader br = null; String err_msg = ""; //输入和输出文件 String path ="C:\\SourceFile.txt"; String resultPath = "C:\\ResultFile.txt"; GetRandomObj getRandomObj = new GetRandomObj(); //接收输入值 br = new BufferedReader(new InputStreamReader(System.in)); String str = null; //-----------------测试 测试---------------// /*System.out.println("是否初始化源文件? Y :是 ; 其他随意键 : 退出 "); try{ str = br.readLine(); }catch (Exception ex){ isGo = false; err_msg = ex.getMessage(); } if("Y".equals(str)){ getRandomObj.initSourceFile(path,100); }*/ //-----------------测试 测试---------------// System.out.println("-----------------------------"); System.out.println("开始读取归档文件!!!!!!"); //读抽取文件 strings = getRandomObj.redaFile(path); //加载上次抽取结果 selected = getRandomObj.redaFile(resultPath); System.out.println("读取归档成功!!!!!!"); System.out.println("-----------------------------"); while(isGo){ System.out.println("----------待抽数据集"+strings.size()+"个---------"); if(strings.size() == 0){ isGo = false; err_msg = "当前待抽数据集是空的,请重新启动程序,初始化数据!!!!!!!! "; break; }else { for (String str1 : strings) { System.out.println(str1); } } System.out.println("---------------------------"); if(selected.size() > 0){ System.out.println("----------已抽数据集"+selected.size()+"个---------"); //for(String str1 : strings){ System.out.println(selected.toString()); //} System.out.println("---------------------------"); } if(!isFisrtStart){ System.out.println("是否继续? Y :是 ; 其他随意键 : 退出 "); try{ str = br.readLine(); }catch (Exception ex){ isGo = false; err_msg = ex.getMessage(); break; } if(!"Y".equals(str)){ isGo = false; err_msg = "退出"; break; } }else { File file = new File(resultPath); if(file.exists()){ System.out.println("是否初始化(把已抽记录还原到待抽列表)? Y :是 ; 其他随意键 : 否 "); try{ str = br.readLine(); }catch (Exception ex){ isGo = false; err_msg = ex.getMessage(); break; } if("Y".equals(str)){ int count = 0; for(String selectStr : selected){ count ++; if(count == 1){ if(strings.size() > 0) { getRandomObj.writeFile(path, "", true, true); } if(count == selected.size()){ getRandomObj.writeFile(path,selectStr,true,false); }else{ getRandomObj.writeFile(path,selectStr,true,true); } }else if(count == selected.size()){ getRandomObj.writeFile(path,selectStr,true,false); }else{ getRandomObj.writeFile(path,selectStr,true,true); } strings.add(selectStr); } selected = new ArrayList<String>(); file.delete(); System.out.println("初始化成功!!!!!!!! "); System.out.println("---------------------------"); System.out.println("----------待抽数据集"+strings.size()+"个---------"); for(String str1 : strings){ System.out.println(str1); } System.out.println("---------------------------"); }else{ System.out.println("---------------------------"); System.out.println("跳过初始化!!!!!!!! "); System.out.println("---------------------------"); } } } if(strings.size() == 0){ isGo = false; err_msg = "当前待抽数据集是空的,请重新启动程序,初始化数据!!!!!!!! "; break; } System.out.println("输入你要检出的个数:"); try{ str = br.readLine(); }catch (Exception ex){ isGo = false; err_msg = ex.getMessage(); break; } int number = 0; try{ number = Integer.valueOf(str); }catch (Exception ex){ isGo = false; err_msg = ex.getMessage() +",结束!"; break; } if(number > strings.size()){ isGo = false; err_msg = "检出的个数大于当前数据集,结束!"; break; } isFisrtStart = false; Random r = new Random(); for(int i=0 ; i<number ; i++) { boolean flag = true; int ran1 = r.nextInt(strings.size()); for(int k : ints){ if(k == ran1){ flag = false; break; } } if(flag){ ints.add(ran1); }else{ i--; } } System.out.println("-------随机抽出"+number+"个----------"); for(int j : ints){ System.out.println(strings.get(j)); getStrs.add(strings.get(j)); selected.add(strings.get(j)); } for(String str2 : getStrs){ strings.remove(str2); } //把上一轮挑选的结果初始化 getStrs = new ArrayList<String>(); ints = new ArrayList<Integer>(); System.out.println("---------------------------"); System.out.println("***********************************************"); if(strings.size() == 0){ try{ //把待选文件清空 File file = new File(path); file.delete(); file.createNewFile(); }catch (Exception ex){ //ex; } isGo = false; System.out.println("----------已选数据集---------"); System.out.println(selected.toString()); System.out.println("---------------------------"); err_msg = "数据全部抽取完毕,结束!"; } } //关闭窗口输入流 if(null != br){ try{ br.close(); }catch (Exception ee){ //ee; } } System.out.println("---------------------------"); System.out.println(err_msg); System.out.println("---------------------------"); //增加记忆功能 try{ int count = 0; //把本次执行后的待选数据归档 for(String str3 : strings){ count ++; if(count == 1) { getRandomObj.writeFile(path,str3,false,true); }else if(count == strings.size()){ getRandomObj.writeFile(path,str3,true,false); } else{ getRandomObj.writeFile(path,str3,true,true); } } // 已选数据更新到文件里面 File file = new File(resultPath); if(!file.exists()){ file.createNewFile(); } count = 0; for(String str4 : selected){ count ++; if(count == 1) { getRandomObj.writeFile(resultPath,str4,false,true); }else if(count == selected.size()){ getRandomObj.writeFile(resultPath,str4,true,false); }else{ getRandomObj.writeFile(resultPath,str4,true,true); } } System.out.println("///"); System.out.println("记忆模式开启,已存档,下次启用会自动读取这次抽取结果!!!"); System.out.println(""); }catch (Exception ex){ err_msg = err_msg +"归档异常!!" + ex.getMessage(); System.out.println("---------------------------"); System.out.println(err_msg); System.out.println("---------------------------"); } } /** * 初始化源文件 * @param path * @param max */ public void initSourceFile(String path,int max){ GetRandomObj obj = new GetRandomObj(); for(int j =1 ;j <= max ; j++){ if(j == 1) { obj.writeFile(path,j+"",false,true); }else if(j == max){ obj.writeFile(path,j+"",true,false); }else{ obj.writeFile(path,j+"",true,true); } } } /** * 根据路径读取文件 * @param path * @return */ public List<String> redaFile(String path){ List<String> result = new ArrayList<String>(); FileReader fileReader = null; BufferedReader br = null; File file = new File(path); if(file.exists()){ try{ fileReader = new FileReader(file); br = new BufferedReader(fileReader);//构造一个BufferedReader类来读取文件 String s = null; while((s = br.readLine())!=null){//使用readLine方法,一次读一行 result.add(s); } }catch (Exception ex){ }finally { if(null != br){ try{ br.close(); }catch (Exception ee){ //ee; } } if(null != fileReader){ try{ fileReader.close(); }catch (Exception ee){ //ee; } } } } return result; } /** * 写文件 * @param path * @param str * @param isAppend * @param isAddChangeLine */ public void writeFile(String path, String str, Boolean isAppend, Boolean isAddChangeLine){ FileWriter fileWriter = null; BufferedWriter bw = null; File file = new File(path); try { fileWriter = new FileWriter(file,isAppend); bw = new BufferedWriter(fileWriter); bw.write(str); if(isAddChangeLine){ bw.write("\n"); } bw.flush(); }catch (Exception ex){ }finally { if(null != fileWriter){ try{ fileWriter.close(); }catch (Exception ee){ //ee; } } if(null != bw){ try{ bw.close(); }catch (Exception ee){ //ee; } } } } }
java实现从指定集合中随机获取指定数量的对象,并具备记忆功能
最新推荐文章于 2024-06-29 02:58:04 发布