DataOutputStream code example

Example 1: java fileinputstream

try{    
            FileInputStream fin=new FileInputStream("D:\\testout.txt");    
            int i=0;    
            while((i=fin.read())!=-1){    
             System.out.print((char)i);    
            }    
            fin.close();    
          }catch(Exception e){System.out.println(e);}

Example 2: objectoutputstream exemple

public static void main(String[] args) throws IOException, ClassNotFoundException {
  Obj array[] = new Obj[10];
  for (int i = 0; i < array.length; i++) {
    array[i] = new Emp(i);
  }
  try {
    ObjectOutputStream fOUT = new ObjectOutputStream(new FileOutputStream("prodotti.dat"));
    for (int i = 0; i < array.length; i++) {
      fOUT.writeObject(array[i]);
    }
    fOUT.close();
  } catch (IOException e) {
    System.out.println("Errore nella scrittura");
  }
  ObjectInputStream fIN = new ObjectInputStream(new FileInputStream("prodotti.dat"));
  while (true) {
    try {
      Obj p = (Obj) fIN.readObject();
      System.out.println(Obj.method());
    } catch (EOFException e) {
      break;
    }
  }
}

Tags:

Java Example