public class Test1 { public static void main(String[] args) throws IOException { // TODO 自动生成的方法存根 String filename ="E:\\123.txt"; File a=new File(filename); File file=new File("E:\\456.txt"); long startTime = System.currentTimeMillis(); //judeFileExists(a); if(judeFileExists(a)) { danci(filename); } else { try { FileWriter fw =new FileWriter(file); fw.write("无此文件"); fw.flush(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } long endTime = System.currentTimeMillis(); //获取结束时间 System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); //输出程序运行时间 } public static void danci(String s) throws IOException { BufferedReader br = new BufferedReader(new FileReader(s)); StringBuffer sb = new StringBuffer(); String text = null; while ((text = br.readLine()) != null) { sb.append(text);// 将读取出的字符追加到stringbuffer中 } br.close(); // 关闭读入流 String str = sb.toString().toLowerCase(); // 将stringBuffer转为字符并转换为小写 String[] words = str.split("[^(a-zA-Z)]+"); // 非单词的字符来分割,得到所有单词 for (int i = 0; i < words.length; i++) { System.out.print(words[i]+" "); if ((i+1)%20==0) System.out.println(); } String end; String start; int num=0; StringBuffer lainjie1 = new StringBuffer(); int S=0,E=0; String b1=words[0]; if(b1.equals("")) { System.out.println("文件为空"); lainjie1.append("文件为空"); } else { if(words.length==1) { System.out.println("只有一个单词"); return; } for(int i=0;i<words.length-1;i++) { int num1=0; StringBuffer lainjie = new StringBuffer(); boolean jued=false; int s1=0,e1=0; for (int j = i+1; j < words.length; j++) { end=words[j-1].substring(words[j-1].length()-1); start=words[j].substring(0,1); if(end.equals(start)) { if(jued==false) { lainjie.append(words[j-1]); lainjie.append(" "); num1++; jued=true; s1=j; } lainjie.append(words[j]); lainjie.append(" "); num1++; } else { e1=j; break; } } if(num1>num) { num=num1; lainjie1=lainjie; S=s1; E=e1; } } if(lainjie1.toString().equals("")) {lainjie1.append("无互联语句");} System.out.println("---------------------------------------"); System.out.println(lainjie1.toString()); System.out.println(num); System.out.println(S); System.out.println(E); } File file =new File("E:\\456.txt"); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } try { FileWriter fw =new FileWriter(file); fw.write(lainjie1.toString()); fw.flush(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } public static boolean judeFileExists(File file) { if (file.exists()) { System.out.println("文件存在"); return true; } else { System.out.println("文件不存在"); return false; } } }
1
标签:String,fw,System,接龙,println,new,文本,首尾,out From: https://www.cnblogs.com/qwb0614/p/17181211.html