java语言程序设计 第十二章 (12.28、12.30、12.33)

程序小白,希望和大家多交流,共同学习
这里写图片描述

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;
    }
}
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值