Wednesday, April 8, 2015

Example 17

Chapter 17

//-----------


Introduction to Java Programming, Tenth Edition, Y. Daniel Liang

import java.io.*;

public class TestFileStream {
  public static void main(String[] args) throws IOException {
    try (
      // Create an output stream to the file
      FileOutputStream output = new FileOutputStream("temp.dat");
    ) {
      // Output values to the file
      for (int i = 1; i <= 10; i++)
        output.write(i);
    }

    try (
      // Create an input stream for the file
      FileInputStream input = new FileInputStream("temp.dat");
    ) {
      // Read values from the file
      int value;
      while ((value = input.read()) != -1)
        System.out.print(value + " ");
    }
  }
}

//------------


Introduction to Java Programming, Tenth Edition, Y. Daniel Liang

import java.io.*;

public class TestDataStream {
  public static void main(String[] args) throws IOException {
    try ( // Create an output stream for file temp.dat
      DataOutputStream output =
        new DataOutputStream(new FileOutputStream("temp.dat"));
    ) {
      // Write student test scores to the file
      output.writeUTF("John");
      output.writeDouble(85.5);
      output.writeUTF("Jim");
      output.writeDouble(185.5);
      output.writeUTF("George");
      output.writeDouble(105.25);
    }
   
    try ( // Create an input stream for file temp.dat
      DataInputStream input =
        new DataInputStream(new FileInputStream("temp.dat"));
    ) {
      // Read student test scores from the file
      System.out.println(input.readUTF() + " " + input.readDouble());
      System.out.println(input.readUTF() + " " + input.readDouble());
      System.out.println(input.readUTF() + " " + input.readDouble());
    }
  }
}

//-------------


Introduction to Java Programming, Tenth Edition, Y. Daniel Liang

import java.io.*;

public class DetectEndOfFile {
  public static void main(String[] args) {
    try {
      try (DataOutputStream output =
        new DataOutputStream(new FileOutputStream("test.dat"))) {
        output.writeDouble(4.5);
        output.writeDouble(43.25);
        output.writeDouble(3.2);
      }
     
      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();
    }
  }
}

//---------------


Introduction to Java Programming, Tenth Edition, Y. Daniel Liang

import java.io.*;

public class Copy {
  /** Main method
     @param args[0] for sourcefile
     @param args[1] for target file
   */
  public static void main(String[] args) throws IOException {
    // Check command-line parameter usage
    if (args.length != 2) {
      System.out.println(
        "Usage: java Copy sourceFile targetfile");
      System.exit(1);
    }

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

    // Check if target file exists
    File targetFile = new File(args[1]);
    if (targetFile.exists()) {
      System.out.println("Target file " + args[1]
        + " already exists");
      System.exit(3);
    }

    try (
      // Create an input stream
      BufferedInputStream input =
        new BufferedInputStream(new FileInputStream(sourceFile));
 
      // Create an output stream
      BufferedOutputStream output =
        new BufferedOutputStream(new FileOutputStream(targetFile));
    ) {
      // Continuously read a byte from input and write it to output
      int r, numberOfBytesCopied = 0;
      while ((r = input.read()) != -1) {
        output.write((byte)r);
        numberOfBytesCopied++;
      }

      // Display the file size
      System.out.println(numberOfBytesCopied + " bytes copied");
    }
  }
}

//------------


Introduction to Java Programming, Tenth Edition, Y. Daniel Liang

import java.io.*;

public class TestObjectOutputStream {
  public static void main(String[] args) throws IOException {
    try ( // Create an output stream for file object.dat
      ObjectOutputStream output =
        new ObjectOutputStream(new FileOutputStream("object.dat"));
    ) {
      // Write a string, double value, and object to the file
      output.writeUTF("John");
      output.writeDouble(85.5);
      output.writeObject(new java.util.Date());
    }
  }
}

//--------------


Introduction to Java Programming, Tenth Edition, Y. Daniel Liang

import java.io.*;

public class TestObjectInputStream {
  public static void main(String[] args)
    throws ClassNotFoundException, IOException {
    try ( // Create an input stream for file object.dat
      ObjectInputStream input =
        new ObjectInputStream(new FileInputStream("object.dat"));
    ) {
      // Read a string, double value, and object from the file
      String name = input.readUTF();
      double score = input.readDouble();
      java.util.Date date = (java.util.Date)(input.readObject());
      System.out.println(name + " " + score + " " + date);
    }
  }
}

//----------------


Introduction to Java Programming, Tenth Edition, Y. Daniel Liang

import java.io.*;

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

    try ( // Create an output stream for file array.dat
      ObjectOutputStream output = new ObjectOutputStream(new
        FileOutputStream("array.dat", true));
    ) {
      // Write arrays to the object output stream
      output.writeObject(numbers);
      output.writeObject(strings);
    }

    try ( // Create an input stream for file array.dat
      ObjectInputStream input =
        new ObjectInputStream(new FileInputStream("array.dat"));
    ) {
      int[] newNumbers = (int[])(input.readObject());
      String[] newStrings = (String[])(input.readObject());
 
      // Display arrays
      for (int i = 0; i < newNumbers.length; i++)
        System.out.print(newNumbers[i] + " ");
      System.out.println();
 
      for (int i = 0; i < newStrings.length; i++)
        System.out.print(newStrings[i] + " ");
    }
  }
}

//----------------


Introduction to Java Programming, Tenth Edition, Y. Daniel Liang

import java.io.*;

public class TestRandomAccessFile {
  public static void main(String[] args) throws IOException {
    try ( // Create a random access file
      RandomAccessFile inout = new RandomAccessFile("inout.dat", "rw");
    ) {
      // Clear the file to destroy the old contents if exists
      inout.setLength(0);
 
      // Write new integers to the file
      for (int i = 0; i < 200; i++)
        inout.writeInt(i);
 
      // Display the current length of the file
      System.out.println("Current file length is " + inout.length());
 
      // Retrieve the first number
      inout.seek(0); // Move the file pointer to the beginning
      System.out.println("The first number is " + inout.readInt());
 
      // Retrieve the second number
      inout.seek(1 * 4); // Move the file pointer to the second number
      System.out.println("The second number is " + inout.readInt());
 
      // Retrieve the tenth number
      inout.seek(9 * 4); // Move the file pointer to the tenth number
      System.out.println("The tenth number is " + inout.readInt());
 
      // Modify the eleventh number
      inout.writeInt(555);
 
      // Append a new number
      inout.seek(inout.length()); // Move the file pointer to the end
      inout.writeInt(999);
 
      // Display the new length
      System.out.println("The new length is " + inout.length());
 
      // Retrieve the new eleventh number
      inout.seek(10 * 4); // Move the file pointer to the eleventh number
      System.out.println("The eleventh number is " + inout.readInt());
    }
  }
}

//------------

No comments:

Post a Comment