Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 21st, 2012  |  syntax: None  |  size: 2.33 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package io;
  6. import java.io.DataInputStream;
  7. import java.io.FileInputStream;
  8. import java.io.DataOutputStream;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11.  
  12. /**
  13.  *
  14.  * @author azlina
  15.  */
  16. public class io {
  17.    
  18.         public static void main(String[] args) {
  19.         //
  20.         // Prepares some data to be written to a file.
  21.         //
  22.        
  23.             int noA = 1;
  24.         String name = "Azlina";
  25.  
  26.      
  27.  
  28.      
  29.  
  30.         try {
  31.             //
  32.             // Create an instance of FileOutputStream with cities.dat
  33.             // as the file name to be created. Then we pass the input
  34.             // stream object in the DataOutputStream constructor.
  35.             //
  36.             FileOutputStream fos = new FileOutputStream("cities.dat");
  37.             DataOutputStream dos = new DataOutputStream(fos);
  38.  
  39.             //
  40.             // Below we write some data to the cities.dat.
  41.             // DataOutputStream class have various method that allow
  42.             // us to write primitive type data and string. There are
  43.             // method called writeInt(), writeFloat(), writeUTF(),
  44.             // etc.
  45.             //
  46.             dos.writeInt(noA);
  47.             dos.writeUTF(name);
  48.  
  49.  
  50.      
  51.  
  52.             dos.flush();
  53.             dos.close();
  54.  
  55.             //
  56.             // Now we have a cities.dat file with some data in it.
  57.             // Next you'll see how easily we can read back this
  58.             // data and display it. Just like the DataOutputStream
  59.             // the DataInputStream class have the corresponding
  60.             // read methods to read data from the file. Some of
  61.             // the method names are readInt(), readFloat(),
  62.             // readUTF(), etc.
  63.             //
  64.             FileInputStream fis = new FileInputStream("cities.dat");
  65.             DataInputStream dis = new DataInputStream(fis);
  66.  
  67.             //
  68.             // Read the first data
  69.             //
  70.             int no1 = dis.readInt();
  71.     int  results;
  72.     results=no1+2;
  73.             System.out.println("Results: " + results);
  74.  
  75.             String name1 = dis.readUTF();
  76.             System.out.println("Name: " + name1);
  77.  
  78.  
  79.         } catch (IOException e) {
  80.             e.printStackTrace();
  81.         }
  82.     }
  83. }