Advertisement
Varasku

Pliki randomowe zadanie

Jan 17th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. package plikirandomowezad;
  2.  
  3. import java.io.IOException;
  4. import java.io.RandomAccessFile;
  5. import java.util.Random;
  6. import java.util.Scanner;
  7.  
  8.  
  9. public class Plikirandomowezad {
  10.  
  11. public static void main(String[] args) {
  12. Random r = new Random();
  13.  
  14. int parzysta = 0;
  15. int podzielna = 0;
  16. int wsz = 0;
  17. int liczba = 0;
  18. int suma= 0;
  19.  
  20. try {
  21.  
  22. RandomAccessFile f = new RandomAccessFile("D:/dane.dat", "rw");
  23. for (int i=0;i<20;i++){
  24. liczba = r.nextInt(20);
  25. f.writeInt(liczba);
  26. }
  27. f.seek(0);
  28. while(f.getFilePointer()<f.length()){
  29. liczba=f.readInt();
  30.  
  31. System.out.print(liczba+", ");
  32. }
  33. System.out.println();
  34.  
  35. f.seek(0);
  36. while(f.getFilePointer()<f.length()){
  37. liczba=f.readInt();
  38. if(liczba%2==0){
  39.  
  40. System.out.print(liczba+", ");
  41. f.seek(f.getFilePointer()-4);
  42. f.writeInt(liczba);
  43. }
  44. }
  45.  
  46. f.seek(0);
  47. while(f.getFilePointer()<f.length()){
  48. liczba=f.readInt();
  49. if(liczba%3==0){
  50. System.out.print("Liczba podzielna przez trzy:"+liczba+"\n");
  51. f.seek(f.getFilePointer()-4);
  52. f.writeInt(liczba);
  53. }
  54. }
  55.  
  56.  
  57. f.close();
  58. } catch (IOException ex) {
  59. System.out.println("Błąd pliku");
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement