Advertisement
GraionDilach

Java.IO #2

Nov 28th, 2012
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. package Advanced;
  2.  
  3. import java.io.*;
  4. import java.util.Random;
  5.  
  6. public class MainIO {
  7.  
  8.     /**
  9.      * @param args
  10.      */
  11.     public static void main(String[] args) {
  12.         // TODO Auto-generated method stub
  13.  
  14.         try {
  15.            
  16.             //1. feladat
  17.             ObjectOutputStream IOExample11 = new ObjectOutputStream (new FileOutputStream ("d:/zimmgy/AIOex1.bin"));
  18.             Random R = new Random();
  19.             int[] Marks;
  20.             String[] ListNames =  new String[]{"Zimmermann Gyula", "Graion Dilach", "Junichi Nakatsuru"};
  21.             Tanulo[] ListTanulos = new Tanulo[ListNames.length];
  22.             for (int j = 0; j<ListTanulos.length; j++){
  23.                 Marks = new int[4];
  24.                 for (int i = 0; i < 4; i++) {
  25.                     do {
  26.                         Marks[i] = (R.nextInt()%5)+1;
  27.                     } while (Marks[i] < 1);
  28.                 }
  29.                 ListTanulos[j] = new Tanulo(ListNames[j], Marks);
  30.                 System.out.println(ListTanulos[j].toString());
  31.                 IOExample11.writeObject(ListTanulos[j]);
  32.             }
  33.             IOExample11.close();
  34.             ObjectInputStream IOExample12 = new ObjectInputStream (new FileInputStream ("d:/zimmgy/AIOex1.bin"));
  35.            
  36.             for (int j = 0; j < ListTanulos.length; j++){
  37.                 System.out.println(((Tanulo)(IOExample12.readObject())).toString());
  38.             }
  39.            
  40.            
  41.             //2. feladat
  42.             //todo- EOL hülye --'
  43.             StreamTokenizer IOExample2 = new StreamTokenizer (new FileReader ("d:/zimmgy/AIOex1.bin"));
  44.             int WordCount =0, NumberCount=0;
  45.             int temp;
  46.             while ((temp = IOExample2.nextToken()) != StreamTokenizer.TT_EOF){
  47.                 if(temp == StreamTokenizer.TT_WORD){
  48.                     WordCount++;
  49.                 }else{
  50.                     NumberCount++;
  51.                 }
  52.                
  53.             }
  54.            
  55.             System.out.println("Szavak száma: " + WordCount + ", számok száma : " + NumberCount);
  56.            
  57.             //3. feladat
  58.            
  59.             File Example = new File("d:/zimmgy");
  60.             String[] StuffInIt = Example.list();
  61.             for (int i=0; i<StuffInIt.length; i++){
  62.                 System.out.println(StuffInIt[i]);
  63.             }
  64.            
  65.         }
  66.         catch (Exception e){
  67.             System.out.println ("Who cares, let's annihilate them!");
  68.         }
  69.     }
  70.  
  71. }
  72.  
  73.  
  74. =============================================================================================
  75.  
  76. package Advanced;
  77.  
  78. import java.io.Serializable;
  79.  
  80. @SuppressWarnings("serial")
  81. public class Tanulo implements Serializable{
  82.  
  83.     String Name;
  84.     int[] Marks;
  85.    
  86.     Tanulo (String _Name, int[] _Marks){
  87.         Name = _Name;
  88.         Marks = _Marks;
  89.         }
  90.    
  91.     public String toString(){
  92.         return Name + ", jegyei: " + Marks[0] + ", " + Marks[1]
  93.                 + ", " + Marks[2] + ", " + Marks[3] + ", ";
  94.     }
  95.    
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement