Advertisement
Guest User

Przykład zastosowania RandomAccessFile

a guest
Dec 19th, 2016
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.77 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.IOException;
  4. import java.io.RandomAccessFile;
  5.  
  6. public class Main {
  7.  
  8.     public static void write_raf(String path) throws IOException {
  9.         RandomAccessFile file = null;
  10.         try {
  11.             // "r", "rw", "rws", "rwd"
  12.             file = new RandomAccessFile(path, "rw");
  13.  
  14.             String imie = "Adam";
  15.             String nazwisko = "Kowalski";
  16.             int wiek = 50;
  17.             char plec = 'M';
  18.             double pensja = 2000;
  19.  
  20.             file.writeUTF(imie);
  21.             file.writeUTF(nazwisko);
  22.             file.writeInt(wiek);
  23.             file.writeChar(plec);
  24.             file.writeDouble(pensja);
  25.  
  26.             imie = "Julia";
  27.             nazwisko = "Nowak";
  28.             wiek = 30;
  29.             plec = 'K';
  30.             pensja = 2000;
  31.  
  32.             file.writeUTF(imie);
  33.             file.writeUTF(nazwisko);
  34.             file.writeInt(wiek);
  35.             file.writeChar(plec);
  36.             file.writeDouble(pensja);
  37.  
  38.         } finally {
  39.             if (file != null) {
  40.                 file.close();
  41.             }
  42.         }
  43.     }
  44.  
  45.     public static void read_raf(String path) throws IOException {
  46.         RandomAccessFile file = null;
  47.         try {
  48.             // "r", "rw", "rws", "rwd"
  49.             file = new RandomAccessFile(path, "rw");
  50.  
  51.             while (file.getFilePointer() < file.length()) {
  52.                 String imie = file.readUTF();
  53.                 String nazwisko = file.readUTF();
  54.                 int wiek = file.readInt();
  55.                 char plec = file.readChar();
  56.                 double pensja = file.readDouble();
  57.  
  58.                 System.out.printf("Imię: %s\nNazwisko: %s\nWiek: %d\nPensja: %.2f\n", imie, nazwisko, wiek, pensja);
  59.             }
  60.  
  61.         } finally {
  62.             if (file != null) {
  63.                 file.close();
  64.             }
  65.         }
  66.     }
  67.  
  68.     public static void modify_raf(String path) throws IOException {
  69.         RandomAccessFile file = null;
  70.         try {
  71.             // "r", "rw", "rws", "rwd"
  72.             file = new RandomAccessFile(path, "rw");
  73.  
  74.             while (file.getFilePointer() < file.length()) {
  75.                 String imie = file.readUTF();
  76.                 String nazwisko = file.readUTF();
  77.                 int wiek = file.readInt();
  78.                 char plec = file.readChar();
  79.                 long poz = file.getFilePointer();
  80.                 double pensja = file.readDouble();
  81.  
  82.                 if (plec == 'K') {
  83.                     pensja = pensja * 2.0;
  84.  
  85.                     file.seek(poz);
  86.                     file.writeDouble(pensja);
  87.                 }
  88.             }
  89.  
  90.         } finally {
  91.             if (file != null) {
  92.                 file.close();
  93.             }
  94.         }
  95.     }
  96.  
  97.     public static void complex_mult(double [] u, double [] v, double [] res) {
  98.         double a = u[0], b = u[1], c = v[0], d = v[1];
  99.         res[0] = a * c - b * d;
  100.         res[1] = a * d + b * c;
  101.     }
  102.  
  103.     public static void complex_add(double [] u, double [] v, double [] res) {
  104.         double a = u[0], b = u[1], c = v[0], d = v[1];
  105.         res[0] = a + c;
  106.         res[1] = b + d;
  107.     }
  108.  
  109.     public static double complex_abs(double [] u) {
  110.         return Math.sqrt(u[0] * u[0] + u[1] * u[1]);
  111.     }
  112.  
  113.     public static void write_julia(String path) throws IOException {
  114.         RandomAccessFile plik = null;
  115.         try {
  116.             plik = new RandomAccessFile(path, "rw");
  117.  
  118.             int w = 800;
  119.             int h = 800;
  120.  
  121.             double re_min = -2.0;
  122.             double re_max = 2.0;
  123.             double im_min = -2.0;
  124.             double im_max = 2.0;
  125.  
  126.             double [] c = new double [] { 0.0, 0.67 };
  127.  
  128.             plik.writeBytes("P5\n# Julia image\n");
  129.             plik.writeBytes(w + " " + h + " 255\n");
  130.  
  131.             for (int i = 0; i < h; i++) {
  132.                 double im = (im_max - im_min) / h * i + im_min;
  133.                 for (int j = 0; j < w; j++) {
  134.                     double re = (re_max - re_min) / w * j + re_min;
  135.                     double [] z = new double [] { re, im };
  136.                     int n = 255;
  137.                     while ( complex_abs(z) < 10 && n >= 5 ) {
  138.                         complex_mult(z, z, z);
  139.                         complex_add(z, c, z);
  140.                         n -= 5;
  141.                     }
  142.                     plik.writeByte(n);
  143.                 }
  144.             }
  145.  
  146.         } finally {
  147.             if (plik != null) {
  148.                 plik.close();
  149.             }
  150.         }
  151.     }
  152.  
  153.  
  154.     public static void write_circle(String path) throws IOException {
  155.         RandomAccessFile plik = null;
  156.         try {
  157.             plik = new RandomAccessFile(path, "rw");
  158.             plik.writeBytes("P5\n");
  159.             int promien = 200;
  160.             plik.writeBytes((promien*2) + " " + (promien*2) + " 255\n");
  161.  
  162.             int k = 0;
  163.             for (int i = 0; i < 2 * promien; i++) {
  164.                 for (int j = 0; j < 2 * promien; j++) {
  165.                     int x = i - promien;
  166.                     int y = j - promien;
  167.                     double odl = Math.sqrt(x * x + y * y);
  168.                     //double v = odl < promien ? 1 : 0 ;
  169.                     double v = (promien - odl) / promien;
  170.                     byte kolor = (byte)Math.max(0, Math.min(255, v * 255));
  171.                     plik.writeByte(kolor);
  172.                     ++k;
  173.                 }
  174.             }
  175.         } finally {
  176.             if (plik != null) {
  177.                 plik.close();
  178.             }
  179.         }
  180.     }
  181.  
  182.     public static void main(String[] args) throws IOException {
  183.         write_raf("dane.dat");
  184.  
  185.         modify_raf("dane.dat");
  186.  
  187.         System.out.println("Po modyfikacji: ");
  188.         read_raf("dane.dat");
  189.  
  190.         write_circle("circle.pgm");
  191.         write_julia("julia.pgm");
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement