Advertisement
Guest User

Serialization

a guest
Feb 6th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.60 KB | None | 0 0
  1.  
  2. package streams2;
  3.  
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9.  
  10. public class app {
  11.     public static void main(String[] args) throws IOException {
  12.         //macierz();
  13.         //echo();
  14.         bitFileCopy("t.png", "t2.png");
  15.     }
  16.    
  17.     private static void echo() throws IOException
  18.     {
  19.         TextRew t = new TextRew();
  20.         while(!t.stop())
  21.         {
  22.             t.readLine();
  23.             t.printR();
  24.         }
  25.     }
  26.    
  27.     private static void macierz() throws IOException
  28.     {
  29.         Macierz m = new Macierz();
  30.         m.printToFile();
  31.         m.printAv();
  32.         m.readFromFile();
  33.         m.printAv();
  34.         m.serializeToFile();
  35.         m.deserializeFromFile();
  36.         //m.bitFileCopy("CloneFile.txt");
  37.     }
  38.         public static int bitFileCopy(String fName, String toFile) throws FileNotFoundException, IOException
  39.     {
  40.         File from = new File(fName);
  41.         File target = new File(toFile);
  42.         FileInputStream in = new FileInputStream (from);
  43.         FileOutputStream out = new FileOutputStream (target);
  44.         int counter = 0;
  45.         int triger = 0;
  46.         byte [] b = new byte [1024];
  47.         do
  48.         {
  49.             triger = in.read(b);
  50.             if (triger > 0)
  51.             {
  52.                 counter += triger;
  53.                 out.write(b);
  54.             }
  55.         }
  56.         while(triger != -1);
  57.         in.close();
  58.         out.close();
  59.         return counter;
  60.     }
  61.  
  62. }
  63. package streams2;
  64.  
  65. import java.io.*;
  66. import java.util.Random;
  67. import java.util.Scanner;
  68.  
  69. public class Macierz implements Serializable
  70. {
  71.     protected double[][] mac;
  72.     protected File sFile;
  73.     public File bitFile;
  74.    
  75.     public Macierz()
  76.     {
  77.         sFile = new File("Macierz.txt");
  78.         bitFile = new File("bitFile.txt");
  79.         mac = new double[5][];
  80.         for (int i = 0; i < 5; i++)
  81.         {
  82.             mac[i] = new double[5];
  83.         }
  84.         Random r = new Random();
  85.         for (int i = 0; i < 5; i++)
  86.         {
  87.             for (int j = 0; j < 5; j++)
  88.             {
  89.                 mac[i][j] = r.nextDouble()* 100;
  90.             }
  91.         }
  92.     }
  93.    
  94.     public void printToFile() throws IOException
  95.     {
  96.         BufferedWriter w = new BufferedWriter(new FileWriter(sFile));
  97.         w.write("Macierz");
  98.         w.newLine();
  99.         w.write("" + mac.length);
  100.         w.newLine();
  101.         w.write("" + mac[0].length);
  102.         w.newLine();
  103.         for (int i = 0; i < mac.length; i++)
  104.         {
  105.             for (int j = 0; j < mac[i].length; j++)
  106.             {
  107.                 w.write(String.format("%.2f\t", mac[i][j]));
  108.             }
  109.             w.newLine();
  110.         }
  111.         //System.out.println(sFile.getCanonicalPath());  
  112.         w.close();
  113.     }
  114.    
  115.     public void readFromFile() throws IOException
  116.     {
  117.         FileReader r = new FileReader(sFile);
  118.         Scanner s = new Scanner(sFile);
  119.         s.nextLine();
  120.         int w = s.nextInt(), k = s.nextInt();
  121.         mac = new double [w][];
  122.         for (int i = 0; i < w; i++)
  123.         {
  124.             mac[i] = new double[k];
  125.         }
  126.         for(int i = 0; i < w; i++)
  127.             for(int j = 0; j < k; j++)
  128.             {
  129.                 mac[i][j] = s.nextDouble();
  130.             }
  131.     }
  132.    
  133.     public void printAv()
  134.     {
  135.         PrintWriter w = new PrintWriter(System.out, true);
  136.         double a = 0;
  137.         for(int i = 0; i < 5; i++)
  138.         {
  139.             for(int j = 0; j < 5; j++)
  140.             {
  141.                 a += mac[i][j];
  142.             }
  143.         }
  144.         w.println("" + a/25);
  145.     }
  146.    
  147.     public void serializeToFile() throws IOException
  148.     {
  149.         FileOutputStream w = new FileOutputStream(bitFile);
  150.         DataOutputStream out = new DataOutputStream(w);
  151.         out.writeInt(mac.length);
  152.         out.writeInt(mac[0].length);
  153.         for(int i = 0; i < mac.length; i++)
  154.             for(int j = 0; j < mac[i].length; j++)
  155.                 out.writeDouble(mac[i][j]);
  156.         out.close();
  157.         w.close();
  158.     }
  159.    
  160.     public void deserializeFromFile() throws IOException
  161.     {
  162.         FileInputStream wej = new FileInputStream(bitFile);
  163.         DataInputStream in = new DataInputStream(wej);
  164.         int w = in.readInt();
  165.         int k = in.readInt();
  166.         mac = new double[w][];
  167.         for(int i = 0; i < w; i++)
  168.         {
  169.             mac[i] = new double[k];
  170.             for(int j = 0; j < k; j++)
  171.                 mac[i][j] = in.readDouble();
  172.         }
  173.         in.close();
  174.         wej.close();
  175.     }
  176.    
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement