Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.sql.SQLOutput;
- import java.util.NoSuchElementException;
- import java.util.Scanner;
- class Main{
- static Scanner sc = new Scanner(System.in);
- static int linenumber(){
- File myFile =new File("List.txt");
- FileReader fileReader = null;
- try {
- fileReader = new FileReader(myFile);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
- int lineNumber = 0;
- while (true){
- try {
- if (!(lineNumberReader.readLine() != null)) break;
- } catch (IOException e) {
- e.printStackTrace();
- }
- lineNumber++;
- }
- return lineNumber;
- }
- public static void main(String[] args){
- /*
- FileInputStream
- -//- Output -//-
- Data -//-
- RandomAccessFile raf = new RandomAccessFile("name", "r");// аналогичен DataOutputStream r == read, rw == read/write
- "rw"
- raf.seek(arg0); //указатель в файле будет сдвинут на опр. кол-во байт
- raf.length();
- raf.setLength(arg0); //какую длину задать
- raf.write //паредает 1 байт
- writeInt
- writeUTF //кодировка
- raf.read
- readInt
- Scanner scan = new Scanner(new File("имя"));
- ("строка 1 2 3");
- scannext()// получим "строку", "1", "2", "3"
- scan.nextLine
- next()
- scan.hasNext();
- */
- Stud[] stud = new Stud [linenumber()];
- for(int i=0; i<stud.length-1; i++) {
- stud[i]=new Stud();
- stud[i].scan(i);
- }
- for(int i=0; i<stud.length-1; i++) {
- stud[i].print();
- }
- try (RandomAccessFile raf = new RandomAccessFile("List.bin", "rw")) {
- for(int i = 0; i<stud.length-1; i++) {
- String n = stud[i].name;
- String n1 = String.valueOf(stud[i].val);
- String n2 = String.valueOf(stud[i].year);
- String obj = n + " " + " " + n1 + " " + n2;
- raf.write(obj.getBytes());
- raf.writeBytes(System.getProperty("line.separator"));
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try (RandomAccessFile raf = new RandomAccessFile("List.bin", "r")) {
- for(int i = 0 ; i < 2; i++)
- raf.readLine();
- System.out.println(raf.readLine());
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- class Stud {
- public String name;
- public double val;
- public int year;
- /* 1. используя scanner считать
- * из Lisc.txt и записать в массив объектов
- * 2. исп. Raf записать всех в бинарный файл
- * 3. Вывести на экран 3-й элемент из бинарного
- */
- void print() {
- System.out.println(name + " " + val + " " + " " + year);
- }
- void scan(int index) {
- index++;
- try {
- Scanner scan = new Scanner(new File("List.txt"));
- for (int i = 0; i < index; i++) {
- scan.nextLine();
- }
- objEl(scan);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }catch (NoSuchElementException er){}
- }
- void objEl(Scanner scanner) {
- String delimeter = " "; // Разделитель
- String[] subStr;
- // Разбить строку str с порогом равным 3, который означает, как много подстрок, должно быть возвращено.
- // Вывод результата на экран
- subStr = scanner.nextLine().split(delimeter);
- for (int i = 0; i < subStr.length; i++) {
- if (i == 0)
- name = String.valueOf(subStr[i]);
- if (i == 1)
- val = Double.parseDouble(subStr[i]);
- if (i == 2)
- year = Integer.parseInt(subStr[i]);
- }
- }
- //public void write(DataOutput output){}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement