Advertisement
lashrone1

file_work

Nov 27th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. import java.sql.SQLOutput;
  4. import java.util.NoSuchElementException;
  5. import java.util.Scanner;
  6.  
  7. class Main{
  8.  
  9. static Scanner sc = new Scanner(System.in);
  10.  
  11. static int linenumber(){
  12. File myFile =new File("List.txt");
  13. FileReader fileReader = null;
  14. try {
  15. fileReader = new FileReader(myFile);
  16. } catch (FileNotFoundException e) {
  17. e.printStackTrace();
  18. }
  19. LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
  20. int lineNumber = 0;
  21. while (true){
  22. try {
  23. if (!(lineNumberReader.readLine() != null)) break;
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. lineNumber++;
  28. }
  29. return lineNumber;
  30. }
  31.  
  32. public static void main(String[] args){
  33.  
  34.  
  35. /*
  36. FileInputStream
  37. -//- Output -//-
  38. Data -//-
  39.  
  40. RandomAccessFile raf = new RandomAccessFile("name", "r");// аналогичен DataOutputStream r == read, rw == read/write
  41. "rw"
  42. raf.seek(arg0); //указатель в файле будет сдвинут на опр. кол-во байт
  43. raf.length();
  44. raf.setLength(arg0); //какую длину задать
  45. raf.write //паредает 1 байт
  46. writeInt
  47. writeUTF //кодировка
  48. raf.read
  49. readInt
  50.  
  51. Scanner scan = new Scanner(new File("имя"));
  52. ("строка 1 2 3");
  53. scannext()// получим "строку", "1", "2", "3"
  54. scan.nextLine
  55. next()
  56. scan.hasNext();
  57.  
  58. */
  59.  
  60.  
  61. Stud[] stud = new Stud [linenumber()];
  62. for(int i=0; i<stud.length-1; i++) {
  63. stud[i]=new Stud();
  64. stud[i].scan(i);
  65. }
  66.  
  67. for(int i=0; i<stud.length-1; i++) {
  68. stud[i].print();
  69. }
  70.  
  71. try (RandomAccessFile raf = new RandomAccessFile("List.bin", "rw")) {
  72.  
  73. for(int i = 0; i<stud.length-1; i++) {
  74. String n = stud[i].name;
  75. String n1 = String.valueOf(stud[i].val);
  76. String n2 = String.valueOf(stud[i].year);
  77. String obj = n + " " + " " + n1 + " " + n2;
  78. raf.write(obj.getBytes());
  79. raf.writeBytes(System.getProperty("line.separator"));
  80. }
  81. } catch (FileNotFoundException e) {
  82. e.printStackTrace();
  83. } catch (IOException e) {
  84. e.printStackTrace();
  85. }
  86.  
  87. try (RandomAccessFile raf = new RandomAccessFile("List.bin", "r")) {
  88. for(int i = 0 ; i < 2; i++)
  89. raf.readLine();
  90. System.out.println(raf.readLine());
  91. } catch (FileNotFoundException e) {
  92. e.printStackTrace();
  93. } catch (IOException e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. }
  98.  
  99. class Stud {
  100. public String name;
  101. public double val;
  102. public int year;
  103.  
  104. /* 1. используя scanner считать
  105. * из Lisc.txt и записать в массив объектов
  106. * 2. исп. Raf записать всех в бинарный файл
  107. * 3. Вывести на экран 3-й элемент из бинарного
  108. */
  109.  
  110.  
  111. void print() {
  112. System.out.println(name + " " + val + " " + " " + year);
  113. }
  114.  
  115.  
  116. void scan(int index) {
  117. index++;
  118. try {
  119. Scanner scan = new Scanner(new File("List.txt"));
  120. for (int i = 0; i < index; i++) {
  121. scan.nextLine();
  122. }
  123. objEl(scan);
  124. } catch (FileNotFoundException e) {
  125. e.printStackTrace();
  126. }catch (NoSuchElementException er){}
  127.  
  128. }
  129.  
  130. void objEl(Scanner scanner) {
  131. String delimeter = " "; // Разделитель
  132. String[] subStr;
  133. // Разбить строку str с порогом равным 3, который означает, как много подстрок, должно быть возвращено.
  134. // Вывод результата на экран
  135.  
  136. subStr = scanner.nextLine().split(delimeter);
  137.  
  138. for (int i = 0; i < subStr.length; i++) {
  139.  
  140. if (i == 0)
  141. name = String.valueOf(subStr[i]);
  142. if (i == 1)
  143. val = Double.parseDouble(subStr[i]);
  144. if (i == 2)
  145. year = Integer.parseInt(subStr[i]);
  146.  
  147.  
  148. }
  149.  
  150.  
  151. }
  152. //public void write(DataOutput output){}
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement