Advertisement
polpoteu

JAVA Działania na plikach

Jan 17th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. package e9randomjava;
  2.  
  3. import java.io.IOException;
  4. import java.io.RandomAccessFile;
  5. import java.util.Random;
  6.  
  7. public class E9RandomJava {
  8. public static void main(String[] args) {
  9. Random e = new Random();
  10. Runtime r = Runtime.getRuntime();
  11. int liczba = 0;
  12. int suma = 0;
  13. double srednia = 0;
  14. try {
  15. RandomAccessFile f = new RandomAccessFile("D:/dane.dat", "rw");
  16. for (int i=0;i<20;i++){
  17. liczba = e.nextInt(100);
  18. f.writeInt(liczba);
  19. }
  20. f.seek(0);
  21. while(f.getFilePointer()<f.length()){
  22. liczba=f.readInt();
  23. suma+=liczba;
  24. System.out.print(liczba+", ");
  25. }
  26. System.out.println();
  27. srednia=suma/10;
  28. System.out.println("Śrenia = "+srednia);
  29. f.seek(0);
  30. while(f.getFilePointer()<f.length()){
  31. liczba=f.readInt();
  32. if(liczba>srednia){
  33. liczba=-liczba;
  34. f.seek(f.getFilePointer()-4);
  35. f.writeInt(liczba);
  36. }
  37. }
  38. f.seek(0);
  39. while(f.getFilePointer()<f.length()){
  40. liczba=f.readInt();
  41. System.out.print(liczba+", ");
  42. }
  43. //sprawdzanie parzystych
  44. int p=0;
  45. f.seek(0);
  46. while(f.getFilePointer()<f.length()){
  47. liczba=f.readInt();
  48. if (liczba % 2 == 0){
  49. p = p+1;
  50.  
  51. }
  52. System.out.print(liczba+", ");
  53.  
  54. }
  55. System.out.println("Przystych jest:"+p+" ");
  56. //sprawdzanie podzielności przez 3
  57. int t=0;
  58. f.seek(0);
  59. while(f.getFilePointer()<f.length()){
  60. liczba=f.readInt();
  61. if (liczba % 3 == 0){
  62. t = t+1;
  63.  
  64. }
  65. System.out.print(liczba+", ");
  66.  
  67. }
  68. System.out.println("Liczb podzielnych przez 3 jest:"+p+" ");
  69. f.close();
  70. } catch (IOException ex) {
  71. System.out.println("Błąd pliku");
  72. }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement