java语言程序设计 第十二章 (12.23、12.24、12.25、12.26)

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

import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
import java.net.URL;

public class URLData
{
    public static void main(String [] args) throws Exception
    {
        ArrayList intList = new ArrayList<>();
        URL url = new URL("http://cs.armstrong.edu/liang/data/Scores.txt");
        Scanner input = new Scanner(url.openStream());
        while (input.hasNext())
        {
            intList.add(new Integer(input.nextInt()));
        }

        double total = 0.0;
        double average = 0.0;
        for (Integer number : intList)
        {
            total += number;
            System.out.println(number);
        }

        average = total / intList.size();

        System.out.println("Total : " + total);
        System.out.printf("%s%.2f\n", "Average : ", average);
    }
}

这里写图片描述
java语言程序设计 第十二章 (12.23、12.24、12.25、12.26)_第1张图片

import java.util.Random;
import java.io.File;
import java.io.PrintWriter;

public class CreatBigDatabase
{
    public static void main(String [] args) throws Exception
    {
        File file = new File("Salary.txt");
        if (file.exists())
        {
            System.out.println("Salary.txt has exist");
            System.exit(1);
        }

        String[] level = {"assistant", "associate", "full"};
        Random random = new Random(7);

        try(
            PrintWriter output = new PrintWriter(file);
        )
        {
            for (int i = 1; i < 1001; i++)
            {
                int levelRandom = random.nextInt(3);
                output.print("FirstName" + i);
                output.print(" SecondName" + i);
                output.print(" " + level[levelRandom]);
                if (levelRandom == 0)
                {
                    output.println(" " + (int)((random.nextDouble() * 30000 + 50000) * 100) / 100.0);
                }
                else if (levelRandom == 1)
                {
                    output.println(" " + (int)((random.nextDouble() * 50000 + 60000) * 100) / 100.0);
                }   
                else if (levelRandom == 2)
                {
                    output.println(" " + (int)((random.nextDouble() * 55000 + 75000) * 100) / 100.0);
                }
            }   
        }
    }
}

这里写图片描述

import java.util.Scanner;
import java.io.File;
import java.net.URL;

public class HandleBigDatabase
{
    public static void main(String [] args) throws Exception
    {
        URL url = new URL("http://cs.armstrong.edu/liang/data/Salary.txt");
        Scanner input = new Scanner(url.openStream());
        double assistantTotal = 0.0;
        int countAssistant = 0;
        double associateTotal = 0.0;
        int countAssociate = 0;
        double fullTotal = 0.0;
        int countFull = 0;

        while (input.hasNext())
        {
            String line = input.nextLine();
            String[] message = line.split(" ");
            if (message[2].equals("assistant"))
            {
                assistantTotal += Double.parseDouble(message[3]);
                countAssistant++;
            }
            else if (message[2].equals("associate"))
            {
                associateTotal += Double.parseDouble(message[3]);
                countAssociate++;
            }
            else if (message[2].equals("full"))
            {
                fullTotal += Double.parseDouble(message[3]);
                countFull++;
            }
        }

        System.out.println("Assistant total " + assistantTotal + ", Average " + (assistantTotal / countAssistant));
        System.out.println("Associate total " + associateTotal + ", Average " + (associateTotal / countAssociate));
        System.out.println("Full total " + fullTotal + ", Average " + (fullTotal / countFull));
    }
}

这里写图片描述

import java.io.File;

public class MakeDirectory
{
    public static void main(String [] args) throws Exception
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a directory: ");
        String directory = input.next();
        File file = new File(directory);
        if (file.mkdirs())
        {
            System.out.println("Directory created successfully");
        }
        else
            System.out.println("Directory already exist");
    }
}

你可能感兴趣的:(异常处理和文本IO,java,语言,文本I-O)