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

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 2.28 KB  |  hits: 16  |  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 teht9;
  6.  
  7. import java.io.FileInputStream;
  8. import java.io.FileOutputStream;
  9. import java.io.ObjectInputStream;
  10. import java.io.ObjectOutputStream;
  11. import java.util.ArrayList;
  12. import java.util.Scanner;
  13.  
  14. /**
  15.  *
  16.  * @author Admin
  17.  */
  18. public class Valtiotesti {
  19.  
  20.     public void Operoi(){
  21.             int asluku;
  22.         String pk, v;
  23.         Scanner lukija = new Scanner(System.in);
  24.         ArrayList <Valtio> valtiot = new ArrayList<Valtio>();
  25.        
  26.         try{
  27.             FileInputStream tiedosto = new FileInputStream("C:\\Users\\Public\\Pictures\\valtiot.dat");
  28.             ObjectInputStream syöte = new ObjectInputStream(tiedosto);
  29.             valtiot = (ArrayList) syöte.readObject();
  30.             syöte.close();
  31.         }catch(Exception e){
  32.             System.out.println("Tiedostoa ei löytynyt");
  33.         }
  34.         do{
  35.             System.out.print("Anna valtion nimi: ");
  36.             v = lukija.nextLine();
  37.            
  38.             if("".equals(v))
  39.                 break;
  40.                         else{
  41.                               System.out.print("Anna pääkaupungin nimi: ");
  42.                               pk = lukija.nextLine();
  43.                               System.out.print("Asukasluku: ");
  44.                               asluku = lukija.nextInt();
  45.                               lukija.nextLine();
  46.                              
  47.                               Valtio valtio = new Valtio(v,pk,asluku);
  48.                                  valtiot.add(valtio);
  49.                
  50.             }
  51.            
  52.         }while(!"".equals(v));
  53.         System.out.println("-------------------------------");
  54.         for(Valtio valtio: valtiot){
  55.             String toString = valtio.toString();
  56.             System.out.println(toString);
  57.         }
  58.        
  59.         try{
  60.             FileOutputStream tiedosto = new FileOutputStream("C:\\Users\\Public\\Pictures\\valtiot.dat");
  61.             ObjectOutputStream syöte = new ObjectOutputStream(tiedosto);
  62.             syöte.writeObject(valtiot);
  63.             syöte.close();
  64.            
  65.         }
  66.         catch(Exception e){
  67.             System.out.println("Tiedostoa ei löytynyt");
  68.         }
  69.        
  70.     }
  71.  
  72. }