Java语言程序设计 第十七章 (17.1、17.2、17.3、17.4、17.5、17.6、17.7)

程序小白,希望和大家多交流,共同学习
Java语言程序设计 第十七章 (17.1、17.2、17.3、17.4、17.5、17.6、17.7)_第1张图片
Java语言程序设计 第十七章 (17.1、17.2、17.3、17.4、17.5、17.6、17.7)_第2张图片
17.1

import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;

public class WriteInfoUseFileIO
{
    public static void main(String[] args) throws IOException
    {
        File file = new File("Exercise17_01.txt");  
        try(
            PrintWriter write = new PrintWriter(file);
        ){
            for (int i = 0; i < 100; i++)
            {
                int random = (int)(Math.random() * 100);
                write.print(random + " ");
            }
        }
    }
}

17.2

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteInfoUseDatIO
{
    public static void main(String[] args) throws IOException
    {
        try(
            FileOutputStream output = new FileOutputStream("Exercise17_02.dat");    
        ){
            int random = 0;
            for (int i = 0; i < 100; i++)
            {
                random = (int)(Math.random() * 100);
                output.write(random);
            }
        }

        try(
            FileInputStream input = new FileInputStream("Exercise17_02.dat");   
        ){
            for (int i = 0; i< 100; i++)
            {
                System.out.print(input.read() + " ");
            }
            System.out.println();
        }
    }
}

17.3

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteInfoUseDataStream
{
    public static void main(String[] args) throws IOException
    {
        try(
            DataOutputStream output = new DataOutputStream(
            new FileOutputStream("Exercise17_03.dat")); 
        ){
            int random = 0;
            for (int i = 0; i < 100; i++)
            {
                random = (int)(Math.random() * 1000);
                output.writeInt(random);
            }
        }
    }
}
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.EOFException;

public class GetSumUseDataStream
{
    public static void main(String[] args)
    {
        int sum = 0;
        try
        {
            try(
                DataInputStream input = new DataInputStream(
                    new FileInputStream("Exercise17_03.dat"));  
            ){
                while (true)
                {
                    sum += input.readInt();
                }
            }
        }
        catch (EOFException ex)
        {
            System.out.println(sum);
            System.out.println("已经读到文件结尾");
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }

    }
}

17.4

import java.io.File;
import java.util.Scanner;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ChangeFileToUTF
{
    public static void main(String[] args) throws IOException
    {
        if (args.length != 2)
        {
            System.out.println("Useage: java ChangeFileToUTF sourceFile.java targetFile.utf");
            System.exit(1);
        }
        File file = new File(args[0]);
        File targetFile = new File(args[1]);
        try(
            Scanner input = new Scanner(file);
            DataOutputStream output = new DataOutputStream(new FileOutputStream(targetFile));
        ){
            String str = "";
            while (input.hasNext())
            {
                str = input.next();
                output.writeUTF(str);
            }

            System.out.println(file.length() + " " + targetFile.length());
        }

    }
}
//java ChangeFileToUTF DetectEndOfFile.java targetFile.utf

17.5

import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;

public class strogeObject
{
    public static void main(String[] args) throws ClassNotFoundException, IOException
    {
        try(
            ObjectOutputStream output = new ObjectOutputStream(
                new FileOutputStream("object1.dat"));
        ){
            int[] numbers = {1, 2, 3, 4, 5};
            double d = 5.5;
            Date date = new Date();
            output.writeObject(numbers);
            output.writeDouble(d);
            output.writeObject(date);
        }

        try(
            ObjectInputStream input = new ObjectInputStream(
                new FileInputStream("object1.dat"));    
        ){
            int[] getNum = (int[])(input.readObject());
            int length = getNum.length;
            for (int i = 0; i < length; i++)
            {
                System.out.print(getNum[i] + " ");
            }
            System.out.println();
            System.out.println(input.readDouble());
            System.out.println((Date)(input.readObject()));
        }
    }
}

17.6

import java.io.Serializable;
/*
//实现接口Serializable的类的对象可以序列化,但是此类中可能存在不可序列化的东西
//例如静态变量。还有引用的未序列化的对象。
//静态变量,JVM会自动忽略,但是引用未序列化的对象,要添加关键字transiznt进行标记
//让JVM忽略此变量*/
public class Loan implements Serializable
{
    private double annualInterestRates;
    private int numberOfYears;
    private double loanAmount;
    private java.util.Date loanDate;

    public Loan()
    {
        this(2.5, 1, 1000);
    }

    public Loan(double annualInterestRates, int numberOfYears, double loanAmount)
    {
        this.annualInterestRates = annualInterestRates;
        this.numberOfYears = numberOfYears;
        this.loanAmount = loanAmount;
        loanDate = new java.util.Date();
    }

    public double getAnnualInterestRates()
    {
        return annualInterestRates;
    }

    public void setAnnualInterestRates(double annualInterestRates)
    {
        this.annualInterestRates = annualInterestRates;
    }

    public int getNumberOfYears()
    {
        return numberOfYears;
    }

    public void setNumberOfYears(int numverOfYears)
    {
        this.numberOfYears = numberOfYears;
    }

    public double getLoanAmount()
    {
        return loanAmount;
    }

    public void setLoanAmount(double loanAmount)
    {
        this.loanAmount = loanAmount;
    }

    public double getMonthlyPayment()
    {
        double monthlyInterestRate = annualInterestRates / 1200;
        double monthlyPayment = loanAmount * monthlyInterestRate / (1 -
            (1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12)));

        return monthlyPayment;
    }

    public double getTotalPayment()
    {
        double totalPayment = getMonthlyPayment() * numberOfYears * 12;
        return totalPayment;
    }

    public java.util.Date getLoanDate()
    {
        return loanDate;
    }
}
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteLoan
{
    public static void main(String[] args) throws IOException
    {
        try(
            ObjectOutputStream output = new ObjectOutputStream(
                new FileOutputStream("exercise17_07.dat")); 
        ){
            for (int i = 0; i < 3; i++)
            {
                output.writeObject(new Loan());
            }
        }
    }
}

17.7

import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.EOFException;

public class ReadLoanAndCalculate
{
    public static void main(String[] args)
    {
        try
        {
            try(
            ObjectInputStream input = new ObjectInputStream(
                new FileInputStream("exercise17_07.dat"));
            ){
                while (true)
                {
                    Loan loan = (Loan)(input.readObject());
                    System.out.println(loan.getTotalPayment());
                    System.out.println(loan.getLoanDate());
                }
            }
        }
        catch (EOFException ex)
        {
            System.out.println("All were read");
        }
        catch(ClassNotFoundException ex)
        {
            System.out.println(ex);
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
    }
}

你可能感兴趣的:(二进制I/O)