Java语言程序设计 第十七章 (样例代码)

程序小白,希望和大家多交流,共同学习
Java语言程序设计 第十七章 (样例代码)_第1张图片

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

public class TestFileStream 
{
    public static void main(String[] args) throws IOException
    {
        try(
            FileOutputStream output = new FileOutputStream("testFile.dat"); 
        ){
            for (int i = 0; i <= 10; i++)
            {
                output.write(i);
            }
        }

        try(
            FileInputStream input = new FileInputStream("testFile.dat");    
        ){
            int value = 0;
            while ((value = input.read()) != -1)
            {
                System.out.print(value + " ");
            }
            System.out.println();
        }
    }
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class TestDataStream 
{
    public static void main(String[] args) throws IOException
    {
        try(
            DataOutputStream output = new DataOutputStream(new FileOutputStream("testDataStream.dat")); 
        ){
            output.writeUTF("John");
            output.writeDouble(85.5);
            output.writeUTF("Jim");
            output.writeDouble(185.5);
            output.writeUTF("George");
            output.writeDouble(105.25);
        }

        try(
            DataInputStream input = new DataInputStream(new FileInputStream("testDataStream.dat")); 
        ){
            System.out.println(input.readUTF() + " " + input.readDouble());
            System.out.println(input.readUTF() + " " + input.readDouble());
            System.out.println(input.readUTF() + " " + input.readDouble());
        }
    }
}
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.EOFException;

public class DetectEndOfFile
{
    public static void main(String[] args)
    {
        /*
        保证输入和输出的方式匹配,也就是使用OutputStream 
        中write()进行输入,使用DataInoutStream中readInt()进行输出的时候,
        读出的数据不可用。(可用readByte()但是这样也不好)
        */
        try
        {
            try(
                DataOutputStream output = new DataOutputStream(new FileOutputStream("test.dat"));   
            ){
                output.writeDouble(4.5);
                output.writeDouble(12.5);
                output.writeDouble(3.5);
            }
            try(
                DataInputStream input = new DataInputStream(new FileInputStream("test.dat"));   
            ){
                while (true)
                {
                    System.out.println(input.readDouble());
                }
            }
        }
        catch (EOFException ex)
        {
            System.out.println("All data were read");
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
    }
}
import java.io.File;
import java.io.BufferedOutputStream;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class Copy
{
    public static void main(String[] args) throws IOException
    {
        if (args.length != 2)
        {
            System.out.println("Usage: java Copy sourceFile targetFile");
            System.exit(1);
        }

        File sourceFile = new File(args[0]);
        if (!sourceFile.exists())
        {
            System.out.println("Source file " + args[0] + " does not exist");
            System.exit(2);
        }

        File targetFile = new File(args[1]);
        if (!targetFile.exists())
        {
            System.out.println("Target file " + args[1] + " does not exist");
            System.exit(3);
        }

        try(
            //BufferedInputStream 继承自FilterInputStream 并没有新的方法,FilterInputStream是抽象类
            //他也没有新的方法,所有的方法均来自FileInputStream
            //我们已经知道FileInputStream的读取数据类型是byte所以最好是使用Byte进行类型转换
            //以免超过Byte类型的字符,在编译之后生成乱码
            BufferedInputStream input = new BufferedInputStream(new FileInputStream(sourceFile));
            BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(targetFile));
        ){
            int r, numberOfBytesCopies = 0;
            while ((r = input.read()) != -1)
            {
                output.write((byte)r);
                numberOfBytesCopies++;
            }
            System.out.println(numberOfBytesCopies + " bytes copied");
        }
    }
}
//java Copy Copy.java target.txt
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

public class TestObjectStream
{
    public static void main(String[] args) throws IOException
    {
        try(
            ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("object.dat")); 
        ){
            output.writeUTF("Jon");
            output.writeDouble(85.5);
            output.writeObject(new java.util.Date());
        }
    }
}
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;

public class TestObjectInputStream
{
    public static void main(String[] args) throws ClassNotFoundException, IOException
    {
        try(
            ObjectInputStream input = new ObjectInputStream(new FileInputStream("object.dat")); 
        ){
            System.out.println(input.readUTF());
            System.out.println(input.readDouble());
            System.out.println((Date)input.readObject());
        }
    }
}
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.io.IOException;

public class TestObjectStreamForArray
{
    public static void main(String[] args) 
            throws ClassNotFoundException, IOException
    {
        int[] numbers = {1, 2, 3, 4, 5, 6};
        String[] names = {"Jhon", "Susan", "Kim"};

        try(
            ObjectOutputStream output = new ObjectOutputStream(
                new FileOutputStream("array.dat", true));   
        ){
            output.writeObject(numbers);
            output.writeObject(names);
        }

        try(
            ObjectInputStream input = new ObjectInputStream(
                new FileInputStream("array.dat"));  
        ){
            int[] newNumbers = (int[])(input.readObject());
            String[] newNames = (String[])(input.readObject());

            int numLen = newNumbers.length;
            int nameLen = newNames.length;
            for (int i = 0; i < numLen; i++)
            {
                System.out.print(newNumbers[i] + " ");
            }

            System.out.println();
            for (int i = 0; i < nameLen; i++)
            {
                System.out.print(newNames[i] + " ");
            }
            System.out.println();
        }
    }
}
import java.io.RandomAccessFile;
import java.io.IOException;

public class TestRandomAccessFile 
{
    public static void main(String[] args) throws IOException
    {
        try(
            RandomAccessFile inout = new RandomAccessFile("inout.dat", "rw");
        ){
            inout.setLength(0);
            for (int i = 0; i <  200; i++)
            {
                inout.writeInt(i);
            }

            System.out.println("Current file length is " + inout.length());

            inout.seek(0);
            System.out.println("The first number is " + inout.readInt());

            inout.seek(1 * 4);
            System.out.println("The second number is " + inout.readInt());

            inout.seek(9 * 4);
            System.out.println("The tenth number is " + inout.readInt());
            //使用seek()将文件指针跳到第十个数值的位置,在读取第十个数值后
            //文件指针在向后跳动,到达地十一个数值的位置,此时重新写入数据555
            //此时第十一个数值被覆盖
            inout.writeUTF("123");
            inout.seek(inout.length());
            //跳到文件末尾处,并写入一个新的数值
            inout.writeInt(999);
            System.out.println("The new length is " + inout.length());

            inout.seek(10 * 4);
            System.out.println("The eleventh number is " + inout.readUTF());
            System.out.println("The thirteen number is " + inout.readInt());

            /*
            每一次使用seek(pos : long);都会使文件指针向后跳动pos长度,
            使用readInt()等读取指定数据类型的方法,也会使文件指针向后跳
            sizeOf(数据类型)长度,此时再调用写writeInt()等方法,
            会占用自此以后的sizeOf(数据类型)的长度。很明显新写入的数据类型如
            果和之前的类型所占用的字节不同,那么自此之后的文件就被修改了。
            */
        }
    }
}

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