Java by API/java.io/DataInputStream

Материал из Java эксперт
Перейти к: навигация, поиск

DataInputStream: readBoolean()

  
/*
 * Output:
 */
import java.io.DataInputStream;
import java.io.FileInputStream;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileInputStream fis = new FileInputStream("fileName.dat");
      // Create a data input stream
      DataInputStream dis = new DataInputStream(fis);
      // Read and display data
      System.out.println(dis.readBoolean());
      System.out.println(dis.readByte());
      System.out.println(dis.readChar());
      System.out.println(dis.readDouble());
      System.out.println(dis.readFloat());
      System.out.println(dis.readInt());
      System.out.println(dis.readLong());
      System.out.println(dis.readShort());
      // Close file input stream
      fis.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





DataInputStream: readByte()

  
/*
 * Output:
 */
import java.io.DataInputStream;
import java.io.FileInputStream;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileInputStream fis = new FileInputStream("fileName.dat");
      // Create a data input stream
      DataInputStream dis = new DataInputStream(fis);
      // Read and display data
      System.out.println(dis.readBoolean());
      System.out.println(dis.readByte());
      System.out.println(dis.readChar());
      System.out.println(dis.readDouble());
      System.out.println(dis.readFloat());
      System.out.println(dis.readInt());
      System.out.println(dis.readLong());
      System.out.println(dis.readShort());
      // Close file input stream
      fis.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





DataInputStream: readChar()

  
/*
 * Output:
 */
import java.io.DataInputStream;
import java.io.FileInputStream;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileInputStream fis = new FileInputStream("fileName.dat");
      // Create a data input stream
      DataInputStream dis = new DataInputStream(fis);
      // Read and display data
      System.out.println(dis.readBoolean());
      System.out.println(dis.readByte());
      System.out.println(dis.readChar());
      System.out.println(dis.readDouble());
      System.out.println(dis.readFloat());
      System.out.println(dis.readInt());
      System.out.println(dis.readLong());
      System.out.println(dis.readShort());
      // Close file input stream
      fis.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





DataInputStream: readDouble()

  
/*
 * Output:
 */
import java.io.DataInputStream;
import java.io.FileInputStream;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileInputStream fis = new FileInputStream("fileName.dat");
      // Create a data input stream
      DataInputStream dis = new DataInputStream(fis);
      // Read and display data
      System.out.println(dis.readBoolean());
      System.out.println(dis.readByte());
      System.out.println(dis.readChar());
      System.out.println(dis.readDouble());
      System.out.println(dis.readFloat());
      System.out.println(dis.readInt());
      System.out.println(dis.readLong());
      System.out.println(dis.readShort());
      // Close file input stream
      fis.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





DataInputStream: readFloat()

  
/*
 * Output:
 */
import java.io.DataInputStream;
import java.io.FileInputStream;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileInputStream fis = new FileInputStream("fileName.dat");
      // Create a data input stream
      DataInputStream dis = new DataInputStream(fis);
      // Read and display data
      System.out.println(dis.readBoolean());
      System.out.println(dis.readByte());
      System.out.println(dis.readChar());
      System.out.println(dis.readDouble());
      System.out.println(dis.readFloat());
      System.out.println(dis.readInt());
      System.out.println(dis.readLong());
      System.out.println(dis.readShort());
      // Close file input stream
      fis.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





DataInputStream: readInt()

  
/*
 * Output:
 */
import java.io.DataInputStream;
import java.io.FileInputStream;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileInputStream fis = new FileInputStream("fileName.dat");
      // Create a data input stream
      DataInputStream dis = new DataInputStream(fis);
      // Read and display data
      System.out.println(dis.readBoolean());
      System.out.println(dis.readByte());
      System.out.println(dis.readChar());
      System.out.println(dis.readDouble());
      System.out.println(dis.readFloat());
      System.out.println(dis.readInt());
      System.out.println(dis.readLong());
      System.out.println(dis.readShort());
      // Close file input stream
      fis.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





DataInputStream: readLong()

  
/*
 * Output:
 */
import java.io.DataInputStream;
import java.io.FileInputStream;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileInputStream fis = new FileInputStream("fileName.dat");
      // Create a data input stream
      DataInputStream dis = new DataInputStream(fis);
      // Read and display data
      System.out.println(dis.readBoolean());
      System.out.println(dis.readByte());
      System.out.println(dis.readChar());
      System.out.println(dis.readDouble());
      System.out.println(dis.readFloat());
      System.out.println(dis.readInt());
      System.out.println(dis.readLong());
      System.out.println(dis.readShort());
      // Close file input stream
      fis.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





DataInputStream: readShort()

  
/*
 * Output:
 */
import java.io.DataInputStream;
import java.io.FileInputStream;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileInputStream fis = new FileInputStream("fileName.dat");
      // Create a data input stream
      DataInputStream dis = new DataInputStream(fis);
      // Read and display data
      System.out.println(dis.readBoolean());
      System.out.println(dis.readByte());
      System.out.println(dis.readChar());
      System.out.println(dis.readDouble());
      System.out.println(dis.readFloat());
      System.out.println(dis.readInt());
      System.out.println(dis.readLong());
      System.out.println(dis.readShort());
      // Close file input stream
      fis.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





DataInputStream: readUnsignedShort()

 
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class Main {
  public static void main(String[] args) throws IOException {
    DataInputStream in = new DataInputStream(new FileInputStream("Main.class"));
    int start = in.readInt();
    if (start != 0xcafebabe) {
      System.out.println("not valid");
    }
    in.close();
    System.out.println(in.readUnsignedShort()+"/"+in.readUnsignedShort());
    
  }
}





DataInputStream: readUTF()

  
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class MainClass {
  public static void main(String[] args) throws Exception {
    String serverName = args[0];
    int port = Integer.parseInt(args[1]);
    try {
      System.out.println("Connecting to " + serverName + " on port " + port);
      Socket client = new Socket(serverName, port);
      System.out.println("Just connected to " + client.getRemoteSocketAddress());
      OutputStream outToServer = client.getOutputStream();
      DataOutputStream out = new DataOutputStream(outToServer);
      out.writeUTF("Hello from " + client.getLocalSocketAddress());
      InputStream inFromServer = client.getInputStream();
      DataInputStream in = new DataInputStream(inFromServer);
      System.out.println("Server says " + in.readUTF());
      client.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}





new DataInputStream(FileInputStream fileInputStream)

  
/*
 * Output:
 */
import java.io.DataInputStream;
import java.io.FileInputStream;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileInputStream fis = new FileInputStream("fileName.dat");
      // Create a data input stream
      DataInputStream dis = new DataInputStream(fis);
      // Read and display data
      System.out.println(dis.readBoolean());
      System.out.println(dis.readByte());
      System.out.println(dis.readChar());
      System.out.println(dis.readDouble());
      System.out.println(dis.readFloat());
      System.out.println(dis.readInt());
      System.out.println(dis.readLong());
      System.out.println(dis.readShort());
      // Close file input stream
      fis.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





new DataInputStream(new BufferedInputStream(new FileInputStream("temp.tmp")))

  
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class Main {
  public static void main(String[] args) throws IOException {
    DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(
        "temp.tmp")));
    for (int i = 0; i < 10; i++)
      dis.readInt();
    dis.close();
  }
}