Løsningsforslag - oppgaver i Vedlegg A.6.2


Oppgave 1

Hvis til-filen finnes fra før og vi tillater at den trunkeres, så må vi bruke REPLACE_EXISTING:

  Path fra = Paths.get("C:/NetBeansAlgDat/AlgDat/fil.txt");
  Path til = Paths.get("C:/NetBeansAlgDat/AlgDat/kopifil.txt");
  Files.copy(fra, til, StandardCopyOption.REPLACE_EXISTING);

Oppgave 2

  public static void postorden(File mappe)
  {
    for (File fil : mappe.listFiles())
    {
      if (fil.isDirectory()) postorden(fil);
      System.out.println(fil);
    }
  }

  public static void main(String... args) throws IOException
  {
    Path start = Paths.get("C:/NetBeansAlgDat/AlgDat");
    postorden(start.toFile());
  }

  public static void postorden(Path mappe) throws IOException
  {
    Files.list(mappe).forEach(vei ->
      {
        if (vei.toFile().isDirectory())
          try
          {
            postorden(vei);
          } catch (IOException ex) {}

        System.out.println(vei);
      }
    );
  }

  public static void main(String... args) throws IOException
  {
    Path start = Paths.get("C:/NetBeansAlgDat/AlgDat");
    postorden(start);
  }