3/1【关于最长英语链的代码学习】
附上我的代码
import java.io.*; import java.util.ArrayList; public class Main { public static void main(String[] args) { File file = new File("D:\\input1.txt"); // 判断文件格式 if (file.exists() && file.length() == 0) { System.out.println(" 文件为空!"); System.exit(0);//ok } else if (oneWord()) { System.out.println(" 文件只有一個單詞!"); System.exit(0); } else { System.out.println(" 文件内容有效。"); achieve(); } } /* * 判断只有一个单词 */ public static boolean oneWord() { ArrayList<String> list = new ArrayList<String>(); String pathname = "D:\\input1.txt"; int i = 0; try ( FileReader reader = new FileReader(pathname); BufferedReader br = new BufferedReader(reader) // 建立一个对象,它把文件内容转成计算机能读懂的语言 ) { String line = br.readLine(); // System.out.println(line); String[] words = line.split("[^(a-zA-Z)]+"); for (int j = 0; j < words.length; j++) { list.add(words[j]); i++; } System.out.println(i); } catch (IOException e) { e.printStackTrace(); } if (i <= 1) { return true; } else { return false; } } /* * 获取单词首尾字母的方法 */ // public static void print() { // String a = "sadfg"; // System.out.println(a.substring(0, 1));//获取首字母 // System.out.println(a.substring(a.length() - 1, a.length()));//获取尾字母 // } /* * 读取txt文件,并且存到ArrayList集合中,找出接龙单词 存入ArrayList1集合, * 把ArrayList1集合写入output1文件 */ public static void achieve() { ArrayList<String> list = new ArrayList<String>(); ArrayList<String> list1 = new ArrayList<String>();//存储遍历结果 String pathname = "D:\\input1.txt"; // 绝对路径或相对路径都可以,写入文件时演示相对路径,读取以上路径的input.txt文件 String writename = "D:\\output1.txt"; //防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw; //不关闭文件会导致资源的泄露,读写文件都同理 try (FileReader reader = new FileReader(pathname); BufferedReader br = new BufferedReader(reader); // 建立一个对象,它把文件内容转成计算机能读懂的语言 FileWriter writer = new FileWriter(writename); BufferedWriter out = new BufferedWriter(writer) ) { String line = br.readLine(); String[] all = line.split("[^(a-zA-Z)]+"); for (int j = 0; j < all.length; j++) { list.add(all[j]); } // System.out.println("遍历所有单词集合(arraylist):"); // for (int i = 0; i < list.size(); i++) { // System.out.println(list.get(i)); // } String word0 = list.get(0); list1.add(word0); String word = " ";//放出第一个之外的单词 for (int j = 1; j < list.size(); j++) { word = list.get(j);//获取之后的所有单词 if (word.substring(0, 1).equals(word0.substring(word0.length() - 1, word0.length())))//比较第一个单词的尾部和第二个单词的首部 { // 一样就放进去,并比较最新的一个 list1.add(word); word0 = word; } } //打印list1 // System.out.println("遍历list1"); //把list1中的结果写入output1.txt文件 for (int g = 0; g < list1.size(); g++) { System.out.println(list1.get(g)); out.write(list1.get(g)); out.write(" "); } System.out.println("程序结束!"); } catch (IOException e) { e.printStackTrace(); } } // /* // * txt文件的读取 // */ // public static void readFile() { // String pathname = "input1.txt"; // //不关闭文件会导致资源的泄露,读写文件都同理 // //Java7的try-with-resources可以优雅关闭文件,异常时自动关闭文件;详细解读https://stackoverflow.com/a/12665271 // try (FileReader reader = new FileReader(pathname); // BufferedReader br = new BufferedReader(reader) // 建立一个对象,它把文件内容转成计算机能读懂的语言 // ) { // String line=br.readLine(); // String[]ww=line.split("[^(a-zA-Z)]+"); // while ((line = br.readLine()) != null) { // // 一次读入一行数据 // System.out.println(line); // } // } catch (IOException e) { // e.printStackTrace(); // } // } // /* // *txt文件的写入 // */ // public static void writeFile() { // try { // File writeName = new File("D:\\output1.txt"); // writeName.createNewFile(); //创建新文件,有同名的文件的话直接覆盖 // try ( // FileWriter writer = new FileWriter(writeName); // BufferedWriter out = new BufferedWriter(writer) // ) { // out.write("我会写入文件啦!\r\n"); // \r\n为换行 // out.flush(); // 把缓存区内容压入文件 // } // } catch (IOException e) { // e.printStackTrace(); // } // } }
总结:
1、读入文件和写入文件的代码,之前不会,现在有所使用,熟悉了一些。
2、关于如何在一堆含有标点符号的文章中获取单词,这里用到了javascript的正则表达式
"[^(a-zA-Z)]+"
3、关于如何进行比较:这里用到了字符数组,我们可以利用两个list数组,进行传递,符合条件传进去,让最新的元素和第二个数组进行比较,以此类推。
本文作者:TranquilTimber
本文链接:https://www.cnblogs.com/gbrr/p/17169633.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步