java语言程序设计 第十二章 (12.28、12.30、12.33)
来源:互联网 发布:wap彩票网站源码 编辑:程序博客网 时间:2024/05/17 05:17
程序小白,希望和大家多交流,共同学习
import java.util.Scanner;import java.io.File;public class RenameToFile{ public static void main(String [] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a file path:"); File file = new File(truePath(input.next())); renameFile(file); } public static String truePath(String path) { return path.replaceAll("\\\\", "/"); } public static void renameFile(File file) { if (getNewName(file).equals("###")) { if (file.isFile()) { return; } else { handleDirectory(file); } } else if (file.isDirectory()) { handleDirectory(file); handleFile(file); } else if (file.isFile()) { handleFile(file); } } public static File handleFile(File file) { File newFile = new File(getNewName(file)); System.out.println(newFile.getAbsolutePath() + " " + file.renameTo(newFile)); return newFile; } public static void handleDirectory(File file) { File[] subFile = file.listFiles(); System.out.println(file.getAbsolutePath()); for (File everyFile : subFile) { renameFile(everyFile); } } public static String getNewName(File file) { String name = truePath(file.getAbsolutePath()); String newName = null; if (!name.contains("Exercise")) { return "###"; } String tail = name.substring(name.lastIndexOf("Exercise"), name.length()); //要求仅仅匹配.加一个字母 if (tail.contains(".")) { String[] countNum = tail.split("[ercise_.]"); if (countNum[countNum.length - 3].length() == 1) { countNum[countNum.length - 3] = "0" + countNum[countNum.length - 3]; } newName = name.substring(0, name.lastIndexOf("Exercise")) + "Exercise" + countNum[countNum.length - 3] + "_" + countNum[countNum.length - 2] + "." + countNum[countNum.length - 1]; } else { String[] countNum = tail.split("[ercise_]"); if (countNum[countNum.length - 2].length() == 1) { countNum[countNum.length - 2] = "0" + countNum[countNum.length - 2]; } newName = name.substring(0, name.lastIndexOf("Exercise")) + "Exercise" + countNum[countNum.length - 2] + "_" + countNum[countNum.length - 1]; } return newName; }}
import java.util.Scanner;import java.io.File;public class CountLetterApper{ public static void main(String [] args) throws Exception { int[] countLetter = new int[26]; Scanner input = new Scanner(System.in); System.out.print("Enter a file name : "); String myFileName = input.next(); File myFile = new File(myFileName); if (!myFile.canRead()) { System.out.println("This file is unreadable"); System.exit(1); } else { Scanner readFile = new Scanner(myFile); while (readFile.hasNextLine()) { String everyLine = readFile.nextLine().toUpperCase(); //System.out.println(everyLine); for (int i = 0; i < everyLine.length(); i++) { if (Character.isLetter(everyLine.charAt(i))) { countLetter[everyLine.charAt(i) - 'A']++; } } } } for (int i = 0; i < 26; i++) { System.out.println("Number of " + (char)(i + 'A') + "'s: " + countLetter[i]); } }}
import java.net.URL;import java.util.Scanner;import java.util.ArrayList;public class SearchWebWord{ public static void main(String [] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a urlString : "); String urlString = input.nextLine(); System.out.print("Enter a word: "); String word = input.next(); input.nextLine(); findWord(word, urlString); } public static void findWord(String word, String startString) { ArrayList<String> listOfPendingURLs = new ArrayList<>(); ArrayList<String> listOfTraversedURLs = new ArrayList<>(); listOfPendingURLs.add(startString); boolean succ = true; while (succ) { String urlString = listOfPendingURLs.remove(0); if (!listOfTraversedURLs.contains(urlString)) { listOfTraversedURLs.add(urlString); if (getWord(word, urlString)) { System.out.println(urlString); System.exit(1); } for (String everyURL : getSubURLs(urlString)) { if (!listOfTraversedURLs.contains(everyURL)) { listOfPendingURLs.add(everyURL); } } } if (listOfPendingURLs.size() == 0) { System.out.println("Not fild " + word); succ = false; } } } public static boolean getWord(String word, String urlString) { try { Scanner input = new Scanner(new URL(urlString).openStream()); while (input.hasNextLine()) { String everyLine = input.nextLine(); if (everyLine.contains(word)) { return true; } } } catch (Exception ex) { System.out.println(ex.getMessage()); } return false; } public static ArrayList<String> getSubURLs(String urlString) { ArrayList<String> list = new ArrayList<>(); try { URL url = new URL(urlString); Scanner input = new Scanner(url.openStream()); int current = 0; while (input.hasNext()) { String line = input.nextLine(); current = line.indexOf("http:"); while (current > 0) { int endIndex = line.indexOf("\"", current); if (endIndex > 0) { list.add(line.substring(current, endIndex)); current = line.indexOf("http:", endIndex); } else current = -1; } } } catch (Exception ex) { System.out.println(ex.getMessage()); } return list; }}
阅读全文
0 0
- java语言程序设计 第十二章 (12.28、12.30、12.33)
- java语言程序设计 第十二章(12.7、12.8、12.9)
- java语言程序设计 第十二章 (12.11、12.12、12.13、12.14)
- java语言程序设计 第十二章 (12.15、12.16、12.17)
- java语言程序设计 第十二章 (12.18、12.19、12.20、12.21、12.22)
- java语言程序设计 第十二章 (12.23、12.24、12.25、12.26)
- java 语言程序设计 第十二章(12.1、12.2、12.3、12.4、12.5、12.6)
- java语言程序设计基础篇第十二章编程练习题
- Java 语言程序设计基础篇原书第八版_第十二章_第八题_程序分享
- java语言程序设计 第八章(8.35、8.36)
- java语言程序设计 第七章(7.21、7.35)
- java语言程序设计 第十一章(11.8 11.9)
- java语言程序设计 第十三章 (13.6、13.7)
- java语言程序设计 第十三章 (13.17、13.18)
- chapter1(JAVA语言程序设计)
- Java语言程序设计(序)
- java语言程序设计 第十一章 11.3
- Java语言程序设计 第13章
- MVP结构基础框架分享
- 谷歌开放的TensorFlow Object Detection API 效果如何?对业界有什么影响
- MySQL日志——Undo | Redo
- 将制定整数插入到数组(数组已排序完成),插入后保持结果有序
- 线性表的基本操作
- java语言程序设计 第十二章 (12.28、12.30、12.33)
- 后缀表达式求值
- C#基础之继承
- Jzoj4892 最优得分
- 解析markdown生成的文件md
- 小项目——基于嵌入式 web 服务器的测控系统
- sdut 2128 树结构练习——排序二叉树(BST)的中序遍历
- Chrome使用技巧 前端开发的一些技巧
- oracle基础知识