/
GermanezZ
/
JavaExercises
Обзор
Документация
Войти
/
GermanezZ
/
JavaExercises
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
AllMetanitLessons/InputOutoutStreamsFolder/Data/InOutClass.java
67 строк
2 KB
germa
Уроки по Metanit
04 дек 2024, 20:39
04 дек 2024, 20:39
48d575a
Код
Авторство
О чём код?
package AllMetanitLessons.InputOutoutStreamsFolder.Data; import java.io.*; // D://Files//data.bin public class InOutClass { public static void main(String[] args) { Person Tom = new Person("Tom",20, 1.70,false); try(DataOutputStream dos = new DataOutputStream(new FileOutputStream("D://Files//data.bin"))){ // ЕСЛИ В РАЗНЫХ ПАКЕТАХ, ТО ИСПОЛЬЗОВАТЬ ГЕТЫ, ЕСЛИ В ОДНОМ, ТО МОЖНО И ПО ИМЕНИ dos.writeUTF(Tom.getName()); dos.writeInt(Tom.getAge()); dos.writeDouble(Tom.getHeight()); dos.writeBoolean(Tom.isMarried()); }catch (IOException exc){ System.out.println(exc.getMessage()); } try(DataInputStream dis = new DataInputStream(new FileInputStream("D://Files//data.bin"))) { String name = dis.readUTF(); int age = dis.readInt(); double height = dis.readDouble(); boolean isMarried = dis.readBoolean(); System.out.printf("Name: %s, age: %d, height: %.2f, isMarried: %b", name, age, height, isMarried); }catch (IOException exc){ System.out.println(exc.getMessage()); } } } class Person{ private String name; private int age; private double height; private boolean isMarried; public String getName() { return name; } public int getAge() { return age; } public double getHeight() { return height; } public boolean isMarried() { return isMarried; } public Person(String name, int age, double height, boolean isMarried) { this.name = name; this.age = age; this.height = height; this.isMarried = isMarried; } }