diff --git a/Seminar 5/code/Project 5/.idea/.gitignore b/Seminar 5/code/Project 5/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/Seminar 5/code/Project 5/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/Seminar 5/code/Project 5/.idea/misc.xml b/Seminar 5/code/Project 5/.idea/misc.xml new file mode 100644 index 0000000..03f397c --- /dev/null +++ b/Seminar 5/code/Project 5/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Seminar 5/code/Project 5/.idea/modules.xml b/Seminar 5/code/Project 5/.idea/modules.xml new file mode 100644 index 0000000..53a8812 --- /dev/null +++ b/Seminar 5/code/Project 5/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Seminar 5/code/Project 5/.idea/vcs.xml b/Seminar 5/code/Project 5/.idea/vcs.xml new file mode 100644 index 0000000..c2365ab --- /dev/null +++ b/Seminar 5/code/Project 5/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Seminar 5/code/Project 5/Project 5.iml b/Seminar 5/code/Project 5/Project 5.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Seminar 5/code/Project 5/Project 5.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Seminar 5/code/Project 5/file1.txt b/Seminar 5/code/Project 5/file1.txt new file mode 100644 index 0000000..6baeab4 --- /dev/null +++ b/Seminar 5/code/Project 5/file1.txt @@ -0,0 +1 @@ +Some text here \ No newline at end of file diff --git a/Seminar 5/code/Project 5/myfile.txt b/Seminar 5/code/Project 5/myfile.txt new file mode 100644 index 0000000..b8e9c4d Binary files /dev/null and b/Seminar 5/code/Project 5/myfile.txt differ diff --git a/Seminar 5/code/Project 5/out/production/Project 5/Main.class b/Seminar 5/code/Project 5/out/production/Project 5/Main.class new file mode 100644 index 0000000..243905d Binary files /dev/null and b/Seminar 5/code/Project 5/out/production/Project 5/Main.class differ diff --git a/Seminar 5/code/Project 5/out/production/Project 5/Student.class b/Seminar 5/code/Project 5/out/production/Project 5/Student.class new file mode 100644 index 0000000..25e8a46 Binary files /dev/null and b/Seminar 5/code/Project 5/out/production/Project 5/Student.class differ diff --git a/Seminar 5/code/Project 5/src/Main.java b/Seminar 5/code/Project 5/src/Main.java new file mode 100644 index 0000000..39f8646 --- /dev/null +++ b/Seminar 5/code/Project 5/src/Main.java @@ -0,0 +1,74 @@ +import java.io.*; + +public class Main { + private static final String FILE_NAME = "myfile.txt"; + + public static void main(String[] args) { +// Student s1 = new Student(); +// s1.setFacultyNumber("123124"); +// s1.setName("Bai Ivan"); +// s1.setEmail("BaiIvan@abv.bg"); + + Student s2 = deserializeStudent(); + System.out.println(s2.getEmail()); + } + + public static void externalizeTeacher(Teacher teacher) { + try (FileOutputStream fileOutput = new FileOutputStream(FILE_NAME); + ObjectOutputStream objectOut = new ObjectOutputStream(fileOutput);) { + objectOut.writeObject(teacher); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static Student deserializeStudent() { + Student result = null; + try (FileInputStream fileOutput = new FileInputStream(FILE_NAME); + ObjectInputStream objectOut = new ObjectInputStream(fileOutput);) { + result = (Student)objectOut.readObject(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + + return result; + } + + public static void serializeStudent(Student student) { + try (FileOutputStream fileOutput = new FileOutputStream(FILE_NAME); + ObjectOutputStream objectOut = new ObjectOutputStream(fileOutput);) { + objectOut.writeObject(student); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void writeToAFile(String fileName) { + try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName))) { + String fileContent = "Some text here"; + bufferedWriter.write(fileContent); + } catch (IOException e) { + // Exception handling​ + } + } + + public static String readStringFromAFile(String fileName) { + String result = ""; + try (BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName))) { + String line = bufferedReader.readLine(); + while (line != null) { + System.out.println("I am from 25: " + line); + result += line; + line = bufferedReader.readLine(); + } + } catch (FileNotFoundException e) { + // Exception handling + } catch (IOException e) { + // Exception handling + } + + return result; + } +} \ No newline at end of file diff --git a/Seminar 5/code/Project 5/src/Student.java b/Seminar 5/code/Project 5/src/Student.java new file mode 100644 index 0000000..c6de30b --- /dev/null +++ b/Seminar 5/code/Project 5/src/Student.java @@ -0,0 +1,31 @@ +import java.io.Serializable; + +public class Student implements Serializable { + private String facultyNumber; + private String name; + private String email; + + public String getFacultyNumber() { + return facultyNumber; + } + + public void setFacultyNumber(String facultyNumber) { + this.facultyNumber = facultyNumber; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } +} diff --git a/Seminar 5/code/Project 5/src/Teacher.java b/Seminar 5/code/Project 5/src/Teacher.java new file mode 100644 index 0000000..9bdbc0a --- /dev/null +++ b/Seminar 5/code/Project 5/src/Teacher.java @@ -0,0 +1,35 @@ +import java.io.Externalizable; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; + +public class Teacher implements Externalizable { + private String name; + private boolean isStupid; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public boolean isStupid() { + return isStupid; + } + + public void setStupid(boolean stupid) { + isStupid = stupid; + } + + @Override + public void writeExternal(ObjectOutput out) throws IOException { + out.writeUTF(this.getName()); + } + + @Override + public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { + this.setName(in.readUTF()); + } +}