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
原创粉丝点击