// PISANJE U FILE - prepisuje postojeći sadržaj Path path = Path.of("podaci.txt"); try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) { writer.write("Prvi red"); writer.newLine(); writer.write("Drugi red"); writer.newLine(); } catch (IOException e) { System.out.println(e.getMessage()); } // PISANJE U FILE - dodavanje na kraj Path path = Path.of("podaci.txt"); try (BufferedWriter writer = Files.newBufferedWriter( path, StandardCharsets.UTF_8, StandardOpenOption.APPEND )) { writer.write("Novi red"); writer.newLine(); } catch (IOException e) { System.out.println(e.getMessage()); } // BRZO PISANJE CIJELOG TEKSTA Path path = Path.of("podaci.txt"); try { Files.writeString(path, "Ovo je tekst\nDrugi red", StandardCharsets.UTF_8); } catch (IOException e) { System.out.println(e.getMessage()); } // BRZO DODAVANJE TEKSTA NA KRAJ Path path = Path.of("podaci.txt"); try { Files.writeString( path, "Novi red\n", StandardCharsets.UTF_8, StandardOpenOption.APPEND ); } catch (IOException e) { System.out.println(e.getMessage()); } // ČITANJE FILEA RED PO RED Path path = Path.of("podaci.txt"); try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) { String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { System.out.println(e.getMessage()); } // ČITANJE CSV-A RED PO RED + SPLIT Path path = Path.of("gradovi.csv"); try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) { String line; reader.readLine(); // preskoči header while ((line = reader.readLine()) != null) { String[] p = line.split(","); String naziv = p[0]; String drzava = p[1]; long populacija = Long.parseLong(p[2]); System.out.println(naziv + " " + drzava + " " + populacija); } } catch (IOException e) { System.out.println(e.getMessage()); } // ČITANJE CIJELOG FILEA U STRING Path path = Path.of("podaci.txt"); try { String tekst = Files.readString(path, StandardCharsets.UTF_8); System.out.println(tekst); } catch (IOException e) { System.out.println(e.getMessage()); } // ČITANJE SVIH LINIJA U LISTU Path path = Path.of("podaci.txt"); try { List lines = Files.readAllLines(path, StandardCharsets.UTF_8); for (String line : lines) { System.out.println(line); } } catch (IOException e) { System.out.println(e.getMessage()); } // ČITANJE CSV-A U LISTU STRINGOVA, BEZ HEADERA Path path = Path.of("gradovi.csv"); try { List lines = Files.readAllLines(path, StandardCharsets.UTF_8); for (int i = 1; i < lines.size(); i++) { String[] p = lines.get(i).split(","); String naziv = p[0]; String drzava = p[1]; long populacija = Long.parseLong(p[2]); System.out.println(naziv + " " + drzava + " " + populacija); } } catch (IOException e) { System.out.println(e.getMessage()); } // PISANJE CSV-A Path path = Path.of("gradovi.csv"); try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) { writer.write("naziv,drzava,populacija"); writer.newLine(); writer.write("Zagreb,Hrvatska,800000"); writer.newLine(); writer.write("Berlin,Njemacka,3600000"); writer.newLine(); writer.write("Madrid,Spanjolska,3200000"); writer.newLine(); } catch (IOException e) { System.out.println(e.getMessage()); } // ČITANJE CSV-A + FILTRIRANJE Path path = Path.of("gradovi.csv"); try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) { String line; reader.readLine(); while ((line = reader.readLine()) != null) { String[] p = line.split(","); String naziv = p[0]; String drzava = p[1]; long populacija = Long.parseLong(p[2]); if (populacija > 1_000_000) { System.out.println(naziv + " - " + drzava + " - " + populacija); } } } catch (IOException e) { System.out.println(e.getMessage()); } // PISANJE BINARNIH PODATAKA try (DataOutputStream out = new DataOutputStream( new BufferedOutputStream( new FileOutputStream("studenti.bin")))) { out.writeInt(2); // broj zapisa out.writeUTF("Ana"); out.writeUTF("12345"); out.writeInt(1); out.writeDouble(4.5); out.writeUTF("Marko"); out.writeUTF("67890"); out.writeInt(2); out.writeDouble(3.8); } // ČITANJE BINARNIH PODATAKA try (DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream("studenti.bin")))) { int brojStudenata = in.readInt(); for (int i = 0; i < brojStudenata; i++) { String ime = in.readUTF(); String jmbag = in.readUTF(); int godina = in.readInt(); double prosjek = in.readDouble(); System.out.println(ime + " " + jmbag + " " + godina + " " + prosjek); } } // PISANJE OBJEKTA try (ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("korisnik.ser"))) { out.writeObject(korisnik); } // ČITANJE OBJEKTA try (ObjectInputStream in = new ObjectInputStream( new FileInputStream("korisnik.ser"))) { Korisnik korisnik = (Korisnik) in.readObject(); System.out.println(korisnik); } //Novo //Pjesma package hr.algebra.blitz04.rp1.zadatak1; import java.io.Serializable; public class Pjesma implements Serializable { private static final long serialVersionUID = 1L; private int id; private String naslov; private String izvodac; private double trajanje; public Pjesma() { } public Pjesma(int id, String naslov, String izvodac, double trajanje) { this.id = id; this.naslov = naslov; this.izvodac = izvodac; this.trajanje = trajanje; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getNaslov() { return naslov; } public void setNaslov(String naslov) { this.naslov = naslov; } public String getIzvodac() { return izvodac; } public void setIzvodac(String izvodac) { this.izvodac = izvodac; } public double getTrajanje() { return trajanje; } public void setTrajanje(double trajanje) { this.trajanje = trajanje; } @Override public String toString() { return "Pjesma{" + "id=" + id + ", naslov='" + naslov + '\'' + ", izvodac='" + izvodac + '\'' + ", trajanje=" + trajanje + '}'; } } //Zadatak1 main package hr.algebra.blitz04.rp1.zadatak1; import java.io.*; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List pjesme = new ArrayList<>(); pjesme.add(new Pjesma(1, "Lose Yourself", "Eminem", 5.26)); pjesme.add(new Pjesma(2, "Numb", "Linkin Park", 3.15)); pjesme.add(new Pjesma(3, "Believer", "Imagine Dragons", 3.24)); try (ObjectOutputStream oos = new ObjectOutputStream( new BufferedOutputStream( new FileOutputStream("podaci.ser")))) { oos.writeObject(pjesme); } catch (IOException e) { e.printStackTrace(); } try (ObjectInputStream ois = new ObjectInputStream( new BufferedInputStream( new FileInputStream("podaci.ser")))) { List ucitanePjesme = (List) ois.readObject(); for (Pjesma p : ucitanePjesme) { System.out.println(p); } } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } } //Zadatak2 main package hr.algebra.blitz04.rp1.zadatak2; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import hr.algebra.blitz04.rp1.zadatak1.Pjesma; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { ObjectMapper mapper = new ObjectMapper(); mapper.enable(SerializationFeature.INDENT_OUTPUT); List pjesme = new ArrayList<>(); pjesme.add(new Pjesma(1, "Lose Yourself", "Eminem", 5.26)); pjesme.add(new Pjesma(2, "Numb", "Linkin Park", 3.15)); pjesme.add(new Pjesma(3, "Believer", "Imagine Dragons", 3.24)); Path path = Path.of("podaci.json"); try { mapper.writeValue(path.toFile(), pjesme); String json = Files.readString(path); System.out.println(json); List ucitanePjesme = mapper.readValue(path.toFile(), new TypeReference>() {}); for (Pjesma p : ucitanePjesme) { System.out.println(p); } } catch (IOException e) { e.printStackTrace(); } } }